코드 저장소.

[Spring boot] Page Interface의 직렬화 본문

포폴/일정관리앱

[Spring boot] Page Interface의 직렬화

slown 2025. 1. 27. 18:26

목차

1. 문제 상황

2.해결책

 

1. 문제 상황

일정 프로젝트를 진행을 하면서 아래와 같은 경고 문구가 나왔다.

2025-01-27 18:15:42 [http-nio-8082-exec-2] [f88939bd-064c-4a9f-916c-14a228ece179] [] WARN  o.s.d.w.c.SpringDataJacksonConfiguration$PageModule$WarningLoggingModifier - Serializing PageImpl instances as-is is not supported, meaning that there is no guarantee about the stability of the resulting JSON structure!
	For a stable JSON structure, please use Spring Data's PagedModel (globally via @EnableSpringDataWebSupport(pageSerializationMode = VIA_DTO))
	or Spring HATEOAS and Spring Data's PagedResourcesAssembler as documented in https://docs.spring.io/spring-data/commons/reference/repositories/core-extensions.html#core.web.pageables.

 

그리고 컨트롤러 부분은 다음과 같다.

@GetMapping("/page")
    public ResponseEntity<Page<MemberResponse>> findAllPage(@PageableDefault Pageable pageable) {
        Page<MemberResponse> memberPageList = memberInConnector.findAll(pageable);
        logger.info("memberList::" + memberPageList);
        return ResponseEntity.status(HttpStatus.OK).body(memberPageList);
    }

 

데이터를 출력을 하는데 있어서는 문제는 없지만 이러한 경고 문구가 나온 이유는 페이징 구현이 직렬화되지 않아
JSON 형식의 body에 안정성이 없다는 것이 이 오류의 원인이다.

2.해결책

해결책은 의외로 간단하다. 메인 메서드 부분에 아래의 코드처럼 작성을 하면 경고 문구는 사라진다.

@EntityScan(basePackages = "com.example")
@EnableJpaRepositories(basePackages = "com.example")
@SpringBootApplication(scanBasePackages = "com.example")
@EnableSpringDataWebSupport(pageSerializationMode = EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO)
public class BootStrapApplication {

	public static void main(String[] args) {
		SpringApplication.run(BootStrapApplication.class, args);
	}
}