mpapenbr opened SPR-17394 and commented

Given a class with @ControllerAdvice annotation

@ControllerAdvice
@RequestMapping(produces = {  "application/vnd.someapp-v1.0+json;charset=utf-8" })
public class RestErrorHandler {
...
@ExceptionHandler(OperationWithProblemsException.class) @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY) 
@ResponseBody public OperationResult processOperationWithProblemsException(OperationWithProblemsException ex) {  
  return ex.getResult();
}

When running with Spring 5.0.X the content type specified in the produces-attribute of the RequestMapping annotation was respected and sent to the client upon this exception.

Starting with Spring 5.1.0 this has somehow changed and now I get 

application/json;charset=UTF-8

instead of the expected

application/vnd.someapp-v1.0+json;charset=utf-8

Affects: 5.1.1

Issue Links: - #20865 @RequestMapping produces condition should not impact error responses

Comment From: spring-projects-issues

Rossen Stoyanchev commented

This is as a result of #20865. The media type from the produces condition is primarily for the success scenario and may be wide ranging, while error responses are typically fixed, so the two may differ (as in the example from #20865) and one should not dictate the other.

One option is to return a ResponseEntity with the content type to use. Another would be to customize ExceptionHandlerExceptionResolver with a ContentNegotiationManager that returns a fixed content type for error responses.

Comment From: fdw

Are there any plans to have an annotation-like way to set the content-type for the response?

Comment From: jamietanna

Maybe see #27944, but also https://www.jvt.me/posts/2022/01/18/spring-negotiate-exception-handler/ which although doesn't use annotations, does allow for content-negotiation of the response