简体   繁体   中英

Why does deleteBy methods in spring data work only with GET requests

I'm using Spring Data Rest and I have an Entity employee

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;}

and trying to delete by firstName by extending JpaRepository as follows.

@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);

}

The code works only if I identified the method type in postman as GET, If selected method type as DELETE I get 404 NOT FOUND.

I dont know whether it is the default spring data behavior or I'm missing something.

Adding that the URL is available under the search resource "http://localhost:8888/employees/search/deleteByFirstName%7B?firstName}"

You get a 404 NOT FOUND because Spring Data Rest does not expose your custom delete Method. As you can see in the documentation , Spring only exposes the following three methods on a resource basis:

  • delete(T)
  • delete(ID)
  • delete(Iterable)

As far as I know it is not possible to expose custom delete methods, at least I was not capable of achieving it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM