静态网站 服务器,上海专业网站建站,东莞网推广网站建设,手机开发游戏又是想下班的一天 文章目录 前言springboot使用AOP简单集成字典注解 一、通过注解进行字典赋值二、使用步骤1.添加注解2.序列化处理类3.实体类添加注解4.json返回值 总结 前言
springboot使用AOP简单集成字典注解
一、通过注解进行字典赋值
使用AOP切面实现字典的赋值,自动将… 又是想下班的一天 文章目录 前言springboot使用AOP简单集成字典注解 一、通过注解进行字典赋值二、使用步骤1.添加注解2.序列化处理类3.实体类添加注解4.json返回值 总结 前言
springboot使用AOP简单集成字典注解
一、通过注解进行字典赋值
使用AOP切面实现字典的赋值,自动将code转换为name减少工作(偷懒)
二、使用步骤
1.添加注解
代码如下示例
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.springblade.modules.system.serialize.DictBizOneSerialize;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** author hua* description*/Target({ElementType.FIELD})
Retention(RetentionPolicy.RUNTIME)
JacksonAnnotationsInside
JsonSerialize(using DictBizOneSerialize.class)
public interface DictBizOne {String type();
}2.序列化处理类
代码如下示例
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.cache.DictBizCache;
import org.springblade.modules.system.annotation.DictBizOne;import java.io.IOException;
import java.util.Objects;/*** author hua* description*/
Slf4j
public class DictBizOneSerialize extends StdSerializerObject implements ContextualSerializer {//字典注解private DictBizOne dict;public DictBizOneSerialize() {super(Object.class);}public DictBizOneSerialize(DictBizOne dict) {super(Object.class);this.dict dict;}private String type;Overridepublic void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {if (Objects.isNull(value)) {gen.writeObject(value);return;}if (Objects.nonNull(dict)){type dict.type();}// 通过数据字典类型和value获取name gen.writeObject(value);gen.writeFieldName(gen.getOutputContext().getCurrentName()Name);//字典赋值(不同项目的字典值获取会不一样个人根据实际情况实现)String label DictBizCache.getValue(type, String.valueOf(value));gen.writeObject(label);}Overridepublic JsonSerializer? createContextual(SerializerProvider prov, BeanProperty beanProperty) throws JsonMappingException {if (Objects.isNull(beanProperty)){return prov.findValueSerializer(beanProperty.getType(), beanProperty);}DictBizOne dict beanProperty.getAnnotation(DictBizOne.class);if (Objects.nonNull(dict)){type dict.type();return this;}return prov.findNullValueSerializer(null);}3.实体类添加注解
import java.io.Serializable;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;Data
public class SpecificJobPositionsVO implements Serializable {private static final long serialVersionUID 1L;ApiModelProperty(value id)private Long id;ApiModelProperty(value name)private String name;ApiModelProperty(value 等级)Dict(type USER_LEVEL) //type中添加字典的codeprivate String level;}
4.json返回值 {id: 1522492702905954306,name: 小庄,level: 1,levelName: 哦呜}, 总结
以上就是注解实现自动添加字典值