[英]Why does deleteBy methods in spring data work only with GET requests
我正在使用 Spring 數據 Rest 我有一個實體員工
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "first_Name")
@com.luv2code.springboot.demo.BootDataRest2.Validators.Employee
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
@NotBlank(message = "Email is mandatory")
@Email()
private String email;}
並嘗試通過擴展 JpaRepository 按 firstName 刪除,如下所示。
@RepositoryRestResource( collectionResourceRel = "employees")
public interface EmployeeRepository extends JpaRepository\<Employee,Integer\> {
@Modifying
@Transactional
public int deleteByFirstName(String firstName);
@Query(value = "SELECT e FROM Employee e where e.firstName like %?1% or e.lastName like %?1%")
public List<Employee> searchWithAny(String searchText);
}
僅當我將 postman 中的方法類型標識為 GET 時,該代碼才有效,如果選擇的方法類型為 DELETE,我得到 404 NOT FOUND。
我不知道這是默認的 spring 數據行為還是我遺漏了什么。
添加 URL 在搜索資源“http://localhost:8888/employees/search/deleteByFirstName%7B?firstName}”下可用
你得到一個404 NOT FOUND
因為 Spring 數據 Rest 沒有公開你的自定義刪除方法。 在文檔中可以看到,Spring在資源基礎上只暴露了以下三個方法:
delete(T)
delete(ID)
delete(Iterable)
據我所知,不可能公開自定義刪除方法,至少我無法實現它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.