java.lang.IllegalStateException: Optional int parameter 'pageSize' is present but cannot be translat

来源:互联网 发布:js给span标签赋值 编辑:程序博客网 时间:2024/05/19 20:18

我的spring mvc 代码:

[java] view plain copy
  1. @Controller  
  2. @RequestMapping("/product")  
  3. public class Fancy {  
  4.     @RequestMapping(value = "/fancy")  
  5.     @ResponseBody  
  6.     public String showFancy(@RequestParam(value = "page", required = falseint page) {  
  7.         return "{\"status\":\"ok\"+}"+page+"\t";   
  8.     }  
  9. }  

报错:

[plain] view plain copy
  1. Optional int parameter 'page' is present but cannot be translated into a null value due to being dec  

继续查看出错内容:

[plain] view plain copy
  1. Request processing failed; nested exception is java.lang.IllegalStateException: Optional int parameter 'page' is present but cannot be translated   
  2. into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type  

大意是说 如果参数是非必须的,则会赋值为null,因此参数应该是一个object,它才能接受这个null值。

而上面代码参数page 的类型 为 int,它接受不了null值。

解决方法:

将int 改为 对象类型 Integer :

[plain] view plain copy
  1. @RequestParam(value = "page", required = false) Integer page  
问题解决。
阅读全文
0 0