-
Notifications
You must be signed in to change notification settings - Fork 40.8k
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
Status code 404 turns into status code 406 when accept header is put to application/xml #13163
Comments
Thanks for the sample. The difference in behaviour is caused by Spring Boot's white label error page which aims to return something more useful out of the box than the empty body that you get with your Spring Framework-based example. With no
It can also return HTML:
When you ask for XML, an attempt is made to convert the same POJO that produces the above JSON response into XML. You application does not include any dependencies that are capable of producing XML from a POJO so Spring MVC deems the requested media type to be unacceptable and responds with a 406. You can disable The ideal would, perhaps, be to respond with an empty body if the error controller's response cannot be written in an acceptable media type. However, I'm not sure that it's possible with Spring MVC. @bclozel knows more about this than I do so he may have some suggestions. |
@wilkinsona is spot on - this is what happens in this case. There might be a way out of this limitation, but such a behavior would still be questionable when considering the RFCs:
For web APIs, I'm usually seeing components throwing custom business exceptions ( |
Thank you, @bclozel. |
@bclozel I have a similar problem. I can follow the points you gave if a client queries an url that is mapped but the accept-header isn't matching. However I think in this case when there is no mapping the 404 should take precedence with an empty body. Is there a way to work around this issue without getting a html body but maybe an empty body? |
@TobiasPfeifer I you believe this is a bug, please create a new issue with a project reproducing the problem. If this is more of a question, please create a StackOverflow question and feel free to point to it from here. In all cases, a description is not enough and a working sample is always better. |
@bclozel I've encountered the same problem. Is there any issues or StackOverflow pointing here? I'd like to know if it is fixed and when. Thanks |
@wangjingfei do you have a sample I can take a look at? If you believe this is a bug, please create a new issue with a sample application reproducing the problem. |
When your application gets a request and throws a
ResponseStatusException(HttpStatus.NOT_FOUND)
the response has a status code404
and a JSON body when no accept header is provided.When your application gets a request and throws a
ResponseStatusException(HttpStatus.NOT_FOUND)
the response has a status code404
and a JSON body when the accept header is put toapplication/json
When your application gets a request and throws a
ResponseStatusException(HttpStatus.NOT_FOUND)
the response has a status code406
and no body when the accept header is put toapplication/xml
.A demo can be found on https://github.com/desmethans/responsestatusexceptionboot.git
and can be seen with
curl http://localhost:8080/persons/2 --header "accept:application/xml" -i
A Spring application without Spring boot does not have this problem as demoed on https://github.com/desmethans/responsestatusexception.git
The text was updated successfully, but these errors were encountered: