You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
@ResponseBody does not work with @ExceptionHandler [SPR-6902] #11567
@ExceptionHandler
@ResponseBody
public String dummyExceptionHandler(Exception e) {
return e.getMessage();
}
Spring mvc tries to resolve the view using the method output --> 404.
As the method is annotated with @ResponseBody, it should directly write the method output to the response.
Could the ResponseErrorHandler (for RestTemplate) get the error message from @ResponseBody in this case? Also, will the @ResponseBody from @ExceptionHandler supports the HttpMessageConversion?
where MyServiceException is mapped to XML via an XSD file.
I then put in my service-servlet.xml file the following annotation definition:
<!-- When an exception occurs, we support the following output formats -->
<beanid="outboundExceptionAdapter"class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
<propertyname="messageConverters">
<util:list>
<!-- Support XML marshaling -->
<beanclass="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<propertyname="marshaller"ref="xmlMarshaller"/>
<propertyname="unmarshaller"ref="xmlMarshaller"/>
</bean>
<!-- Return plain text -->
<beanclass="org.springframework.http.converter.StringHttpMessageConverter"/>
</util:list>
</property>
</bean>
<beanid="xmlMarshaller"class="org.springframework.oxm.xmlbeans.XmlBeansMarshaller"/>
And now when an exception occurs, I get nicely rendered XML returned with an HTTP 500 error code.
This issue is slightly different than #11359. If you annotate an @ExceptionHandler method with @ResponseBody, then Spring will use a MessageConverter to marshal the returned object to the proper response message (an XML document in your example). The fix for this issue (#11567) should make @ExceptionHandler and @ResponseBody work well together.
If you do not apply the @ResponseBody annotation to an @ExceptionHandler method, then Spring will try to use a View to marshal the returned object instead of a MessageConverter. MarshallingView is a View that can be used to marshal the object to XML (i.e. MarshallingView is the View analog to MarshallingHttpMessageConverter). Fixing #11359 should make this path work as expected, so developers can choose to use MessageConverters or Views to marshal the output.
Don't know if this is a regression, but with 3.0.5 I get the following warn message and the exception is thrown as if no handler was defined. Debugging shows that the handler is executed.
WARN : org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Could not find HttpMessageConverter that supports return type [class default.W3CGeoLocation] and [application/json, text/javascript, /]
The handler is as follows:
@ExceptionHandler
public @ResponseBody
W3CGeoLocation handleException(IllegalArgumentException exception) {
return new W3CGeoLocation();
}
W3CGeoLocation is a simple pojo. I return this object when everything is ok as well (here it is working fine), but when an exception happens I whant to return an empty object since this a Ajax request.
It would be really nice if exception handler method behaved like normal request handler method plus the feature that they are called when an exception in a request method happens. This would make the controller code much cleaner.
Right now it is not possible to pass a ModelMap to a exception handler method, and also all ModelAttributes get lost somehow. This means if I return the same view with a form in it, the form-tag cannot find the model anymore.
Arjen, given that you cannot reproduce the problem (and many of us are stumbling on this),
Could you include/document the necessary/sufficient conditions to make this work?
What version, what config, or what ever?
Sorry for not giving peace on this, but this time I made a small test project where you can verify the issue. I was unable to make a junit test for this, so you will have to click this through.
It is really simple. The controller looks like this:
@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home() {
return "index";
}
@RequestMapping(value = "/data", method = RequestMethod.GET)
public @ResponseBody
MyData get(@RequestParam boolean exception) {
if (exception) {
throw new IllegalArgumentException();
}
else {
return new MyData();
}
}
@ExceptionHandler(IllegalArgumentException.class)
public @ResponseBody
MyData handleException() {
return new MyData();
}
}
When you click on "Get JSON Data" everything is fine. When you click "Get Json Data with Exception" you get nothing in the browser and there is the following entry in tomcat log:
WARN : org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Could not find HttpMessageConverter that supports return type [class de.emaitie.test.MyData] and [application/json, text/javascript, /;q=0.01]
The issue was that the MappingJacksonHttpMessageConverter was not registered with the AnnotationMethodHandlerExceptionResolver (which handles @ExceptionHandler methods), only with the AnnotationMethodHandlerAdapter (which handles @RequestMapping methods).
This is now fixed, or at least when you use mvc:annotation-driven/.
Adrian, could you try a recent 3.1 SNAPSHOT (as of tonight), and see if it works for you? If so, we will backport it to 3.0.6.
Thanks Arjen. I tried it with 3.1.0.BUILD-20110111.020601-120 and its working now.
I was hoping that you could solve it a little bit differently. IMHO the RequestHandler and ExceptionHandler Adapter/Resolver should behave exactly the same, meaning allowing the same input parameters and return values. The only difference should be that the ExceptionHandler methods are called when something breaks in the RequestHandler methods.
This is already requested here: https://jira.springframework.org/browse/SPR-7637
Activity
spring-projects-issues commentedon Mar 15, 2010
Scott Frederick commented
This is somewhat related to #11359. Is there any chance that #11359 can be addressed sooner than 3.1?
spring-projects-issues commentedon Mar 15, 2010
Simon Wong commented
Could the ResponseErrorHandler (for RestTemplate) get the error message from
@ResponseBodyin this case? Also, will the@ResponseBodyfrom@ExceptionHandlersupports the HttpMessageConversion?spring-projects-issues commentedon Mar 17, 2010
Simon Wong commented
Add a new issue regarding to my concern
https://jira.springsource.org/browse/SPR-7001
spring-projects-issues commentedon Apr 5, 2010
Tyler Vallillee commented
I believe this bugfix also addresses #11359
I have the following web controller method:
where MyServiceException is mapped to XML via an XSD file.
I then put in my service-servlet.xml file the following annotation definition:
And now when an exception occurs, I get nicely rendered XML returned with an HTTP 500 error code.
spring-projects-issues commentedon Apr 6, 2010
Scott Frederick commented
Tyler,
This issue is slightly different than #11359. If you annotate an
@ExceptionHandlermethod with@ResponseBody, then Spring will use a MessageConverter to marshal the returned object to the proper response message (an XML document in your example). The fix for this issue (#11567) should make@ExceptionHandlerand@ResponseBodywork well together.If you do not apply the
@ResponseBodyannotation to an@ExceptionHandlermethod, then Spring will try to use a View to marshal the returned object instead of a MessageConverter. MarshallingView is a View that can be used to marshal the object to XML (i.e. MarshallingView is the View analog to MarshallingHttpMessageConverter). Fixing #11359 should make this path work as expected, so developers can choose to use MessageConverters or Views to marshal the output.spring-projects-issues commentedon Dec 1, 2010
Adrian commented
Don't know if this is a regression, but with 3.0.5 I get the following warn message and the exception is thrown as if no handler was defined. Debugging shows that the handler is executed.
WARN : org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Could not find HttpMessageConverter that supports return type [class default.W3CGeoLocation] and [application/json, text/javascript, /]
The handler is as follows:
@ExceptionHandlerpublic
@ResponseBodyW3CGeoLocation handleException(IllegalArgumentException exception) {
return new W3CGeoLocation();
}
spring-projects-issues commentedon Dec 9, 2010
Arjen Poutsma commented
@Adrian:I am not sure this is related to the issue above. Is W3CGeoLocation a supported object by the JSON message converter?
spring-projects-issues commentedon Dec 9, 2010
Adrian commented
W3CGeoLocation is a simple pojo. I return this object when everything is ok as well (here it is working fine), but when an exception happens I whant to return an empty object since this a Ajax request.
I think this whole thing is related to https://jira.springframework.org/browse/SPR-7637
It would be really nice if exception handler method behaved like normal request handler method plus the feature that they are called when an exception in a request method happens. This would make the controller code much cleaner.
Right now it is not possible to pass a ModelMap to a exception handler method, and also all ModelAttributes get lost somehow. This means if I return the same view with a form in it, the form-tag cannot find the model anymore.
spring-projects-issues commentedon Dec 20, 2010
Arjen Poutsma commented
I cannot reproduce this, using
@ResponseBodyfor@ExceptionHandlerworks fine for me (also when using a JSON response object).spring-projects-issues commentedon Dec 24, 2010
Jack Punt commented
Arjen, given that you cannot reproduce the problem (and many of us are stumbling on this),
Could you include/document the necessary/sufficient conditions to make this work?
What version, what config, or what ever?
spring-projects-issues commentedon Jan 10, 2011
Adrian commented
Sorry for not giving peace on this, but this time I made a small test project where you can verify the issue. I was unable to make a junit test for this, so you will have to click this through.
It is really simple. The controller looks like this:
The corresponding JSP file:
When you click on "Get JSON Data" everything is fine. When you click "Get Json Data with Exception" you get nothing in the browser and there is the following entry in tomcat log:
WARN : org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Could not find HttpMessageConverter that supports return type [class de.emaitie.test.MyData] and [application/json, text/javascript, /;q=0.01]
It is running on Springframework 3.0.5.
spring-projects-issues commentedon Jan 10, 2011
Adrian commented
Test project
spring-projects-issues commentedon Jan 10, 2011
Arjen Poutsma commented
Thanks Adrian, I will take another look at this now.
spring-projects-issues commentedon Jan 10, 2011
Arjen Poutsma commented
Fixed.
The issue was that the MappingJacksonHttpMessageConverter was not registered with the AnnotationMethodHandlerExceptionResolver (which handles
@ExceptionHandlermethods), only with the AnnotationMethodHandlerAdapter (which handles@RequestMappingmethods).This is now fixed, or at least when you use mvc:annotation-driven/.
Adrian, could you try a recent 3.1 SNAPSHOT (as of tonight), and see if it works for you? If so, we will backport it to 3.0.6.
spring-projects-issues commentedon Jan 11, 2011
Adrian commented
Thanks Arjen. I tried it with 3.1.0.BUILD-20110111.020601-120 and its working now.
I was hoping that you could solve it a little bit differently. IMHO the RequestHandler and ExceptionHandler Adapter/Resolver should behave exactly the same, meaning allowing the same input parameters and return values. The only difference should be that the ExceptionHandler methods are called when something breaks in the RequestHandler methods.
This is already requested here: https://jira.springframework.org/browse/SPR-7637
spring-projects-issues commentedon Jan 11, 2011
Rossen Stoyanchev commented
Arjen, based on the description of the fix #11850 is probably fixed too.
spring-projects-issues commentedon Jun 10, 2011
Randy Avis commented
There's a workaround at
http://stackoverflow.com/questions/6267021/spring-3-rest-responsebody-and-exceptionhandler/6311083