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
@ResponseBody does not work with @ExceptionHandler when reason is set in @ResponseStatus [SPR-9159] #13797
Comments
Rossen Stoyanchev commented Added code formatting tags. |
Rossen Stoyanchev commented I'm not sure what we can do. Consider that
It sounds like what you want to do is send information in the body of the response. Obviously you can do one or the other but not both. I'm not sure what your intent is with setting the reason but it sounds like it's not what you thought it was. |
Tim Reidel commented I was expecting the 'reason' string to be passed back in the "Reason-Phrase" part of the HTTP Status-Line. This should be possible even when sending back a serialized object instead of an HTML-formatted error page. That said specifying the Reason-Phrase in this case is a "nice to have" and is not required for my (or likely any) application to work. Perhaps a viable solution is to ignore the reason code when also using the |
Rossen Stoyanchev commented
In theory yes but I don't see a way to do it with the Servlet API. bq Perhaps a viable solution is to ignore the reason code when also using the We need to consider how all other return values combine as well. In the very least we can add some clarification to the |
Rossen Stoyanchev commented Changing from bug to improvement since there is no obvious way to do what's requested with the Servlet API. |
Rossen Stoyanchev commented I've added a fix that should prevent the IOException in the original description. Essentially when |
Tim Reidel opened SPR-9159 and commented
An IOException is thrown by Jetty when returning an object from an exception handler in my
@Controller
annotated class. Some investigation revealed that the issue was seen only when I defined an error response in the@ResponseStatus
annotation.An example
@ExceptionHandler
that demonstrates the issue:It seems the IOException is triggered by the HTTPConnection being written to twice in ServletInvocableHandlerMethod.invokeAndHandle()
If there is a reason defined in the
@ResponseStatus
then setResponseStatus() will write output text to the HTTP response which will cause the outputStream to close. Later within returnValueHandlers.handleReturnValue() the JAXB seralization attempts to write to the outputStream which is closed. This triggers the IOException.Below is a snippet from ServletInvocableHandlerMethod that shows the logic that generates the error page via sendError(). This is only called when this.responseReason is non-null. Thus if you define a reason in the
@ResponseStatus
annotation the@ResponseBody
annotation won't work.Affects: 3.1.1
Referenced from: commits 2295372, d52fc3b
The text was updated successfully, but these errors were encountered: