6

code is like this:

   @Controller
    public class TestController {

        @RequestMapping(value = "/testerror", method = RequestMethod.GET)
        public @ResponseBody ErrorTO testerror(HttpServletRequest request,HttpServletResponse response) {
           throw new ABCException("serious error!");
        }


        @ExceptionHandler(ABCException.class)
        public  @ResponseBody ErrorTO handleException(ABCException ex,
                HttpServletRequest request, HttpServletResponse response) {
            response.setStatus(response.SC_BAD_REQUEST);
            return new ErrorTO(ex.getMessage());
        }


     @RequestMapping(value = "/test", method = RequestMethod.GET)
    public @ResponseBody ErrorTO test(HttpServletRequest request, 
                                      HttpServletResponse response) {
        ErrorTO error = new ErrorTO();
        error.setCode(-12345);
        error.setMessage("this is a test error.");
        return error;
    }

}

when I tried curl -H "Accept:application/json" -v "http://localhost.com:8080/testerror" I got this error: org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Could not find HttpMessageConverter that supports return type [class com.kibboko.poprocks.appservices.dtos.ErrorTO] and [application/json]

but if I try curl -H "Accept:application/json" -v "http://localhost.com:8080/test", worked and returned json response. "application/xml" worked too.

Is there anything special about exception handler I need to take care of so it can work with json or xml? Thanks!

2 Answers 2

7

It appears that AnnotationMethodHandlerExceptionResolver has its own array of HttpMessageConverters. You need to configure it to use the same array as used by AnnotationMethodHandlerAdapter.

However, it can be complicated when AnnotationMethodHandlerAdapter is declared implicitly. Perhaps declaring the following FactoryBean may help in all cases:

public class AnnotationMethodHandlerExceptionResolverFactoryBean
        implements FactoryBean<AnnotationMethodHandlerExceptionResolver> {
    @Autowired
    private AnnotationMethodHandlerAdapter a;

    public AnnotationMethodHandlerExceptionResolver getObject()
            throws Exception {
        AnnotationMethodHandlerExceptionResolver r = new AnnotationMethodHandlerExceptionResolver();
        r.setMessageConverters(a.getMessageConverters());
        return r;
    }

    ...
}
Sign up to request clarification or add additional context in comments.

4 Comments

That is really a hassle. Would it be possible to find a simpler solution? Can you give me a working example?
@Bobo: Have you tried to declare that bean in your context? It should work fine.
@axtavt: sorry I am a real novice in Spring land. By "declare that bean in your context" you mean declaring it in spring configuration file?
@axtavt: so Spring uses this FactoryBean when he needs the exceptionResolver, right?! we don't need to call the getObject() manually in the @ExceptionHandler annotated method, right?
4

Seems like a known Spring bug (Fixed in 3.1 M1)

https://jira.springsource.org/browse/SPR-6902

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.