猿问

MethodArgumentTypeMismatchException

我有一个基本的 SpringBoot 2.0.6.RELEASE 应用程序。使用 Spring Initializer、JPA、嵌入式 Tomcat、Thymeleaf 模板引擎,并打包为具有 Restful 架构的可执行 JAR


@PutMapping(path = "/users/alarms2", consumes = "application/json", produces = "application/json")

    public void setAlerts2(@RequestHeader(value = "Authorization") String authHeader,

            @RequestBody AlertConfiguration alertConfiguration)


            throws DataAccessException, ClientProtocolException, SQLException, IOException {

..

}

但是当我从 curl 调用这个方法时:


curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJyaWNhcmQub2SAsZUBnbWFpbC5jb20iLCJleHAiOjE2MDAxODMzNDAsImlhdCI6MTUzOTcwMzM0MH0.2gbyyGnkcoHjOw7HbUBBQgb59Bw8iAyFbqTe2DPUlOA-V5UwrW3KXWHZlXssZni8oRJ_o1QRzAFtAWMfz7f0Yw" -d ‘{“symbol": “MENU”,  "alarmKey”:”VEGAN” , "enabled": "true"}' "http://95.90.225.68:1133/restos/api/v1/users/alarms2"

我在服务器中收到此错误:


2018-10-17 17:16  [http-nio-1133-exec-9] WARN  o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver.resolveException(140) - Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'long'; nested exception is java.lang.NumberFormatException: For input string: "alarms2"]




红颜莎娜
浏览 633回答 1
1回答

LEATH

您可能有另一种具有更通用签名的方法,该方法与给定的签名重叠。就像是:@PutMapping(path = "/users/{userId}", ...)public void someMethod(@PathVariable(value = "userId") Long userId) {...}因此,当您使用/api/v1/users/alarms2Spring 时,首先尝试将“alarms2”(显然不是有效的Long)转换为userId(是Long)
随时随地看视频慕课网APP

相关分类

Java
我要回答