[英]Request not processing using jackson REST web services (RESTful Spring Controllers) @PathVariable
I got this error when I was working with Jackson REST web services (RESTful Spring Controllers) @PathVariable.当我使用 Jackson REST web 服务(RESTful Z38008ControllerECF81C2F4AFD8DE1Z85)时出现此错误
@Controller
@RequestMapping("/api")
public class EmployeeRestControllerImpl{
@GetMapping("/employeeList/{empId}")
public @ResponseBody
ResponseMessage getEmployeeListById(@PathVariable int empId){
.....
System.out.println(employees.get(empId));
.....
}
}
employees: [{"firstName":"ABC","lastName":"ABC"},{"firstName":"XYZ","lastName":"XYZ"},{"firstName":"DEF","lastName":"DEF"}]
员工:
[{"firstName":"ABC","lastName":"ABC"},{"firstName":"XYZ","lastName":"XYZ"},{"firstName":"DEF","lastName":"DEF"}]
Exception:例外:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Optional int parameter 'empId' 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.
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Is it necessary to change the primitive type to wrapper object.. why?是否有必要将原始类型更改为包装器 object.. 为什么? Help me to figure out.Thanks in advance..
帮我弄清楚。提前谢谢..
The error is very specific错误非常具体
org.springframework.web.util.NestedServletException: Request processing failed;
org.springframework.web.util.NestedServletException:请求处理失败; nested exception is java.lang.IllegalStateException: Optional int parameter 'empId' is present but cannot be translated into a null value due to being declared as a primitive type.
嵌套异常是 java.lang.IllegalStateException:存在可选的 int 参数“empId”,但由于被声明为原始类型,因此无法转换为 null 值。 Consider declaring it as object wrapper for the corresponding primitive type.
考虑将其声明为相应原始类型的 object 包装器。
Seems you are requesting the API without any empId
hence the null value for path variable.似乎您正在请求没有任何
empId
的 API 因此路径变量的 null 值。 The error message says that if you want to support null then instead of primitive int
variable user wrapper objects like Integer
错误消息说,如果你想支持 null ,而不是像
Integer
这样的原始int
变量用户包装对象
as作为
@GetMapping("/employeeList/{empId}")
public @ResponseBody ResponseMessage getEmployeeListById(@PathVariable Integer empId)
http://localhost:8080/api/employeeList/123
and it should workhttp://localhost:8080/api/employeeList/123
中传递正确的empId,它应该可以工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.