您的位置:首页 > 编程语言 > Java开发

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

2017-07-12 10:22 676 查看
我的spring mvc 代码:

[java] view
plain copy

@Controller  

@RequestMapping("/product")  

public class Fancy {  

    @RequestMapping(value = "/fancy")  

    @ResponseBody  

    public String showFancy(@RequestParam(value = "page", required = false) int page) {  

        return "{\"status\":\"ok\"+}"+page+"\t";   

    }  

}  

报错:

[plain] view
plain copy

Optional int parameter 'page' is present but cannot be translated into a null value due to being dec  

继续查看出错内容:

[plain] view
plain copy

Request processing failed; nested exception is java.lang.IllegalStateException: Optional int parameter 'page' is present but cannot be translated   

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

@RequestParam(value = "page", required = false) Integer page  

问题解决。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐