首頁 > 軟體

Java分析講解序列化與字典功能的序列化

2022-06-08 18:02:57

兩種解決方案

  • 前端查詢字典資料然後前端轉碼
  • 後端查詢字典值,然後再轉碼返回給前段。

本文及時針對方案2 進行的改進

目標:

在需要返回給前段的欄位上新增指定的註解例如:@DictDesc 則根據該欄位定義的值結合註解設定生成 xxxDesc欄位並自動賦值為註解屬性值所對應的字典描述;

具體使用的技術涉及到jackson序列化與反序列化,其他JSON工具包也型別的效果;

字典註解定義

/**
 * 字典型別欄位自動生成Desc描述欄位
 */
@Inherited
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@JacksonAnnotationsInside
@JsonSerialize(using = DictDescSerializer.class)
@JsonDeserialize(using = DictDescDeserializer.class)
public @interface DictDesc {
    /**
     * 列舉型別的class
     * 取值:getValue, getCode, getStatus, name
     * 描述:getDesc
     *
     * @return 字典型別
     */
    Class<? extends Enum<? extends DictEnum>>[] enumType() default {};
    /**
     * 字典型別分組
     *
     * @return 字典型別
     */
    String[] dictType() default {};
    /**
     * 字典轉換失敗時預設值
     *
     * @return String 預設值
     */
    String defaultValue() default "";
    /**
     * 是否丟擲異常,預設不丟擲異常,返回預設值
     *
     * @return true 轉換失敗則丟擲異常,false 異常返回預設值
     */
    boolean throwException() default false;
}

該註解中定義瞭解析該註解需要序列化器與返序列化器:

@JsonSerialize(using = DictDescSerializer.class)

@JsonDeserialize(using = DictDescDeserializer.class)

字典序列化與返序列化器的實現

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.aimilin.common.dict.annotation.DictDesc;
import com.aimilin.common.dict.service.impl.DictDescSerializerUtils;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Objects;
/**
 * 字典型別返序列化器
 *
 * @author liujunguang1
 * @version V1.0
 * @date 2022/5/20 21:08
 */
@Slf4j
@NoArgsConstructor
public class DictDescDeserializer extends JsonDeserializer<Object> implements ContextualDeserializer {
    /**
     * 生成序列化欄位字尾
     */
    private static final String LABEL_SUFFIX = "Desc";
    /**
     * 引數型別
     */
    private Class<?> rawClass;
    /**
     * 預設轉換器
     */
    private ConversionService converter;
    /**
     * 設定方法
     */
    private Method writeMethod;
    /**
     * 字典設定資訊
     */
    private DictDesc dict;
    public DictDescDeserializer(DictDesc dict, BeanProperty property) {
        this.dict = dict;
        this.rawClass = property.getType().getRawClass();
        this.converter = new DefaultConversionService();
        Class<?> targetClass = property.getMember().getDeclaringClass();
        String writeField = property.getName() + LABEL_SUFFIX;
        PropertyDescriptor propertyDescriptor = BeanUtils.getPropertyDescriptor(targetClass, writeField);
        this.writeMethod = Objects.isNull(propertyDescriptor) ? null : propertyDescriptor.getWriteMethod();
        if (Objects.isNull(this.writeMethod)) {
            log.info("類:{},字典屬性:{},沒有寫入方法:{},不設定值!", targetClass.getName(), property.getName(), writeField);
        }
    }
    @Override
    public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException {
        DictDesc dict = property.getAnnotation(DictDesc.class);
        if (dict != null) {
            return new DictDescDeserializer(dict, property);
        }
        return this;
    }
    @Override
    public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
        Object result = this.getValue(p.getText());
        this.setDictDesc(result, p.getCurrentName(), p.getCurrentValue());
        return result;
    }
    /**
     * 將資料型別轉換為目標型別
     *
     * @param value 字串值
     * @return 目標型別值
     * @throws IOException
     */
    public Object getValue(String value) throws IOException {
        return converter.convert(value, this.rawClass);
    }
    /**
     * 設定字典會限制
     *
     * @param result       字典value
     * @param currentName  當前屬性名稱
     * @param currentValue 當前物件
     */
    private void setDictDesc(Object result, String currentName, Object currentValue) {
        try {
            if (this.writeMethod != null) {
                writeMethod.invoke(currentValue, DictDescSerializerUtils.getDesc(this.dict, currentName, result));
            }
        } catch (Exception e) {
            log.error("類:{},字典屬性:{},回顯異常:{}", currentValue.getClass(), currentName, e.getMessage(), e);
        }
    }
}
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.aimilin.common.dict.annotation.DictDesc;
import com.aimilin.common.dict.service.impl.DictDescSerializerUtils;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
/**
 * 字典序列化器
 *
 * @author liujunguang1
 * @version V1.0
 * @date 2022/5/20 20:48
 */
@Slf4j
@NoArgsConstructor
public class DictDescSerializer extends JsonSerializer<Object> implements ContextualSerializer {
    /**
     * 生成序列化欄位字尾
     */
    private static final String LABEL_SUFFIX = "Desc";
    /**
     * 字典設定資訊
     */
    private DictDesc dict;
    /**
     * 構造方法
     *
     * @param dict 字典描述
     */
    public DictDescSerializer(DictDesc dict) {
        this.dict = dict;
    }
    @Override
    public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
        DictDesc dict = property.getAnnotation(DictDesc.class);
        if (dict != null) {
            return new DictDescSerializer(dict);
        }
        return this;
    }
    /**
     * Method that can be called to ask implementation to serialize
     * values of type this serializer handles.
     *
     * @param value    Value to serialize; can <b>not</b> be null.
     * @param gen      Generator used to output resulting Json content
     * @param provider Provider that can be used to get serializers for
     *                 serializing Objects value contains, if any.
     */
    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        provider.defaultSerializeValue(value, gen);
        if (dict != null) {
            String fieldName = gen.getOutputContext().getCurrentName();
            // 新增轉換之後的欄位:xxxDesc
            gen.writeStringField(fieldName.concat(LABEL_SUFFIX), DictDescSerializerUtils.getDesc(dict, fieldName, value));
        }
    }
}

字典序列化與反序列工具類

import cn.hutool.extra.spring.SpringUtil;
import com.aimilin.common.core.pojo.system.SysDict;
import com.aimilin.common.dict.annotation.DictDesc;
import com.aimilin.common.dict.annotation.DictEnum;
import com.aimilin.common.dict.exception.DictException;
import com.aimilin.common.dict.exception.enums.DictExceptionEnum;
import com.aimilin.common.dict.service.SysDictService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
 * 字典轉換工具類
 *
 * @author liujunguang1
 * @version V1.0
 * @date 2022/5/20 23:19
 */
@Slf4j
public class DictDescSerializerUtils {
    /**
     * 獲取字典資訊
     *
     * @param dict  字典物件
     * @param value 字典值
     * @return
     */
    public static String getDesc(DictDesc dict, String field, Object value) {
        if (ArrayUtils.isEmpty(dict.dictType()) && ArrayUtils.isEmpty(dict.enumType())) {
            throw new DictException(DictExceptionEnum.REQUEST_DICT_TYPE, field);
        }
        try {
            if (value == null) {
                throw new DictException(DictExceptionEnum.REQUEST_NOT_NULL, field);
            }
            if (ArrayUtils.isNotEmpty(dict.enumType())) {
                return getEnumDesc(dict, field, value);
            }
            return getDictDesc(dict, field, value);
        } catch (Exception e) {
            log.error("字典轉換異常, field:{}, enumType:{}, dictType:{}, 值:{}, 異常:{}",
                    field, dict.enumType(), dict.dictType(), value, e.getMessage(), e);
            if (dict.throwException()) {
                throw e instanceof DictException ? (DictException) e : new DictException(DictExceptionEnum.DICT_EXCEPTION, e);
            }
            return dict.defaultValue();
        }
    }
    /**
     * 獲取列舉型別的描述資訊
     *
     * @param dict  字典
     * @param value 值
     * @return 列舉desc欄位
     */
    public static String getEnumDesc(DictDesc dict, String field, Object value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        for (Class<? extends Enum<? extends DictEnum>> dictEnum : dict.enumType()) {
            Method getCode = dictEnum.getMethod("getCode");
            Method getMessage = dictEnum.getMethod("getMessage");
            for (Enum<? extends DictEnum> e : dictEnum.getEnumConstants()) {
                if (value.equals(getCode.invoke(e))) {
                    return Objects.toString(getMessage.invoke(e));
                }
            }
        }
        throw new DictException(DictExceptionEnum.UNKNOWN_ENUM_DICT_VALUE,
                String.format("Field:%s, EnumType: %s, Value: %s", field, Arrays.toString(dict.enumType()), value));
    }
    /**
     * 獲取字典中的值
     *
     * @param dict  字典註解
     * @param value 屬性值
     * @return 字典名稱
     */
    public static String getDictDesc(DictDesc dict, String field, Object value) {
        if (ArrayUtils.isEmpty(dict.dictType())) {
            throw new DictException(DictExceptionEnum.REQUEST_DICT_TYPE, field);
        }
        List<SysDict> sysDictList = SpringUtil.getBean(SysDictService.class).getDictByDictTypeCode(dict.dictType());
        if (CollectionUtils.isEmpty(sysDictList)) {
            throw new DictException(DictExceptionEnum.NO_DICT_DATA, field, Arrays.toString(dict.dictType()));
        }
        for (SysDict sysDict : sysDictList) {
            if (StringUtils.equals(sysDict.getCode(), Objects.toString(value))) {
                return sysDict.getValue();
            }
        }
        throw new DictException(DictExceptionEnum.UNKNOWN_DICT_VALUE, field, Arrays.toString(dict.dictType()));
    }
}

字典轉換服務類

/**
 * 字典服務類
 *
 * @author liujunguang1
 * @version V1.0
 * @date 2022/5/20 16:03
 */
public interface SysDictService {
    /**
     * 根據字典型別code獲取字典列表
     *
     * @param dictTypeCodes 字典型別code
     * @return List<SysDict>
     */
    public List<SysDict> getDictByDictTypeCode(String... dictTypeCodes);
}

服務實現類:

/**
 * 系統字典服務實現類
 *
 * @author liujunguang1
 * @version V1.0
 * @date 2022/5/20 16:13
 */
@Service
public class SysDictServiceImpl implements SysDictService {
    @Resource
    private SystemContextServiceApi systemContextServiceApi;
    @Resource
    private SysDictCache sysDictCache;
    /**
     * 根據字典型別編碼獲取字典資料
     *
     * @param dictTypeCodes 字典型別編碼值
     * @return List<SysDict>
     */
    @Override
    public List<SysDict> getDictByDictTypeCode(String... dictTypeCodes) {
        List<SysDict> dictTypeCache = sysDictCache.getDictTypeCache(dictTypeCodes);
        if (CollectionUtils.isNotEmpty(dictTypeCache)) {
            return dictTypeCache;
        }
        return systemContextServiceApi.getDictByDictTypeCode(dictTypeCodes).getData();
    }
}

字典快取服務

可以修改為使用本地快取方式

/**
 * 字典快取服務
 *
 * @version V1.0
 * @date 2022/5/19 12:13
 */
@Slf4j
@Service
public class SysDictCache {
    @Resource
    private RedisService redisService;
    /**
     * 獲取字典型別快取
     *
     * @param dictTypes 字典型別
     * @return 字典列表
     */
    public List<SysDict> getDictTypeCache(String... dictTypes) {
        if (Objects.isNull(redisService)) {
            log.info("redisService 為空,不使用字典快取");
            return null;
        }
        List<List<SysDict>> dictValues = redisService.getMultiCacheMapValue(CommonConstant.DICT_CACHE_KEY, Arrays.asList(dictTypes));
        if (CollectionUtils.isEmpty(dictValues)) {
            return null;
        }
        List<SysDict> result = new ArrayList<>();
        dictValues.stream().filter(Objects::nonNull).forEach(result::addAll);
        log.debug("查詢字典快取,dictTypes:{}, 結果:{}", dictTypes, result);
        return result;
    }
    /**
     * 清空字典型別快取
     *
     * @param dictTypes 字典型別
     */
    public void cleanDictTypeCache(String... dictTypes) {
        if (Objects.isNull(redisService)) {
            return;
        }
        redisService.deleteCacheMapValue(CommonConstant.DICT_CACHE_KEY, dictTypes);
        log.info("清除字典快取,dictTypes:{}", StringUtils.join(dictTypes));
    }
    /**
     * 新增快取
     *
     * @param sysDictList 系統字典列表
     */
    public void putDictTypeCache(List<SysDict> sysDictList) {
        if (Objects.isNull(redisService) || CollectionUtils.isEmpty(sysDictList)) {
            return;
        }
        Map<String, List<SysDict>> collect = sysDictList.stream().collect(Collectors.groupingBy(SysDict::getTypeCode));
        for (Map.Entry<String, List<SysDict>> entry : collect.entrySet()) {
            redisService.setCacheMapValue(CommonConstant.DICT_CACHE_KEY, entry.getKey(), entry.getValue());
            log.info("設定字典快取,dictType:{},結果:{}", entry.getKey(), entry.getValue());
        }
    }
}

到此這篇關於Java分析講解序列化與字典功能的序列化的文章就介紹到這了,更多相關Java序列化內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com