@RestController
is not meant to be used to return views to be resolved. It is supposed to return data which will be written to the body of the response, hence the inclusion of @ResponseBody
. You can not selectively disable the @ResponseBody
on individual handler methods when @ResponseBody
is already annotation on class level.
You can work around it by returning ModelAndView
, which will work even in @RestController
, but you really shouldn't:
@RequestMapping
public ModelAndView renderFooList() {
ModelAndView mav = new ModelAndView("foo/list");
mav.addObject("foos", fooService.getFoos());
return mav;
}
It would be better to create separate controllers for normal handlers returning views and REST controllers for the RESTful stuff. Or to annotate the class with plain @Controller
and put @ResponseBody
on the methods where you actually need it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…