Max
Max

Reputation: 774

Specify DELETE HTTP method for custom repository method

I need to add and expose the custom delete method to the spring data rest repository. How is it possible to specify DELETE method, because by default spring-data-rest exposes it like GET.

@RepositoryRestResource(path = "documents")
public interface DocumentRepository extends JpaRepository<DocumentEntity, Long> {

  void deleteByDate(@Param("date") LocalDate date);
}

Upvotes: 1

Views: 249

Answers (1)

Alan Hay
Alan Hay

Reputation: 23226

The method you have added will be considered a 'Search' resource (as can be seen from the exposed path /documents/search/deleteByDate?...) and as the documentation notes:

5.5.1. Supported HTTP Methods As the search resource is a read-only resource, it supports only the GET method.

https://docs.spring.io/spring-data/rest/docs/3.1.9.RELEASE/reference/html/#repository-resources.search-resource

Solution then is to create a standard Spring MVC controller to handle this custom action:

@DeleteMapping("/documents/deleteByDate")
public ResponseEntity<> deleteDocumentByDate(@RequestParam("date") LocalDate date){
    ....
}

Upvotes: 1

Related Questions