Spring @ExceptionHandler does not work with @ResponseBody

后端 未结 7 1602
南笙
南笙 2020-11-29 23:45

I try to configure a spring exception handler for a rest controller that is able to render a map to both xml and json based on the incoming accept header. It throws a 500 se

7条回答
  •  无人及你
    2020-11-30 00:38

    Thanx, seems I overread this. That's bad... any ideas how to provides exceptions automatically in xml/json format?

    New in Spring 3.0 MappingJacksonJsonView can be utilized to achieve that:

    private MappingJacksonJsonView  jsonView = new MappingJacksonJsonView();
    
    @ExceptionHandler(Exception.class)
    public ModelAndView handleAnyException( Exception ex )
    {
        return new ModelAndView( jsonView, "error", new ErrorMessage( ex ) );
    }
    

提交回复
热议问题