Customize DELETE method in Spring Data repository

2019-06-09 14:55发布

For some logging purpose, i'm using AspectJ to log CRUD operations, for the delete operation i'm supporting only repository.delete(object) so repository.delete(id) is not supported, but while using http DELETE call in Spring Data repository, i intercept repository.findOne() then repository.delete(id) calls.

My Question

How i could customize Http DELETE method in Spring Data repository to call repository.delete(object) not repository.delete(id).

here's repository interface:

package com.geopro.repositories;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import com.geopro.entities.Product;

@RepositoryRestResource(collectionResourceRel = "product", path = "product")
public interface ProductRepository extends PagingAndSortingRepository<Product, Long> {

}

AspectJ code :

@Pointcut("execution(public * org.springframework.data.repository.Repository+.*(..))")
public void publicNonVoidRepositoryMethod() {
}

@Around("publicNonVoidRepositoryMethod()")
public Object publicNonVoidRepositoryMethod(ProceedingJoinPoint pjp) throws Throwable {

    if (pjp.getArgs()[0].getClass().getName() == "java.util.Arrays$ArrayList"  || pjp.getArgs()[0].getClass().getName() == "java.util.LinkedList") {
        Iterable arr = (Iterable) pjp.getArgs()[0];
        return saveHistoriqueOperation2(pjp, arr);
    } else {
        Object objs = pjp.getArgs()[0];
        if (objs.getClass().getName() == "com.geopro.entities.HistOperation") {
            Object o = pjp.proceed();
            return o;
        }
        return saveHistoriqueOperation(pjp, objs);
    }

}

i'm managing cases where objs is an entity object, so all my delete operations are using delete(entity_object), not delete(id), i'm looking for a manner to modify function calls where http DELETE 'ressource_url/id' gets called.

Thanks in advance

1条回答
爷的心禁止访问
2楼-- · 2019-06-09 15:41

Have you tried with @RestResource(exported = false) on the delete(id) method?

I just created a simple project and it seems to work.

Here is the code of the repository class in my project

public interface ProductRepository extends PagingAndSortingRepository<Product, Long> {
    @RestResource(exported = false)
    @Override
    void delete(Long var1);

    @Override
    void delete(Product var1);
}
查看更多
登录 后发表回答