首先序列化注解一般有2个
1.@JsonSerialize
2.@JsonDeserialize
一般 @JsonDeserialize 作用在setting上,而 @JsonSerialize 作用在getting上
当然也可以写在类的属性上
废话不多说上代码
public class DictUtil {
private static final Logger LOG = LoggerFactory.getLogger(DictUtil.class);
private static final BaseDictConvertService BASE_DICT_CONVERT_SERVICE = Jalor.getContext()
.getBean("baseDictConvert", BaseDictConvertService.class);
private static List<DictVO> initDictByKey(String classifyCode) {
List<DictVO> dictList = BASE_DICT_CONVERT_SERVICE.findAllDictList(classifyCode);
LOG.info("return List The is:{}" , JSONObject.toJSONString(dictList));
return dictList;
}
private static List<DictVO> findLookUpByKey(String classifyCode) {
List<DictVO> lookUpList = BASE_DICT_CONVERT_SERVICE.findLookupListByCode(classifyCode);
LOG.info("return List The is:{}" , JSONObject.toJSONString(lookUpList));
return lookUpList;
}
private static DictVO initDictByKeyAndItemCode(String classifyCode, String itemCode) {
DictVO dict = BASE_DICT_CONVERT_SERVICE.findDictByCode(classifyCode, itemCode);
LOG.info("return DictVO The is:{}", JSONObject.toJSONString(dict));
return dict;
}
/**
* 根据父级CODE查询 子级集合
*
* @param codeKey String
* @return List
*/
public static List<DictVO> getItemList(String codeKey) {
return DictUtil.initDictByKey(codeKey);
}
/**
* 根据父级CODE查询Lookup
*
* @param codeKey String
* @return List
*/
public static List<DictVO> getLookupItemList(String codeKey) {
return findLookUpByKey(codeKey);
}
/**
* 根据父级和子级CODE查询 子级名称
*
* @param codeKey String
* @param code String
* @return String
*/
public static String getItemNameByItemCode(String codeKey, String code) {
return initDictByKeyAndItemCode(codeKey, code).getKeyName();
}
}
@Service("baseDictConvert")
public class BaseDictConvertService {
@Autowired
private IBaseDictConvertDao dictConvertDao;
@Autowired
private CommonServiceImpl commonService;
/**
* 根据codeKey查询值
*
* @param key 入参
* @return DictVO
*/
public List<DictVO> findAllDictList(String key) {
if (Const.LEVEL.equals(key)) {
List<LookupItemVO> itemList = commonService.findItemListByZhOrEn(key);
List<DictVO> dictList = new ArrayList<>();
itemList.forEach(item -> {
DictVO dictVO = new DictVO();
dictVO.setId(String.valueOf(item.getItemId()));
dictVO.setCodeKey(item.getItemCode());
dictVO.setKeyName(item.getItemName());
dictList.add(dictVO);
});
return dictList;
} else {
return dictConvertDao.findAllDictList(key);
}
}
/**
* 根据codeKey和code查询值
*
* @param key 入参
* @param code 入参
* @return DictVO
*/
public DictVO findDictByCode(String key, String code) {
if (Const.LEVEL.equals(key) && StringUtils.isNotBlank(code)) {
LookupItemVO item = commonService.findLookupItemOneByItem(key, code);
DictVO dictVO = new DictVO();
dictVO.setId(String.valueOf(item.getItemId()));
dictVO.setCodeKey(item.getItemCode());
dictVO.setKeyName(item.getItemName());
return dictVO;
} else {
List<DictVO> dictByCode = dictConvertDao.findDictByCode(key, code);
return dictByCode.get(0);
}
}
@Component
public class DictConvertUtils extends JsonSerializer<String> {
private static final Logger LOG = LoggerFactory.getLogger(DictConvertUtils.class);
private final List<DictVO> dictList;
public DictConvertUtils() {
this(StringUtils.EMPTY);
}
public DictConvertUtils(String codeSerializer) {
if (StringUtils.isEmpty(codeSerializer)) {
dictList = new ArrayList<>();
} else {
dictList = DictUtil.getItemList(codeSerializer);
}
}
@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (ObjectUtils.isEmpty(value)) {
gen.writeString("");
} else {
StringJoiner joiner = new StringJoiner(",");
List<String> values = Arrays.asList(value.split(","));
for (DictVO dict : dictList) {
if (values.contains(dict.getCodeKey())) {
joiner.add(dict.getKeyName());
}
}
gen.writeString(joiner.toString());
}
/**
* 导出LooK时CODE转Name
*
* @param dictVOS 字典数组
* @param param 比较值入参
* @return String s
*/
public static String matchingValues(List<DictVO> dictVOS, String param) {
if (CollectionUtils.isEmpty(dictVOS) || StringUtils.isBlank(param)) {
return "";
}
if (StringUtils.isNotBlank(param) && param.contains(Const.COMMA)) {
List<String> stringList = new ArrayList<>();
String[] split = param.split(Const.COMMA);
for (String str : split) {
String value = dictVOS.stream().filter(item -> item.getCodeKey().equals(str)).distinct()
.map(DictVO::getKeyName).collect(Collectors.joining(Const.COMMA));
stringList.add(value);
}
return String.join(Const.COMMA, stringList);
} else {
return dictVOS.stream().filter(item -> item.getCodeKey().equals(param)).distinct()
.map(DictVO::getKeyName).collect(Collectors.joining(Const.COMMA));
}
}
}
/**
* 业务角色
*/
public static class DomianList extends DictConvertUtils {
public DomianList() {
super(Const.LOOKUP_DOMAIN);
}
}
/**
* 分类
*/
public static class CategoryList extends DictConvertUtils {
public CategoryList() {
super(Const.LOOKUP_CATEGORY);
}
}
/**
* 级别
*/
public static class LeveList extends DictConvertUtils {
public LeveList() {
super(Const.LEVEL);
}
}
/**
* 证书
*/
public static class CertificateList extends DictConvertUtils {
public CertificateList() {
super(Const.CARD);
}
}
}
@Setter
@Getter
public class DictVO extends PublicVo {
/**
* 主键id
*/
private String id;
/**
* code
*/
private String codeKey;
/**
* 名称
*/
private String keyName;
}
好了大致就是这样实现序列化字典值的,但由于我的项目是业务字典,结果导致一个问题,就是用户修改了字典的name 导致页面列表的 字典名称不能实时更新,有知道的小伙伴是什么原因吗?欢迎留言告知,谢谢