Delete user by ID

0

I have the following link

<div class="col-md-8">
    <a href="borrarUsuario/${session.userData.id}">Borrar Usuario</a>
</div>

This is the controller method that clears

@RequestMapping(value="/borrarUsuario/{id}",method = RequestMethod.POST)  
     public String borrarUsuario(@RequestParam("id") Long id){  
         repoUsuario.delete(id); 
         return "views/_t/main";
}

Class UsuarioRepository extends from JpaRepository

What I can not do is delete the user by id .

SOLUTION:

The problem was that I did not take the value of id , so I looked for it in this way if I took it without having to parsed the variable.

            <div class="col-md-8">
                <a th:href="@{'/borrarUsuario/' + ${session.userData.id}}">Borrar Usuario</a>
            </div>
    
asked by Stewie 21.11.2018 в 09:00
source

2 answers

1

You are waiting for a parameter in the request (you have the annotation @RequestParam ), but you are actually passing it as part of the URL, so you should be marking the parameter of your method as a @PathVariable :

@PostMapping(value="/borrarUsuario/{id}")  
public String borrarUsuario(@PathVariable("id") Long id){  
     repoUsuario.delete(id); 
     return "views/_t/main";
}
    
answered by 21.11.2018 в 09:38
0

You have to think that what arrives to you in the url in the Path variable is a String, and you need the value in Long to pass it to your JPA query therefore you must pick it up as a String and pass it paired to Long. Try this and it should work:

@PostMapping(value="/borrarUsuario/{id}")  
public String borrarUsuario(@PathVariable("id") String id){  
     repoUsuario.delete(Long.parseLong(id)); 
     return "views/_t/main";

}
    
answered by 21.11.2018 в 12:03