Results 1 to 10 of 23

Thread: ResponseEntity<?> return type @ExceptionHandler method in 3.2.0.RC2

Hybrid View

  1. #1

    Default ResponseEntity<?> return type @ExceptionHandler method in 3.2.0.RC2

    I just wrote up this bug: https://jira.springsource.org/browse/SPR-10090

    But I wanted to check here and make sure I'm not doing anything wrong. I have an @Controller class that defines an @ExceptionHandler that returns a ResponseEntity:

    Code:
    @ExceptionHandler(java.lang.Exception.class)
    public
    ResponseEntity<String>
    handleException(java.lang.Exception inEx)
    {
        ...
    }
    Pretty straightforward. But it throws an exception:

    Code:
    00:50:42.949 ERROR annotation.AnnotationMethodHandlerExceptionResolver (AnnotationMethodHandlerExceptionResolver.java:147) Invoking request method resulted in exception : public org.springframework.http.ResponseEntity com.latencyzero.gamecenter.web.ServiceController.handleException(java.lang.Exception)
    java.lang.IllegalArgumentException: Invalid handler method return value: <500 Internal Server Error,{"resultMsg":"Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Cannot open connection","result":1},{Content-Type=[application/json;charset=UTF-8]}>
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.getModelAndView(AnnotationMethodHandlerExceptionResolver.java:414)
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.doResolveException(AnnotationMethodHandlerExceptionResolver.java:144)
    	at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:136)
    	at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1148)
    	at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:985)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:917)
    	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:813)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:120)
    	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:798)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:97)
    and the source code doesn't seem to be able to handle anything that doesn't turn into a ModelAndView. However, the docs are pretty clear that the RequestEntity return type is supported, and there are even other classes provided by Spring that do that.

    So, am I missing something? Thanks!

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Your configuration is wrong... The AnnotationMethodHandlerExceptionResolver is indeed not supporting this it stems from the initial and first annotation-driven web mvc approach. You should be using the approriate exceptionhandler the ExceptionHandlerExceptionResolver.

    Either configure this explicitly or use the mvc:annotation-driven or @EnableWebMvc if you are using @Configuration for configuring your app.

    Note: According to the issue you modified the code, not sure why and also the @deprecated in the class should have been an indicator that you shouldn't use this class.

    Note2: The other case you linked to also mention the appropriate class to use.

    So please do a bit more/better investigation next time before raising a bug!
    Last edited by Marten Deinum; Dec 12th, 2012 at 04:52 AM. Reason: Fixed URL link.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Thanks for pointing that out.

    So please do a bit more/better investigation next time before raising a bug!
    Seriously? Dude, people here aren't very clear about stuff. I'm extraordinarily busy trying to get my app to work. I spent a lot of time trying to figure this out, something I didn't need to do, but got curious. IT IS NOT CLEAR what needs to be done to make things work. I read the docs on the classes and and the bug, and it was NOT CLEAR from either exactly what I had to do, especially in the context of a move from code that originated back in the 2.5 days. Even your post is not entirely clear. What is "mvc:annotation-driven"? As opposed to what? Obviously my code is annotation-driven, but if I'm not using "mvc:annotation-driven" then I must be using something else. You could be a lot more helpful by indicating what need to look for to change it. Now I gotta dig through the web to find the answer. Had the docs been clear in the first place, I might not've had to do any of this.

    But the docs lead me toward @ControllerAdvice, which is not what I wanted.

    Note that I haven't modified any code but my own copy, and provided the changes. I didn't notice the @deprecated in there, or I would not have wasted my time.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    What is "mvc:annotation-driven"? As opposed to what? Obviously my code is annotation-driven, but if I'm not using "mvc:annotation-driven" then I must be using something else
    I suggest a read of the reference guide (this is the 3.1 link because 3.2 isn't final yet). Which explains what is mvc:annotation-driven and what is registered when you use that. There is also a brief explanation of the DispatcherServlet defaults.

    When doing an upgrade I tend to read the documentation of the new product at least the section that are relevant. But that could just be me.

    it was NOT CLEAR from either exactly what I had to do
    The comment I linked to gives you the classes which provide the support you want and explains what you need to do.

    But to be short and solve your problem add mvc:annotation-driven to your dispatcher servlet context file.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    When doing an upgrade I tend to read the documentation of the new product at least the section that are relevant. But that could just be me.
    That can be a LOT of reading for something like Spring. And I find it very easy to completely forget about the one-time configuration I did way back when, so I wouldn't even know that there was something to go look at. More helpful would've been logging as the code executes indicating I'm using deprecated classes and pointing me at documentation summarizing the changes (rather than having to read through long docs to find the one bit that I need).

    But to be short and solve your problem add mvc:annotation-driven to your dispatcher servlet context file.
    Thank you. I'm reading the docs now. It's still not entirely clear, but it has revealed other things that need to be updated. I'm tired and I've wasted a lot of time with this issue (which of course manages to only come up now). Hopefully it'll be resolved soon.

  6. #6

    Default

    Argh, nope, still not clear. This doc pretty explicitly states I can choose either annotation-based ("Java config") OR XML. It does not say I need both.

    But if I have no .xml file, I get an ERROR exception logged at startup and it fails to initialize the context. Do I just put an empty XML file there? Inelegant, at best.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •