簡體   English   中英

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

[英]Request not processing using 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));
                   .....
        }

    }

員工: [{"firstName":"ABC","lastName":"ABC"},{"firstName":"XYZ","lastName":"XYZ"},{"firstName":"DEF","lastName":"DEF"}]

例外:

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)

是否有必要將原始類型更改為包裝器 object.. 為什么? 幫我弄清楚。提前謝謝..

  • 錯誤非常具體

    org.springframework.web.util.NestedServletException:請求處理失敗; 嵌套異常是 java.lang.IllegalStateException:存在可選的 int 參數“empId”,但由於被聲明為原始類型,因此無法轉換為 null 值。 考慮將其聲明為相應原始類型的 object 包裝器。

    似乎您正在請求沒有任何empId的 API 因此路徑變量的 null 值。 錯誤消息說,如果你想支持 null ,而不是像Integer這樣的原始int變量用戶包裝對象

作為

@GetMapping("/employeeList/{empId}")
public @ResponseBody ResponseMessage getEmployeeListById(@PathVariable Integer empId)
  • 嘗試在請求http://localhost:8080/api/employeeList/123中傳遞正確的empId,它應該可以工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM