创建字段注解TranslationField
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TranslationField {
String type() default "";
}
创建方法注解Translation
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Translation {
}
创建注解切面TransaletionAspect
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.cglib.CglibUtil;
import net.sf.cglib.beans.BeanGenerator;
import org.apache.commons.beanutils.PropertyUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
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.ArrayList;
import java.util.List;
@Aspect
@Order(1)
@Component
public class TransaletionAspect {
@Pointcut("@annotation(com.ruoyi.translation.Translation)")
public void TransaletionPointCut() {
}
@Around("TransaletionPointCut()")
public Object translateFields(ProceedingJoinPoint joinPoint) throws Throwable {
Object result = joinPoint.proceed();
Object resObj = null;
if (result instanceof List) {
List<?> entities = (List<?>) result;
List list = new ArrayList<>();
for (Object entity : entities) {
BeanGenerator generator = new BeanGenerator();
Field[] fields = entity.getClass().getDeclaredFields();
for (Field field : fields) {
generator.addProperty(field.getName(), field.getType());
if (field.isAnnotationPresent(TranslationField.class)) {
// 创建新的属性
generator.addProperty(field.getName() + "Translation", String.class);
}
}
Class<?> clazz = (Class<?>) generator.createClass();
Object object = clazz.newInstance();
for (Field field : fields) {
setProperty(object, field.getName(), getValue(entity, field.getName()));
if (field.isAnnotationPresent(TranslationField.class)) {
// 获取所有翻译注解字段
TranslationField translation = field.getAnnotation(TranslationField.class);
// 获取转换原始数据
Object value = getValue(entity, field.getName());
// 获取字典编码
String type = translation.type();
// TODO 通过字典编码转换数据
// TODO 先用时间代替
String translationValue = DateUtil.now();
// 给新的属性赋值
setProperty(object, field.getName() + "Translation", translationValue);
}
}
list.add(object);
}
resObj = list;
}
return resObj;
}
private static void setProperty(Object object, String propertyName, Object value) {
try {
PropertyUtils.setProperty(object, propertyName, value);
} catch (Exception e) {
e.printStackTrace();
}
}
private static Object getProperty(Object object, String propertyName) {
try {
return PropertyUtils.getProperty(object, propertyName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 通过反射获取指定对象的指定屬性
*/
private static Object getValue(Object object, String fieldName) {
try {
Field field = object.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(object);
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException e) {
}
return null;
}
/*
* 通过反射设置指定对象的指定屬性
*/
public static <T> void setValue(T t, String fieldName, Object value) {
try {
Class<T> tClass = (Class<T>) t.getClass();
Field declaredField = tClass.getDeclaredField(fieldName);
declaredField.setAccessible(true);
declaredField.set(t, value);
} catch (NoSuchFieldException e) {
} catch (SecurityException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
}
}
需要引入pom,但是jdk17之后cglib 会报错需要在启动向中加入
--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/sun.net.util=ALL-UNNAMED
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
使用方法
创建实体类
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class User {
// 姓名
private String name;
// 性别
@TranslationField(type = "generalDict")
private String general;
}
创建测试Controller
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("test")
public class Test {
@Translation
@RequestMapping("users")
public List list(){
ArrayList<User> users = new ArrayList<>();
users.add(new User("张三","nan"));
users.add(new User("李四","nv"));
return users;
}
}