Spring MVC-缺少字段的HTTP状态代码400(错误请求),定义为不需要 [英] Spring MVC - HTTP status code 400 (Bad Request) for missing field which is defined as being not required

查看:133
本文介绍了Spring MVC-缺少字段的HTTP状态代码400(错误请求),定义为不需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用此控制器方法的Spring MVC应用程序.

I have Spring MVC application with this controller method.

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addNumber(@RequestParam(value="number", required=false) Long number) {
   ...
   return "redirect:/showAll/";
}

在我的JSP中,我有一个标准的HTML表单,该表单将一个名为"number"的值发布到上述控制器方法中.但是,如果我遗漏了该值(不要在文本字段中输入任何内容)并将数据过帐到控制器,则在调用控制器方法之前,浏览器将显示

In my JSP I have a standard HTML form which is posting a value named "number" to the controller method above. However, if I leave out the value (do not enter anything into the text field) and POST the data to the controller, before the controller method is called my browser shows

HTTP Status 400 - Required Long parameter 'number' is not present

尽管控制器方法注释清楚地将数字"参数定义为不是必需的.

although the controller method annotation clearly defines the "number"-parameter as not required.

有人对发生的事情有一点想法吗?

Does anyone have a slight idea of what could be going on?

谢谢.

PS:引发的异常如下:

PS: The exception that is being thrown is as follows:

org.springframework.web.bind.MissingServletRequestParameterException: Required Long parameter 'number' is not present

这是Spring 3.2.3.RELEASE的错误(请参见此处).在版本3.1.4.RELEASE中,我不再存在此问题.

This is a Spring 3.2.3.RELEASE bug ( see here). With version 3.1.4.RELEASE I do not have this problem anymore.

推荐答案

我遇到了相同的情况,当您的参数以空值出现在请求中时,就会发生这种情况.

I came across the same situation, and this happens when your parameter is present in the request with an empty value.

也就是说,如果您的POST正文包含"number =(具有空值),那么Spring会抛出此异常.但是,如果请求中根本不存在该参数,则该参数应该可以正常工作.

That is, if your POST body contains "number=" (with empty value), then Spring throws this exception. However, if the parameter is not present at all in the request, it should work without any errors.

这篇关于Spring MVC-缺少字段的HTTP状态代码400(错误请求),定义为不需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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