Customize DELETE method in Spring Data repository

一个人想着一个人 提交于 2019-12-08 09:35:38

问题


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:


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


来源:https://stackoverflow.com/questions/28945306/customize-delete-method-in-spring-data-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!