开发者

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";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜