-
Notifications
You must be signed in to change notification settings - Fork 40.4k
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
BasicErrorController returns body even if NO_CONTENT status causing 406 later #18136
Comments
By default the registered message converter in Thats the reason for getting 406 as there is no message converter found for To get the desired result of 204, try updating the @Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
converters.add(converter);
super.configureMessageConverters(converters);
}
} |
Thanks for trying to help, @s2agrahari. The 406 only occurs because the error controller is returning a body. If the response had no body, Spring MVC would not throw a |
@aleksanderlech Using an exception to return a 204 is, perhaps, a little unusual. It's also a little inefficient, which may or may not be a problem depending on how often your application will return such a response. You may want to consider returning a @GetMapping(path = "/{id}/content")
public ResponseEntity<FileSystemResource> getContent(@PathVariable String id) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} This will take Boot's error handling and error controller completely out of the picture. |
Have in mind that this is just example to reproduce the error. In bigger projects using exceptions can be more handy and allows you to abstract from the ResponseEntity. Anyway this is not discussion about the use cases but just a bug report I found in BasicErrorController :) |
This is exactly what happens. When the exception its thrown the JSON body is generated no matter what causing the HttpMediaTypeNotAcceptableException later on. I think NO_CONTENT should be handled there different way (either not attach the body or check before throwing the exception). |
Having exception defined like this:
And the controller method as following:
I would expect that the response code will be no content no matter what but while executing
I am getting 406 (Not Acceptable) because the BasicErrorController that always attaches the body no matter what the status code is:
will later cause 406 as the body cannot be converted to the requested type. It seems to be a bug to me as according to the spec the no content response should never contain body.
The text was updated successfully, but these errors were encountered: