简体   繁体   English

使用 jackson REST web 服务(RESTful Z38008DD81C2F4D7985ECF6839D47D60BC4555EE00C3FZ web 服务(RESTful Z38008DD81C2F4D7985ECF)@EV0ZCE8

[英]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)
  • Try passing right empId in the request http://localhost:8080/api/employeeList/123 and it should work尝试在请求http://localhost:8080/api/employeeList/123中传递正确的empId,它应该可以工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM