I am woring with Spring boot and jpa to manage connection to postgresql database.

This is what i use for general exception purpose.

@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice
public class GeneralExceptionHandler extends ResponseEntityExceptionHandler {
    @Override
    protected ResponseEntity<Object> createResponseEntity(Object body, HttpHeaders headers, HttpStatusCode statusCode,
                                                          WebRequest request) {
        HttpStatus httpStatus = HttpStatus.resolve(statusCode.value());

        if (httpStatus == null) return ResponseHandler.generateErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR);
        return ResponseHandler.generateErrorResponse(httpStatus, ((ProblemDetail) body).getDetail());
    }
}
    public static ResponseEntity<Object> generateErrorResponse(@NotNull HttpStatus status, String message) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("error", status.getReasonPhrase());
        map.put("message", message);
        map.put("status", status.value());

        return new ResponseEntity<Object>(map, status);
    }

body = ProblemDetail[type='about:blank', title='Internal Server Error', status=500, detail='Failed to write request', instance='null', properties='null']

i get this response

{
    "data"
}{
    "error": "Internal Server Error",
    "message": "Failed to write request",
    "status": 500
}

as you can clearly see with "data"}{ in plus

Comment From: bclozel

This happens because the response is written to before the exception happens. This should be addressed by https://github.com/spring-projects/spring-framework/pull/31104 and https://github.com/spring-projects/spring-framework/issues/31154

There's nothing we can do in the Spring Boot project to fix that. Please try the latest Spring Boot 3.2 milestones to validate the fix.