Java spring引导中的MethodArgumentTypeMismatchException
我试图通过在url中获取id来删除用户,但出现错误:Java spring引导中的MethodArgumentTypeMismatchException,java,spring,spring-boot,request-mapping,path-variables,Java,Spring,Spring Boot,Request Mapping,Path Variables,我试图通过在url中获取id来删除用户,但出现错误: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: 我将int-id更改为String-id,但是deleteMyUser()将不起作用,因为它接受整数 代码: @请求映射(“/delete user
Failed to convert value of type 'java.lang.String' to required type 'int';
nested exception is java.lang.NumberFormatException: For input string:
我将int-id更改为String-id,但是deleteMyUser()
将不起作用,因为它接受整数
代码:
@请求映射(“/delete user{id}”)
公共字符串deleteUser(@PathVariable(“id”)int-id,HttpServletRequest)
{
setAttribute(“mode”,“mode_HOME”);
userService.deleteMyUser(id);
返回“欢迎”;
}
这个怎么办:
RequestMapping("/delete-user/{id}")
在delete user和id之间使用斜杠,然后调用
<a href="/delete-user/${user.id}">x</a>
还要确保${user.id}包含有效的数值您应该将
id
添加到路径,以便删除?id=
:
<a href="/delete-user${user.id}">x</a>
问题在于您混淆了“查询参数”和“路径变量”
//将值作为查询参数传递
@RequestMapping(“/delete user{id}”)//应为路径变量
要解决此问题,请同时更改为query param或path variable(此处我更改为path variable):
@请求映射(“/delete user/{id}”)
公共字符串deleteUser(@PathVariable(“id”)int-id,HttpServletRequest)
{
setAttribute(“mode”,“mode_HOME”);
userService.deleteMyUser(id);
返回“欢迎”;
}
让我向您解释一些URL和映射
第一个/user/{id}/{userId}这是路径变量格式/user?id=1&userId=2这是requestparam/queryparam格式
https://domainname.com/find/user?id=1
@GetMapping("/find/user")
public String deleteUser(@RequestParam("id") int id){
}
https://domainname.com/find/user/1
@GetMapping("/find/user/{id}")
public String deleteUser(@Pathvariable("id") int id){
}
https://domainname.com/find/user/1/2
@GetMapping("/find/user/{id}/{userid}")
public String deleteUser(@Pathvariable("id") int id,@Pathvariable("userId")
int userId){
}
**对于pathvariable,您的变量是映射的一部分
后请求
https://domainname.com/find/user
in request body {"id":1}
@PostMapping("/find/user")
public String deleteUser(@RequestBody Integer id){
}
https://domainname.com/find/user/1?userId=2
@GetMapping("/find/user/{id}")
public String deleteUser(@Pathvariable("id") int id,@RequestParam("userId")
int userId){
}
如果您使用的是@RequestMapping
,那么建议您定义方法,默认情况下,它也会与get请求进行映射
@RequestMapping(method=[RequestMethod.GET])
检查html页面中id参数的值(通过查看html页面的源代码),可能是在a
标记的url中,id不是数字该值有效,然后像这样保留
标记并更改@RequestMapping>(“/delete user{id}”)
到@RequestMapping(“/delete user”)
和@PathVariable(“id”)
到@RequestParam(“id”)
如何添加多个变量?@K.ne他通常用斜线分隔它们,如/{id}/{otherId}”
不起作用问题在于我混淆了查询参数和路径变量您的权利-问题在于查询参数和路径变量混淆谢谢我理解
<a href="/delete-user/${user.id}">x</a>
@RequestMapping("/delete-user/{id}")
public String deleteUser(@PathVariable("id") int id,HttpServletRequest request)
{
request.setAttribute("mode","MODE_HOME");
userService.deleteMyUser(id);
return "welcome";
}
https://domainname.com/find/user?id=1
@GetMapping("/find/user")
public String deleteUser(@RequestParam("id") int id){
}
https://domainname.com/find/user/1
@GetMapping("/find/user/{id}")
public String deleteUser(@Pathvariable("id") int id){
}
https://domainname.com/find/user/1/2
@GetMapping("/find/user/{id}/{userid}")
public String deleteUser(@Pathvariable("id") int id,@Pathvariable("userId")
int userId){
}
https://domainname.com/find/user
in request body {"id":1}
@PostMapping("/find/user")
public String deleteUser(@RequestBody Integer id){
}
https://domainname.com/find/user/1?userId=2
@GetMapping("/find/user/{id}")
public String deleteUser(@Pathvariable("id") int id,@RequestParam("userId")
int userId){
}