请参阅我的以下 4 个简单示例,其中 2 个适用于 xml,另外 2 个不适用。
//works for html, json, xml
@RequestMapping(value = "/test", method = RequestMethod.GET)
public ModelAndView testContentNegiotation(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
TestTO test = new TestTO("some msg", -888);
mav.addObject("test", test);
mav.setViewName("test"); //test is a jsp page
return mav;
}
//does not work for xml
@RequestMapping(value = "/test", method = RequestMethod.GET)
public ModelAndView testContentNegiotation(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
ErrorTO error = new ErrorTO("some error", -111);
mav.addObject("error",error );
TestTO test = new TestTO("some msg", -888);
mav.addObject("test", test);
mav.setViewName("test");
return mav;
}
//works for xml and json
@RequestMapping(value = "/test3", method = RequestMethod.GET)
public @ResponseBody ErrorTO test3(HttpServletRequest request, HttpServletResponse response) {
ErrorTO error = new ErrorTO();
error.setCode(-12345);
error.setMessage("this is a test error.");
return error;
}
//does not work for xml
@RequestMapping(value = "/testlist", method = RequestMethod.GET)
public @ResponseBody List<ErrorTO> testList2(HttpServletRequest request, HttpServletResponse response) {
ErrorTO error = new ErrorTO("an error", 1);
ErrorTO error2 = new ErrorTO("another error", 2);
ArrayList<ErrorTO> list = new ArrayList<ErrorTO>();
list.add(error);
list.add(error2);
return list;
}
在两个不能生成xml的例子中,是否可以配置spring使其工作?