问题描述
在Spring MVC @RequestMapping注释中,我返回JSP页面名称作为结果。这将返回HTTP状态代码200 OK。如何将此状态代码更改为201创建的状态?
In Spring MVC @RequestMapping annotation, I am returning JSP page name as result. This returns HTTP status code 200 OK. How can I change this status code to something like 201 created ?
@ResponseStatus不起作用。此外,HttpServletResponse将无法工作,因为我只需要返回我的自定义JSP页面。
@ResponseStatus doesn't work. Also, HttpServletResponse won't work since I need to return my custom JSP page only.
@RequestMapping(method = RequestMethod.POST)
public String addPhone(@ModelAttribute("phone") Phone phoneVO) {
phoneManager.addPhone(phoneVO);
return "redirect:/phone";
}
推荐答案
好的,我找到了解决方案:
Ok, I found the solution:
response.setStatus(HttpServletResponse.SC_CREATED);
返回phonePage;
如@SotiriosDelimanolis所述,重定向覆盖了setStatus中的值。所以,我没有重定向,而是直接调用JSP页面(同时也重新发送参数)。
As mentioned by @SotiriosDelimanolis, the redirect was overwriting the value in setStatus. So, instead of redirect, I am calling the JSP page directly (while also re-sending the parameters).
我想重定向,状态必须是HTTP OK 。
I guess with redirect, the status has to be HTTP OK.
这篇关于Spring MVC返回自定义HTTP状态代码(无错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!