spring aop 翻译bean中属性的数据字典

本文介绍了如何使用Hutool和Spring框架,结合AspectJ和CglibUtil,实现对Controller接口返回结果中数据字典的自动处理,包括切点注解、属性注解和字典翻译功能。
摘要由CSDN通过智能技术生成

糊涂包


<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.26</version>
</dependency>

切点注解

/**
 * aop数据字典拦截
 * Author:daWang
 * Date:2024/3/4  16:24
 */
public @interface DicAop {
}

属性注解

/**
 * Author:daWang
 * Date:2024/3/4  16:32
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DicData {
    //数据字典分类
    String dicType() default "";
}

翻译类



import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crmzf.cloud.api.annotation.DicData;
import com.crmzf.cloud.common.util.Result;
import com.crmzf.cloud.system.entity.SysDictDataEntity;
import com.crmzf.cloud.system.util.DictUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 数据字典统一处理
 * Author:daWang
 * Date:2024/3/4  16:20
 */
@Slf4j
@Aspect
@Component
public class DicAopAspect {

    @Pointcut("@annotation(com.crmzf.cloud.api.aspectj.DicAop)")
    public void pointCut() {
    }


    @AfterReturning(pointcut = "pointCut()", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, Object jsonResult) {
        if (jsonResult instanceof Result) {
            Result result = (Result) jsonResult;
            Object data = result.getData();
            if (data instanceof Page) {
                Page page = (Page) data;
                List records = page.getRecords();
                if (CollUtil.isNotEmpty(records)) {
                    Object o = records.stream().findFirst().get();
                    Field[] declaredFields = ClassUtil.getDeclaredFields(o.getClass());
                    List<Field> collect = Arrays.stream(declaredFields).filter(item -> {
                        DicData annotation = item.getAnnotation(DicData.class);
                        return Objects.nonNull(annotation) && StringUtils.isNotBlank(annotation.dicType()) ? true : false;
                    }).collect(Collectors.toList());
                    if (CollUtil.isNotEmpty(collect)) {
                        //准备数据字典
                        HashMap<Field, List<SysDictDataEntity>> map = new HashMap<>();
                        for (Field field : collect) {
                            List<SysDictDataEntity> dictCache = DictUtils.getDictCache(field.getAnnotation(DicData.class).dicType());
                            map.put(field, dictCache);
                        }
                        //遍历对象封装字典翻译
                        Object transBean = records.stream().map(item -> {
                            Map<String, Object> bean = BeanUtil.beanToMap(item);
                            for (Map.Entry<Field, List<SysDictDataEntity>> fieldListEntry : map.entrySet()) {
                                Field key = fieldListEntry.getKey();
                                String dicType = key.getAnnotation(DicData.class).dicType();
                                List<SysDictDataEntity> value = fieldListEntry.getValue();
                                String name = key.getName();
                                Object o1 = "";//字典值
                                try {
                                    Field field = ReflectUtil.getField(item.getClass(), name);
                                    field.setAccessible(true);
                                    o1 = field.get(item);
                                } catch (IllegalAccessException e) {
                                    e.printStackTrace();
                                }
                                for (SysDictDataEntity entity : value) {
                                    String dictLabel = entity.getDictLabel();
                                    String dictValue = entity.getDictValue();
                                    if (o1.toString().equals(dictValue)) {
                                        String label = name + "DictLabel";
                                        bean.put(label, dictLabel);
                                        log.debug("字典分类:{},字典值:{},字典翻译:{},", dicType, dictValue, label);
                                    }
                                }
                            }

                            return bean;
                        }).collect(Collectors.toList());
                        page.setRecords((List) transBean);

                    }
                }
            }
        }
    }
}


Bean
在这里插入图片描述

Controlle
在这里插入图片描述
接口返回
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值