-
Notifications
You must be signed in to change notification settings - Fork 38.2k
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
If controller method has produces="*/*" in 5.2.3 response is 500 instead of 406 #24466
Comments
Can reproduce with a simple controller: @RestController
@RequestMapping("foo")
public class ResourceController {
@GetMapping(produces = MediaType.ALL_VALUE)
public void foo(final HttpServletResponse response) throws IOException {
response.setHeader("Content-Range", "bytes */foo");
response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE); //416
}
} RestAssured
.given(//**.....**//)
.header(HttpHeaders.RANGE, "bytes=10000-")
.when()
.port(this.port)
.get("/foo")
.then()
.statusCode(Matchers.is(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE.value())); //fails |
In spring 5.2.2 the behavior didn't seem all too right neither in fact, because instead of returning |
Yes, overriding 406 (Not Acceptable) with a 500 has some recent history. Starting in 5.2 and #23205, if a controller method returns a In 5.2.3 and #23287, this was further extended for the case where a controller method declares what it produces, and yet fails to produce it. Arguably through we should be double checking that the declared mime type is concrete, so I'm scheduling a follow-up fix. That said, a |
On closer look, the problem is not what seems. This happens after Boot's Typically this attribute would be removed in case of an exception from the controller, so as to not interfere with error response rendering, but in this case the controller method operates directly on the response and calls Keep in mind however, this fix wouldn't help because on some Servlet container, @GetMapping(produces = MediaType.ALL_VALUE)
public void foo(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
response.setHeader("Content-Range", "bytes */foo");
request.removeAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE); // <---
response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
} Note also that Spring MVC does support HTTP ranges. If you return a @GetMapping(produces = MediaType.ALL_VALUE)
public Resource foo() {
Resource resource = ... ;
return resource;
} |
I should also note with the above workaround in place, and Boot 2.2.4, I get a 416 response. |
You should also see the following spring-projects/spring-boot#19522 which is coming in Boot 2.3 as an improvement so that error detail rendering does not end up overriding the original error response if it fails to find a compatible media type (say client wants text, but we want to render a JSON error response). |
With the following commit, if a RequestMapping specifies
produces="*/*
and the request headers don't specify any accept, it will override the response with a 500 as the code below suggests :34d3240#diff-24571dc5c7743b2a69f7a5df4f2109b2R316
The text was updated successfully, but these errors were encountered: