0

Here is my Exception handler example

   @ExceptionHandler
public ResponseEntity<ApiErrorResponse> handleException(EntityNotFoundException exc) {
    ApiErrorResponse error = new ApiErrorResponse();
    error.setStatus(HttpStatus.FORBIDDEN.value());
    error.setMessage(exc.getMessage());
    error.setTimeStamp(System.currentTimeMillis());
    return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
}

Here is the exception I throw

    public static void throwExceptionIfBindingResultHasErrors(BindingResult bindingResult) {
    List<FieldError> fieldErrorList = bindingResult.getFieldErrors();
    var stringBuilder = new StringBuilder();

    fieldErrorList
            .forEach(fieldError -> stringBuilder.append("Field name: ")
                    .append(fieldError.getField())
                    .append(" Rejected Value: ")
                    .append(fieldError.getRejectedValue())
                    .append(" Message ")
                    .append(fieldError.getDefaultMessage())
                    .append(System.lineSeparator()));

    throw new InvalidRequestException(stringBuilder.toString());
}

Here is the result I get ( no new lines, just \r\n symbols )

"status": 400,
"message": "Field name: address Rejected Value: toooloooongStriingstoooloooongStriingstoooloooongStriings Message must be between 5 and 60\r\nField name: name Rejected Value: toooloooongStriingstoooloooongStriingstoooloooongStriingstoooloooongStriings Message must be between 5 and 50\r\n",
"timeStamp": 1629117439443
  • 2
    Your response is json...that is how space is representation in json.. https://stackoverflow.com/questions/42068/how-do-i-handle-newlines-in-json – JCompetence Aug 16 '21 at 12:49

0 Answers0