gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:09:49 25 4
gpt4 key购买 nike

使用@ResponseBody,我的 Controller 默认返回我的pojo的JSON表示,但默认情况下可以将 View 更改为JSP,并且仅当您的内容类型为application/json时才返回JSON响应>?

@RequestMapping(value="/myRequest")
public @ResponseBody myPojo myRequest() throws Exception {
return service.getMyPojo();
}

PS:我尝试过 ContentNegotiatingViewResolver,但我不确定这是实现这一目标的最佳选择。

最佳答案

您可以有两个映射:

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

关于java - Spring MVC : JSP or JSON view in Controller method depending on request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3822506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com