update methods in spring data rest repositories
-
21-12-2019 - |
Pregunta
I am using Spring data rest and I want to add some additional functionality to my repository.
@RestResource(exported = true)
public class ItemRepository extends JpaRepository<Item, Long> {
@Query("update ...")
void modifyItem();
}
The modifyItem()
method can only be accessed through a GET request at /items/find/modifyItem
How can I change the requestmethod to UPDATE?
How can I remove the "find" from the URI?
Solución
Two issues here:
- The query method declaration is invalid as it needs to carry an
@Modifying
annotation to correctly execute the query. - The method is not exported by Spring Data REST as it's generally hard to reason about the correct HTTP method to use for the exposure. The current way of exposing the method is to plug in a manually implemented controller that gets the repository injected and manually invokes the method.
Otros consejos
@Query(Update (Table name) SET (schema you want to update) = :variable WHERE ID=1)
public void update(@Param("variable") String variable);
I think that should work
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow