user3450918
user3450918

Reputation:

update methods in spring data rest repositories

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?

Upvotes: 1

Views: 1552

Answers (2)

user3073234
user3073234

Reputation: 771

@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

Upvotes: 0

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83081

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.

Upvotes: 3

Related Questions