我的spring mvc 代码:
@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";
}
}
报错:
Optional int parameter 'page' is present but cannot be translated into a null value due to being dec
继续查看出错内容:
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 :
@RequestParam(value = "page", required = false) Integer page
问题解决。