Servlet.service() for servlet [springmvc] in context with path [] threw exception [Request processing failed; nest
ed 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 pr
imitive type.] with root cause
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.
@RequestMapping("/findAll")
public List<TbBrand> findAll(){
return brandService.findAll();
}
/**
* 返回全部列表
* @param page
* @param rows
* @return
*/
@RequestMapping("/findPage")
public PageResult findPage(int page,int size){
return brandService.findPage(page,size);
}
}
大意是说 如果参数是非必须的,则会赋值为null,因此参数应该是一个object,它才能接受这个null值。
而上面代码参数page 的类型 为 int,它接受不了null值。
解决方法:
将int 改为 对象类型 Integer :
运行结果:
com.pinyougou.manager.controller.BrandController.findPage(BrandController.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
这个原因是因为前端传的数据是(page,size)和后台的findPage中的(page,rows)不匹配,所以报空指针异常。