在 Spring Data 存储库中自定义 DELETE 方法 [英] Customize DELETE method in Spring Data repository

查看:63
本文介绍了在 Spring Data 存储库中自定义 DELETE 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某些日志目的,我使用 AspectJ 来记录 CRUD 操作,对于删除操作,我仅支持 repository.delete(object) 所以 repository.delete(id) 不受支持,但在 Spring Data 存储库中使用 http DELETE 调用时,我拦截了 repository.findOne() 然后 repository.delete(id) 调用.

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.

我的问题

我如何在 Spring Data 存储库中自定义 Http DELETE 方法以调用 repository.delete(object) 而不是 repository.delete(id).

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

这里是存储库界面:

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 代码:

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

}

我正在管理 objs 是实体对象的情况,所以我所有的删除操作都使用 delete(entity_object),而不是 delete(id),我正在寻找一种方法来修改调用 http DELETE 'ressource_url/id' 的函数调用.

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.

提前致谢

推荐答案

您是否尝试过在 delete(id) 方法上使用 @RestResource(exported = false) ?

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

我刚刚创建了一个简单的项目,它似乎可以工作.

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

这里是我项目中repository类的代码

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

这篇关于在 Spring Data 存储库中自定义 DELETE 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆