如果我有一个类似于以下的请求映射:
@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public void test(@RequestParam(value = "i", defaultValue = "10") int i) {
}
然后使用以下命令调用此请求:
http://example.com/test?i=
我收到错误消息
无法将“java.lang.String”类型的值转换为“int”类型;嵌套异常是 java.lang.NumberFormatException: For input string: ""'
我可以通过阻止 javascript 客户端发送空参数或通过接受字符串值并仅在未发现它们为空时才解析来解决此问题。
更新:更高版本的 spring 现在实现了最初想要的行为。
我刚刚在春季 4.3.5 中对此进行了测试,发现现在的行为实际上会将 null 值转换为默认值而无需提高 a NumberFormatException
,因此;我原来的映射现在工作正常。
我不确定这种行为改变是哪个版本的 spring。