문제 상황
ResponseEntity가 아닌 제네릭을 이용한 공통 리스폰스 객체를 만들어 보내려 하였다.
전체 코드
@PostMapping("/join")
public ApiResult<String> join(@RequestBody Member member) throws DuplicateException {
log.info(member.toString());
try {
if (isDuplicateId(member)) {
throw new DuplicateException("중복된 ID 입니다.");
}
} catch (DuplicateException e) {
return new ApiResult<>(false, null, new ApiResult.ApiError("아이디 중복", HttpStatus.CONFLICT));
}
String userId = memberService.join(member);
return new ApiResult<>(true, userId, null);
}
package com.example.shoppingmall.member;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
@AllArgsConstructor
public class ApiResult<T> {
boolean success;
T response;
ApiError error;
@AllArgsConstructor
static class ApiError {
String message;
HttpStatus status;
}
}
에러 메세지
in Response body
406 Status Code와 함께 Response가 오지 않는다.
in Spring boot
Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: No acceptable representation]
접근이 불가능하다고 한다 ..!?
원인
@Getter가 없어 결과값을 클라이언트에서 역직렬화하는 데 실패하였기 때문이다.
@Getter을 추가해주어 해결해주었다.
package com.example.shoppingmall.member;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.http.HttpStatus;
@Getter // 추가
@AllArgsConstructor
public class ApiResult<T> {
boolean success;
T response;
ApiError error;
@AllArgsConstructor
@Getter // 추가
static class ApiError {
String message;
HttpStatus status;
}
}
참고
[Springboot - 에러해결]Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable repr
1. 에러 메시지 Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation] 2. 원인 - ContentsController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 /** * content의 이미지 저장 */ @PostMapping("/new/ima
chea-young.tistory.com
[에러 해결] HttpMediaTypeNotAcceptableException
스프링 프로젝트를 하면서 REST API를 만드는 도중 다음과 같은 에러를 만났습니다. "org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation... 기존 방식에서는 http body에 데
taengsw.tistory.com
'프레임워크 > Spring Boot' 카테고리의 다른 글
[Spring Boot] 유효성 검사 with @Valid, @Validated (0) | 2024.05.17 |
---|---|
[Spring] DO(Entity), DTO (0) | 2024.05.16 |
[Spring Boot] @JsonNaming, @JsonProperty (0) | 2024.05.13 |
[JAVA] Iterator & Foreach (0) | 2024.05.10 |
[JAVA] 배열 vs 리스트 (0) | 2024.05.10 |