3

I try to delete a user by getting id in url with an error:

Failed to convert value of type 'java.lang.String' to required type 'int'; 
nested exception is java.lang.NumberFormatException: For input string:

I change int id to String id, but then deleteMyUser() will not work because it accepts an integer.

Code:

<a href="/delete-user?id=${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";

    }
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
K.Nehe
  • 424
  • 10
  • 22
  • Check which is the value of id parameter in the html page (by viewing the source of the html page) it might be that in the url of the `a` tag the id is not a number –  Dec 19 '18 at 13:42
  • The value is valid – K.Nehe Dec 19 '18 at 13:53
  • then keep the `a` tag like this `x` and change `@RequestMapping("/delete-user{id}")` to `@RequestMapping("/delete-user")` and `@PathVariable("id")` to `@RequestParam("id")` –  Dec 19 '18 at 13:56

4 Answers4

1

You should add the id to path, so remove ?id=:

<a href="/delete-user${user.id}">x</a>
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
1

The problem is you are confusing between "query parameter" and "path variable"

<a href="/delete-user?id=${user.id}">x</a> // Passing value as query param


@RequestMapping("/delete-user{id}") // Expecting Path variable

To Fix this, either change both to query param or path variable (here I changed to path variable):

<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";

    }
1

Let me explain you some urls and there mapping

First /user/{id}/{userId} this is path variable format /user?id=1&userid=2 this is requestparam/query param format.

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){   

}

** in case of pathvariable your variable are part of mapping

POST request

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){   

}

if you are using @RequestMapping then its recommanded to define method also by default it map with get request.

@RequestMapping(method = [RequestMethod.GET])

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
jss
  • 199
  • 2
  • 13
0

What about this:

 RequestMapping("/delete-user/{id}")

Use a slash between delete-user and id and then call

<a href="/delete-user/${user.id}">x</a>

Also ensure that ${user.id} contains a valid number value

Otis Ottington
  • 425
  • 5
  • 17