Spring MVC返回自定义HTTP状态代码(无错误) [英] Spring MVC return custom HTTP status code (no error)

查看:375
本文介绍了Spring MVC返回自定义HTTP状态代码(无错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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状态代码(无错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆