작성
·
2.1K
0
아래와 같이 프로젝트에 적용 중입니다. Controller, Serializer 둘 다 sprigframework.validation의 Errors를 임포트하고 있습니다. 어떻게 해결해야할까요?
ErrorsSerializer
package com.kakaogame.queen.boysinthesharehouse.global.common;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.boot.jackson.JsonComponent;
import org.springframework.validation.Errors;
import java.io.IOException;
@JsonComponent
public class ErrorsSerializer extends JsonSerializer<Errors> {
@Override
public void serialize(Errors errors, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeStartArray();
errors.getFieldErrors().forEach(e -> {
try {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("field", e.getField());
jsonGenerator.writeStringField("objectName", e.getObjectName());
jsonGenerator.writeStringField("code", e.getCode());
jsonGenerator.writeStringField("defaultMessage", e.getDefaultMessage());
Object rejectedValue = e.getRejectedValue();
if (rejectedValue != null) {
jsonGenerator.writeStringField("rejectedValue", rejectedValue.toString());
}
jsonGenerator.writeEndObject();
} catch (IOException e1) {
e1.printStackTrace();
}
});
errors.getGlobalErrors().forEach(e -> {
try {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("objectName", e.getObjectName());
jsonGenerator.writeStringField("code", e.getCode());
jsonGenerator.writeStringField("defaultMessage", e.getDefaultMessage());
jsonGenerator.writeEndObject();
} catch (IOException e1) {
e1.printStackTrace();
}
});
jsonGenerator.writeEndArray();
}
}
PlayerController
@PutMapping("/{platformId}")
public ResponseEntity updatePlayer(@PathVariable Long platformId,
@RequestBody @Valid PlayerDto.UpdateRequest reqDto,
Errors errors) {
var optionalAccount = accountSearchService.getAccount(platformId);
if (optionalAccount.isEmpty())
return ResponseEntity.notFound().build();
if (errors.hasErrors())
return ResponseEntity.badRequest().body(errors);
var isSuccess = playerUpdateService.updatePlayer(optionalAccount.get(), reqDto);
var playerDto = new PlayerDto.UpdateResponse(isSuccess);
return ResponseEntity.ok(playerDto);
}
Error 메시지
org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.springframework.validation.DefaultMessageCodesResolver]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.validation.DefaultMessageCodesResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.validation.BeanPropertyBindingResult["messageCodesResolver"])
----------------------------------------------------------------------------------------------------------------
ObjectMapper 를 AutoWired 해서 다음과 같이하면 실행되긴하네요 ㅠ
return ResponseEntity.badRequest().body(objectMapper.writeValueAsString(errors));
답변 2
3
같은 문제가 발생하여 디버깅 해보니
실제 json 데이터로 직렬화 하려는 부분은 errors 안의 errors 안에 있어 이벤트 컨트롤러에서 ResponseEntity의 body에 errors 를 담는 부분을 errors.getAllErrors() 로 변경하니 오류 없이 진행 되네요
return ResponseEntity.badRequest().body(errors.getAllErrors());
0
No serializer found for class org.springframework.validation.DefaultMessageCodesResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
이 에러 메시지 발생하는 지점을 찾아 보시죠. 디버거를 써서 찾아보시고 어디서 왜 DefaultMessgeCodesResolver를 변환하려고 하는건지 찾아내야 할 것 같습니다.