Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content-Type not set correctly on @ControllerAdvice [SPR-17394] #21927

Closed
spring-projects-issues opened this issue Oct 16, 2018 · 3 comments
Closed
Assignees
Labels
in: core status: declined

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Oct 16, 2018

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
@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 26, 2018

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.

@spring-projects-issues spring-projects-issues added status: declined type: regression in: core labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: regression label Jan 12, 2019
@fdw
Copy link

@fdw fdw commented Mar 6, 2020

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

@jamietanna
Copy link

@jamietanna jamietanna commented Jan 18, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core status: declined
Projects
None yet
Development

No branches or pull requests

4 participants