오류
cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance ofcom.crudtest.request.UserCreate
(although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 2, column: 2]]
컨트롤러 /email 로 POST 요청과 함 UserCreate DTO 에 맞춰서 JSON 데이터를 전달했음에도 불구하고 맵핑 안되고 에러뜸
//컨트롤러
@PostMapping("/email")
public Map email(@RequestBody UserCreate request) {
request.validate();
Long userID = userService.emailWrite(request);
return Map.of("userID",userID);
}
//UserCreate DTO
@ToString
@Getter
@Setter
public class UserCreate {
@NotBlank(message = "이메일을 입력해주세요.")
@Email(message = "이메일 형식에 맞지 않습니다.")
private String email;
@Builder
public UserCreate(String email){
this.email = email;
}
public void validate(){
if(StringUtils.isNullOrEmpty(email)){
throw new InvalidRequest("email","이메일을 입력해주세요.");
}
if(!isValidEmail(email)){
throw new InvalidRequest("email","잘못된 이메일 형식입니다.");
}
}
}
Entity 클래스를 반환해주는 과정에서 클래스의 JSON Serialize 과정에서 오류가 났었다.
인자 없는 생성자를 추가해주면 간단하게 해결이 가능하다.
//UserCreate DTO
@ToString
@Getter
@Setter
public class UserCreate {
@NotBlank(message = "이메일을 입력해주세요.")
@Email(message = "이메일 형식에 맞지 않습니다.")
private String email;
@Builder
public UserCreate(String email){
this.email = email;
}
public UserCreate(){} // 추가
public void validate(){
if(StringUtils.isNullOrEmpty(email)){
throw new InvalidRequest("email","이메일을 입력해주세요.");
}
if(!isValidEmail(email)){
throw new InvalidRequest("email","잘못된 이메일 형식입니다.");
}
}
}