개발/Spring

[에러 해결] HttpMediaTypeNotAcceptableException

|테드| 2021. 8. 14. 22:40

스프링 프로젝트를 하면서 REST API를 만드는 도중 다음과 같은 에러를 만났습니다.

"org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation...

 

기존 방식에서는 http body에 데이터 하나밖에 넘겨줄 수 없었기에, 제네릭을 이용한 공통 리스폰스 객체를 만들고 싶었습니다.

@PostMapping(value = "/login")
    public ResponseEntity<String> login(@RequestBody AuthRequest authRequest) throws Exception {
        //생략
        String token = "token";
        return ResponseEntity.status(HttpStatus.OK).body(token);
    }

위의 코드를

@PostMapping(value = "/login")
    public ResponseEntity<BasicResponse> login(@RequestBody AuthRequest authRequest) throws Exception {
        // 생략
        String token = "token";
        // SuccessResponse는 BasicResponse를 상속받음
        return ResponseEntity.status(HttpStatus.OK).body(new SuccessResponse<>(token));
    }

으로 바꾸던 중 만난 에러입니다.

 

핸들러가 클라이언트가 요청한 Type으로 응답을 내려줄 수 없는 것이 원인이었습니다.

이는 SuccessResponse에 Getter가 있지 않아서 생기는 문제였습니다.

SuccessResponse에 @Getter를 추가하여 해결했습니다.

 

참고 : https://stackoverflow.com/questions/28466207/could-not-find-acceptable-representation-using-spring-boot-starter-web

 

"Could not find acceptable representation" using spring-boot-starter-web

I am trying to use spring-boot-starter-web to create a rest service serving up JSON representations of Java objects. From what I understand this boot-starter-web jar is supposed to handle the conve...

stackoverflow.com

728x90
반응형