如果我有一个类似如下的请求映射:
@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public void test(@RequestParam(value = "i", defaultValue = "10") int i) {
}
然后调用此请求与:
http://example.com/test?i=
我得到错误消息
Failed to convert value of type ‘java.lang.String’ to type ‘int’; nested exception is java.lang.NumberFormatException: For input string: “”‘
我可以通过停止javascript客户端发送空参数,或接受字符串值,只有解析,如果没有发现是空白的解决这个问题。
更新:弹簧的更后版本现在实现最初期望的行为。
我刚刚在4.3.5Spring测试,发现,现在的行为现在将null值转换为默认值,而不会引发NumberFormatException,因此;我的原始映射现在工作正常。
我不知道哪个版本的Spring这种行为的变化。