简体   繁体   English

是否可以让空的 RequestParam 值使用 defaultValue?

[英]Is it possible to have empty RequestParam values use the defaultValue?

if I have aa request mapping similar to the following:如果我有一个类似于下面的请求映射:

@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public void test(@RequestParam(value = "i", defaultValue = "10") int i) {
}

And then call this request with:然后调用此请求:

http://example.com/test?i=

I get the error message我收到错误信息

Failed to convert value of type 'java.lang.String' to type 'int';无法将“java.lang.String”类型的值转换为“int”类型; nested exception is java.lang.NumberFormatException: For input string: ""'嵌套异常是 java.lang.NumberFormatException: For input string: ""'

I can solve this by either stopping the javascript client from sending empty parameters, or by accepting string values and only parsing if they are not found to be blank.我可以通过阻止 javascript 客户端发送空参数,或者接受字符串值并仅在未发现它们为空时才进行解析来解决此问题。

UPDATE : Later versions of spring now implement the originally desired behaviour.更新:spring 的更高版本现在实现了最初期望的行为。

I've just tested this in spring 4.3.5 and have found that the behaviour will now in fact turn the null value into the default value without raising a NumberFormatException , thus;我刚刚在 spring 4.3.5 中对此进行了测试,发现该行为现在实际上会将 null 值转换为默认值,而不会引发NumberFormatException ,因此; my original mapping now works fine.我原来的映射现在工作正常。

I am not sure which version of spring this behavioural change was made.我不确定 spring 的哪个版本进行了此行为更改。

You could change the @RequestParam type to an Integer and make it not required. 您可以将@RequestParam类型更改为Integer并使其不是必需的。 This would allow your request to succeed, but it would then be null. 这将允许您的请求成功,但它将为null。 You could explicitly set it to your default value in the controller method: 您可以在控制器方法中将其显式设置为默认值:

@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public void test(@RequestParam(value = "i", required=false) Integer i) {
    if(i == null) {
        i = 10;
    }
    // ...
}

I have removed the defaultValue from the example above, but you may want to include it if you expect to receive requests where it isn't set at all: 我已从上面的示例中删除了defaultValue,但是如果您希望接收未完全设置的请求,则可能需要包含它:

http://example.com/test

You can keep primitive type by setting default value, in the your case just add "required = false" property: 您可以通过设置默认值来保留基本类型,在您的情况下只需添加“required = false”属性:

@RequestParam(value = "i", required = false, defaultValue = "10") int i

PS This page from Spring documentation might be useful: Annotation Type RequestParam PS Spring文档中的这个页面可能很有用: 注释类型RequestParam

您可以使用泛型类Integer而不是int来设置RequestParam,它将解决您的问题。

   @RequestParam(value= "i", defaultValue = "20") Integer i

You can use @Nullable with default value.您可以将@Nullable与默认值一起使用。

@Nullable @RequestParam(value = "i", defaultValue = "10") Integer i

你也可以这样做 -

 @RequestParam(value= "i", defaultValue = "20") Optional<Integer> i

This was considered a bug in 2013: https://jira.spring.io/browse/SPR-10180 这被认为是2013年的一个错误: https//jira.spring.io/browse/SPR-10180

and was fixed with version 3.2.2. 并使用版本3.2.2修复。 Problem shouldn't occur in any versions after that and your code should work just fine. 在此之后的任何版本中都不应该出现问题,并且您的代码应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM