开发者

Spring MVC: JSP or JSON view in Controller method depending on request

开发者 https://www.devze.com 2023-01-17 21:47 出处:网络
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 a

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

精彩评论

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