工作中遇到springmvc架構定義的方法中使用了【@RequestBody】 這個注解
代碼如下:
@RequestMapping(value = "create", method = RequestMethod.POST)
@ResponseBody
public ResultData createOrder(@RequestBody PrintOrders printOrders, HttpServletRequest request) throws JsonGenerationException, JsonMappingException, IOException {
Integer productId = printOrders.getProductId();
}
調用接口的時候發生異常
2018-05-07 10:55:59.323 [qtp983814036-76] WARN o.s.w.s.m.s.DefaultHandlerExceptionResolver - Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "create"
明顯說的是轉換類型問題, 引起這個問題的原因是@RequestBody ,注解用于讀取http請求的内容并通過httpMessageConverter接口将内容綁定到controller的方法參數上,由于我沒傳printorders的參數,是以轉換類型失敗。