Im making RESTful API for practice. I want to valid check request body and made ExceptionHandler.class like below.
@Override
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request){
List<String> errorList = ex
.getBindingResult()
.getFieldErrors()
.stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.toList());
ApiResponse apiResponse=new ApiResponse(HttpStatus.BAD_REQUEST, ex.getMessage(), errorList, ex);
return new ResponseEntity<>(
apiResponse, HttpStatus.BAD_REQUEST
);
}
I made ApiResponse.class for response body in ResponseEntity<>.
@Data
@Slf4j
public class ApiResponse{
public HttpStatus status;
public String msg;
public List<String> err;
public Exception ex;
public ApiResponse(HttpStatus status, String msg, List<String> err, Exception ex){
this.status=status;
this.msg=msg;
this.err=err;
this.ex=ex;
}
}
but when MethodArgumentNotValidException happen, I got error like below...
Caused by: 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: com.practice.skillup.restfulapi.dto.ApiResponse["ex"]-
what is the reason and how can I resolve it. (I'm just a web develop beginner it would be very thankful to explain easily)