java 实现自定义注解实现字典翻译

  1. 1、定义接口

    /** * 数据字典注解 */
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Dict {

    /**
     * 字典类型
     *
     * @return
     */
    String dictCode();
    
    /**
     * 返回属性名
     *
     * @return
     */
    String dictText() default ""; }
    

    2、定义切面 /** * 数据字典切面 */
    @Aspect @Component @Slf4j public class
    DictAspect {

    /**
     * 字典后缀
     */
    private static String DICT_TEXT_SUFFIX = "Text";
    
    @Autowired
    private ISysDictTypeService dictTypeService;
    
    /**
     * 切点,切入 controller 包下面的所有方法
     */
    @Pointcut("execution( * com.ruoyi.*.controller.huandian.*.*(..)) || execution( * com.ruoyi.*.controller.*.*(..)) ")
    public void dict() {
    
    }
    
    @Around("dict()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long time1 = System.currentTimeMillis();
        Object result = pjp.proceed();
        long time2 = System.currentTimeMillis();
        //  log.debug("获取JSON数据 耗时:" + (time2 - time1) + "ms");
        long start = System.currentTimeMillis();
        this.parseDictText(result);
        long end = System.currentTimeMillis();
        //   log.debug("解析注入JSON数据  耗时" + (end - start) + "ms");
        return result;
    }
    
    private void parseDictText(Object result) {
        List<JSONObject> items = new ArrayList<>();
        if (result instanceof AjaxResult) {
            AjaxResult rr = (AjaxResult) result;
            if (rr.getData() instanceof String) {
                //字符串直接返回
                rr.setData(rr.getData());
            } else {
                rr.setData(getDataModel(rr.getData()));
            }
        } else if (result instanceof TableDataInfo) {
            TableDataInfo rr = (TableDataInfo) result;
            List<?> list = (List<?>) rr.getRows();
            getData(items, list);
            rr.setRows(items);
        } else if (result instanceof PageDataInfo) {
            PageDataInfo rr = (PageDataInfo) result;
            List<?> list = (List<?>) rr.getData();
            getData(items, list);
            rr.setData(items);
        }
    }
    
    
    private void getData(List<JSONObject> items, List<?> listData) {
        List<?> list = listData;
        for (Object record : list) {
            ObjectMapper mapper = new ObjectMapper();
            String json = "{}";
            try {
                json = mapper.writeValueAsString(record);
            } catch (JsonProcessingException e) {
                log.error("Json解析失败:" + e);
            }
            JSONObject item = JSONObject.parseObject(json);
            for (Field field : ObjConvertUtils.getAllFields(record)) {
                if (field.getAnnotation(Dict.class) != null) {
                    // 拿到注解的dictDataSource属性的值
                    String dictType = field.getAnnotation(Dict.class).dictCode();
                    // 拿到注解的dictText属性的值
                    String text = field.getAnnotation(Dict.class).dictText();
                    //获取当前带翻译的值
                    String key = String.valueOf(item.get(field.getName()));
                    //翻译字典值对应的text值
                    String textValue = translateDictValue(dictType, key);
                    //如果给了文本名
                    if (!StringUtils.isBlank(text)) {
                        item.put(text, textValue);
                    } else {
                        // 走默认策略
                        item.put(field.getName() + DICT_TEXT_SUFFIX, textValue);
                    }
                }
                // date类型默认转换string格式化日期
                if ("java.util.Date".equals(field.getType().getName())
                        && field.getAnnotation(JsonFormat.class) == null
                        && item.get(field.getName()) != null) {
                    SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
                }
            }
            items.add(item);
        }
    }
    
    
    private JSONObject getDataModel(Object data) {
        ObjectMapper mapper = new ObjectMapper();
        String json = "{}";
        try {
            json = mapper.writeValueAsString(data);
        } catch (JsonProcessingException e) {
            log.error("Json解析失败:" + e);
        }
        JSONObject item = JSONObject.parseObject(json);
        for (Field field : ObjConvertUtils.getAllFields(data)) {
            if (field.getAnnotation(Dict.class) != null) {
                String dictType = field.getAnnotation(Dict.class).dictCode();
                String text = field.getAnnotation(Dict.class).dictText();
                String key = String.valueOf(item.get(field.getName()));
                String textValue = translateDictValue(dictType, key);
                if (!StringUtils.isBlank(text)) {
                    item.put(text, textValue);
                } else {
                    // 走默认策略
                    item.put(field.getName() + DICT_TEXT_SUFFIX, textValue);
                }
            }
            if ("java.util.Date".equals(field.getType().getName())
                    && field.getAnnotation(JsonFormat.class) == null
                    && item.get(field.getName()) != null) {
                SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
            }
        }
        return item;
    
    }
    
    /**
     * 翻译字典文本
     *
     * @param dictType
     * @param key
     * @return
     */
    private String translateDictValue(String dictType, String key) {
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        StringBuffer textValue = new StringBuffer();
        String[] keys = key.split(",");
        for (String k : keys) {
            if (k.trim().length() == 0) {
                continue;
            }
            String dict_label = "";
            List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
            if (null == data) {
                return dict_label;
            }
            for (SysDictData sysDictData : data) {
                if (sysDictData.getDictValue().equals(key)) {
                    dict_label = sysDictData.getDictLabel();
                }
            }
            textValue.append(dict_label);
            //log.info("数据字典翻译: 字典类型:{},当前翻译值:{},翻译结果:{}", dictType, k.trim(), dict_label);
        }
        return textValue.toString();
    } }
    
    3、工具类 /**  * 对象转换工具类  */ @SuppressWarnings("ALL") public class ObjConvertUtils {
    
    /**
     * 获取类的所有属性,包括父类
     *
     * @param object
     * @return
     */
    public static Field[] getAllFields(Object object) {
        Class<?> clazz = object.getClass();
        List<Field> fieldList = new ArrayList<>();
        while (clazz != null) {
            fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
            clazz = clazz.getSuperclass();
        }
        Field[] fields = new Field[fieldList.size()];
        fieldList.toArray(fields);
        return fields;
    }
    
    public static boolean isEmpty(Object object) {
        if (object == null) {
            return (true);
        }
        if ("".equals(object)) {
            return (true);
        }
        if ("null".equals(object)) {
            return (true);
        }
        return (false);
    } }
    

    4、返回模型 import java.io.Serializable;

    /** * 操作消息提醒 * * @author Lion Li */
    @Data @NoArgsConstructor
    @AllArgsConstructor
    @Accessors(chain = true)
    @ApiModel(“操作消息提醒”)
    public class AjaxResult implements Serializable {

    private static final long serialVersionUID = 7364644042368216173L;
    
    /**
     * 状态码
     */
    @ApiModelProperty("状态码")
    private int code;
    
    /**
     * 返回内容
     */
    @ApiModelProperty("返回内容")
    private String msg;
    
    /**
     * 数据对象
     */
    @ApiModelProperty("数据对象")
    private T data;
    
    /**
     * 初始化一个新创建的 AjaxResult 对象
     *
     * @param code 状态码
     * @param msg  返回内容
     */
    public AjaxResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    
    /**
     * 返回成功消息
     *
     * @return 成功消息
     */
    public static AjaxResult<Void> success() {
        return AjaxResult.success("操作成功");
    }
    
    /**
     * 返回成功数据
     *
     * @return 成功消息
     */
    public static <T> AjaxResult<T> success(T data) {
        return AjaxResult.success("操作成功", data);
    }
    
    /**
     * 返回成功消息
     *
     * @param msg 返回内容
     * @return 成功消息
     */
    public static AjaxResult<Void> success(String msg) {
        return AjaxResult.success(msg, null);
    }
    
    /**
     * 返回成功消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static <T> AjaxResult<T> success(String msg, T data) {
        return new AjaxResult<>(HttpStatus.HTTP_OK, msg, data);
    }
    
    /**
     * 返回错误消息
     *
     * @return
     */
    public static AjaxResult<Void> error() {
        return AjaxResult.error("操作失败");
    }
    
    /**
     * 返回错误消息
     *
     * @param msg 返回内容
     * @return 警告消息
     */
    public static AjaxResult<Void> error(String msg) {
        return AjaxResult.error(msg, null);
    }
    
    /**
     * 返回错误消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static <T> AjaxResult<T> error(String msg, T data) {
        return new AjaxResult<>(HttpStatus.HTTP_INTERNAL_ERROR, msg, data);
    }
    
    /**
     * 返回错误消息
     *
     * @param code 状态码
     * @param msg  返回内容
     * @return 警告消息
     */
    public static AjaxResult<Void> error(int code, String msg) {
        return new AjaxResult<>(code, msg, null);
    }
    

    }

    5、使用 添加注解即可
    @ApiModelProperty(value = “状态 0-使用中 1-已归还”)
    @Dict(dictCode = “orderStatus”)
    private Integer status; ```请添加图片描述
    请添加图片描述

用到的类

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.List;

/**
 * 表格分页数据对象
 *
 * @author Lion Li
 */

@Data
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel("分页对象")
public class TableDataInfo<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 总记录数
     */
    @ApiModelProperty("总记录数")
    private long total;

    /**
     * 列表数据
     */
    @ApiModelProperty("列表数据")
    private List<T> rows;

    /**
     * 消息状态码
     */
    @ApiModelProperty("消息状态码")
    private int code;

    /**
     * 消息内容
     */
    @ApiModelProperty("消息内容")
    private String msg;

    /**
     * 分页
     *
     * @param list  列表数据
     * @param total 总记录数
     */
    public TableDataInfo(List<T> list, long total) {
        this.rows = list;
        this.total = total;
    }
    public TableDataInfo(List<T> list, long total,String msg,int code) {
        this.rows = list;
        this.total = total;
        this.msg = msg;
        this.code = code;
    }
}
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.List;

/**
 * 表格分页数据对象
 *
 * @author Lion Li
 */

@Data
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel("分页响应对象")
public class PageDataInfo<T> implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * 总记录数
	 */
	@ApiModelProperty("总记录数")
	private long total;

	/**
	 * 列表数据
	 */
	@ApiModelProperty("列表数据")
	private List<T> data;

	/**
	 * 消息状态码
	 */
	@ApiModelProperty("消息状态码")
	private int code;

	/**
	 * 消息内容
	 */
	@ApiModelProperty("消息内容")
	private String msg;

	/**
	 * 分页
	 *
	 * @param list  列表数据
	 * @param total 总记录数
	 */
	public PageDataInfo(List<T> list, long total) {
		this.data = list;
		this.total = total;
	}

}
import cn.hutool.http.HttpStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 操作消息提醒
 *
 * @author Lion Li
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel("操作消息提醒")
public class AjaxResult<T> implements Serializable {

    private static final long serialVersionUID = 7364644042368216173L;

    /**
     * 状态码
     */
    @ApiModelProperty("状态码")
    private int code;

    /**
     * 返回内容
     */
    @ApiModelProperty("返回内容")
    private String msg;

    /**
     * 数据对象
     */
    @ApiModelProperty("数据对象")
    private T data;

    /**
     * 初始化一个新创建的 AjaxResult 对象
     *
     * @param code 状态码
     * @param msg  返回内容
     */
    public AjaxResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 返回成功消息
     *
     * @return 成功消息
     */
    public static AjaxResult<Void> success() {
        return AjaxResult.success("操作成功");
    }

    /**
     * 返回成功数据
     *
     * @return 成功消息
     */
    public static <T> AjaxResult<T> success(T data) {
        return AjaxResult.success("操作成功", data);
    }

    /**
     * 返回成功消息
     *
     * @param msg 返回内容
     * @return 成功消息
     */
    public static AjaxResult<Void> success(String msg) {
        return AjaxResult.success(msg, null);
    }

    /**
     * 返回成功消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static <T> AjaxResult<T> success(String msg, T data) {
        return new AjaxResult<>(HttpStatus.HTTP_OK, msg, data);
    }

    /**
     * 返回错误消息
     *
     * @return
     */
    public static AjaxResult<Void> error() {
        return AjaxResult.error("操作失败");
    }

    /**
     * 返回错误消息
     *
     * @param msg 返回内容
     * @return 警告消息
     */
    public static AjaxResult<Void> error(String msg) {
        return AjaxResult.error(msg, null);
    }

    /**
     * 返回错误消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static <T> AjaxResult<T> error(String msg, T data) {
        return new AjaxResult<>(HttpStatus.HTTP_INTERNAL_ERROR, msg, data);
    }

    /**
     * 返回错误消息
     *
     * @param code 状态码
     * @param msg  返回内容
     * @return 警告消息
     */
    public static AjaxResult<Void> error(int code, String msg) {
        return new AjaxResult<>(code, msg, null);
    }


}
根据提供的引用内容,我们可以看出这是一个Java Web项目,其中包含了自定义注解和拦截器的使用。在Java中,我们可以通过自定义注解和拦截器来实现验签功能。具体步骤如下: 1.定义自定义注解@Sign,用于标记需要验签的方法或类。 2.定义拦截器,在拦截器中获取请求参数和签名,并进行验签操作。 下面是一个简单的Java验签示例: ```java // 自定义注解 @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Sign { } // 拦截器 @Component public class SignInterceptor implements HandlerInterceptor { @Autowired private SignService signService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 判断是否有@Sign注解 if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; Sign sign = handlerMethod.getMethodAnnotation(Sign.class); if (sign == null) { sign = handlerMethod.getBeanType().getAnnotation(Sign.class); } if (sign != null) { // 获取请求参数和签名 Map<String, String[]> parameterMap = request.getParameterMap(); String signValue = request.getHeader("sign"); // 验签 boolean verifyResult = signService.verifySign(parameterMap, signValue); if (!verifyResult) { // 验签失败,返回错误信息 response.setContentType("application/json;charset=UTF-8"); response.getWriter().write("{\"code\":400,\"msg\":\"验签失败\"}"); return false; } } } return true; } } // 验签服务 @Service public class SignService { // 验签密钥 private static final String SIGN_KEY = "123456"; /** * 验签 * * @param parameterMap 请求参数 * @param signValue 签名 * @return 验签结果 */ public boolean verifySign(Map<String, String[]> parameterMap, String signValue) { // 将请求参数按照字典序排序并拼接成字符串 StringBuilder sb = new StringBuilder(); parameterMap.keySet().stream().sorted().forEach(key -> { String[] values = parameterMap.get(key); Arrays.sort(values); sb.append(key).append("=").append(values[0]).append("&"); }); sb.append("key=").append(SIGN_KEY); // 计算签名 String sign = DigestUtils.md5Hex(sb.toString()); // 验证签名 return StringUtils.equals(sign, signValue); } } // 控制器 @RestController public class TestController { @Autowired private SignService signService; @PostMapping("/test") @Sign public ResponseBody<AuthCodeJson> getAuthCode(@CurrentUser UserInfo userInfo) { System.out.println(userInfo.getId()); System.out.println(ThreadContextHolder.getUserInfo().getUserId()); return this.success(); } } ``` 在上面的示例中,我们定义了一个自定义注解@Sign,用于标记需要验签的方法或类。然后定义了一个拦截器SignInterceptor,在拦截器中获取请求参数和签名,并进行验签操作。最后,在控制器TestController中使用@Sign注解标记了需要验签的方法getAuthCode。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值