1

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?

  • 1
    the http parameters are probbaly on the REST layer. repositories are the persistence layer. find what code calls this method... – radai Jul 31 '14 at 13:32
  • http://docs.spring.io/spring-data/rest/docs/2.1.1.RELEASE/reference/htmlsingle/#d5e112 – Stefan Jul 31 '14 at 14:14
  • I am using spring data rest to exposure the repository methods directly as REST interface –  Aug 04 '14 at 18:09

2 Answers2

3

Two issues here:

  1. The query method declaration is invalid as it needs to carry an @Modifying annotation to correctly execute the query.
  2. 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.
Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
  • I already assumed that I had to write a controller, but I had seen some examples before which override the save/delete method of a repository –  Aug 04 '14 at 18:08
  • Do you mind giving an example of how to implement controller methods in a repository (if I understood you correctly) –  Aug 04 '14 at 18:41
0
@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

Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
user3073234
  • 771
  • 5
  • 11
  • 24