Spring MVC: JSP or JSON view in Controller method depending on request
Using @ResponseBody my controller returns a JSON representation of my pojo by default, but is possible to change the view to JSP by default, and return a JSON response only when your content type is application/json
?
@RequestMapping(value="/myRequest")
public @ResponseBody myPojo myRequest() throws Exception {
return service.getMyPojo();
}
PS: I've tried ContentNegotiatingViewResolver
, but开发者_如何学编程 I'm not sure is the best one for achieving this.
You can have two mappings:
@RequestMapping(value = "/myRequest", headers="content-type=application/json")
public @ResponseBody jsonExample() throws Exception {
return service.getMyPojo();
}
@RequestMapping(value = "/myRequest", headers="content-type=text/*")
public String jspExample() throws Exception {
return "myJspView";
}
精彩评论