ExceptionHandler returning JSON or XML not working in spring mvc 3

前端 未结 2 1996
谎友^
谎友^ 2020-12-28 23:52

code is like this:

   @Controller
    public class TestController {

        @RequestMapping(value = \"/testerror\", method = RequestMethod.GET)
        pub         


        
相关标签:
2条回答
  • 2020-12-29 00:25

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

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

    0 讨论(0)
  • 2020-12-29 00:33

    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;
        }
    
        ...
    }
    
    0 讨论(0)
提交回复
热议问题