为自定义存储库方法指定 DELETE HTTP 方法

Specify DELETE HTTP method for custom repository method

我需要添加自定义删除方法并将其公开到 spring 数据存储库。 如何指定 DELETE 方法,因为默认情况下 spring-data-rest 像 GET 一样公开它。

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

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

您添加的方法将被视为 'Search' 资源(从暴露的路径中可以看出 /documents/search/deleteByDate?...)并且如文档所述:

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

解决方案是创建一个标准 Spring MVC 控制器来处理此自定义操作:

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