先定义一个枚举基类

  1. import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
  2. import com.fasterxml.jackson.databind.annotation.JsonSerialize;
  3.  
  4. @JsonDeserialize(using = BaseEnumDeserializer.class)
  5. @JsonSerialize(using = BaseEnumSerializer.class)
  6. public interface BaseEnum {
  7.  
  8. /**
  9. * @description:必须在子类的重写方法返回具体的枚举name ,例如return this.name()<br/>
  10. * @author: Binz
  11. */
  12. String getCode();
  13.  
  14. String getDisplayName();
  15.  
  16. static <E extends Enum<E>> BaseEnum valueOf(String enumCode,Class<E> clazz) {
  17. BaseEnum enumm = (BaseEnum) Enum.valueOf(clazz, enumCode);
  18. return enumm;
  19. }
  20. }

定义自己的枚举并且实现 BaseEnum

  1. /**
  2. * 通用状态,所有跟状态相关的都按照这个定义
  3. * @author Binz
  4. * @date 2019-05-14 11:28:25
  5. */
  6. public enum CommonStatus implements BaseEnum{
  7. CREATE("新建"),
  8. //一般默认都是这个状态,只有特殊情况才会用到新建状态
  9. ENABLED("启用"),
  10. DISABLED("停用"),
  11. DELETE("删除")
  12. ;
  13.  
  14. CommonStatus(String displayName){
  15. this.displayName = displayName;
  16. }
  17.  
  18. private String displayName;
  19.  
  20. @Override
  21. public String getDisplayName() {
  22. return displayName;
  23. }
  24.  
  25. @Override
  26. public String getCode() {
  27. return this.name();
  28. }
  29.  
  30. }

自定义枚举转换器-序列化

  1. import java.io.IOException;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4.  
  5. import com.fasterxml.jackson.core.JsonGenerator;
  6. import com.fasterxml.jackson.databind.JsonSerializer;
  7. import com.fasterxml.jackson.databind.SerializerProvider;
  8. /**
  9. * 用于BaseEum的子类解析成json格式,一般在api中注入此解析器
  10. * @author Binz
  11. * 2019-05-27 11:29:02
  12. */
  13. public class BaseEnumSerializer extends JsonSerializer<BaseEnum>{
  14.  
  15. @Override
  16. public void serialize(BaseEnum value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
  17. Map<String,String> map = new HashMap<>();
  18. map.put("code", value.getCode());
  19. map.put("displayName", value.getDisplayName());
  20. gen.writeObject(map);
  21. }
  22.  
  23. }

自定义枚举转换器-反序列化

  1. import java.io.IOException;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.ParameterizedType;
  4. import java.lang.reflect.Type;
  5. import java.util.Collection;
  6.  
  7. import org.springframework.beans.BeanUtils;
  8.  
  9. import com.fasterxml.jackson.core.JsonParser;
  10. import com.fasterxml.jackson.core.JsonProcessingException;
  11. import com.fasterxml.jackson.core.JsonStreamContext;
  12. import com.fasterxml.jackson.databind.DeserializationContext;
  13. import com.fasterxml.jackson.databind.JsonDeserializer;
  14. import com.fasterxml.jackson.databind.JsonNode;
  15. import com.fasterxml.jackson.databind.node.JsonNodeType;
  16.  
  17. /**
  18. * 接收BaseEnum反序列化
  19. * @author Binz
  20. */
  21. public class BaseEnumDeserializer extends JsonDeserializer<BaseEnum>{
  22.  
  23. @SuppressWarnings({ "unchecked", "rawtypes" })
  24. @Override
  25. public BaseEnum deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
  26.  
  27. JsonNode node = jp.getCodec().readTree(jp);
  28. String currentName = jp.currentName();
  29. Object currentValue = jp.getCurrentValue();
  30. Class findPropertyType = null;
  31. if(currentValue instanceof Collection) {
  32.  
  33. JsonStreamContext parsingContext = jp.getParsingContext();
  34.  
  35. JsonStreamContext parent = parsingContext.getParent();
  36. Object currentValue3 = parent.getCurrentValue();
  37. String currentName3 = parent.getCurrentName();
  38. try {
  39. Field listField = currentValue3.getClass().getDeclaredField(currentName3);
  40. ParameterizedType listGenericType = (ParameterizedType) listField.getGenericType();
  41. Type listActualTypeArguments = listGenericType.getActualTypeArguments()[0];
  42. findPropertyType = (Class) listActualTypeArguments;
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. }else {
  47. findPropertyType = BeanUtils.findPropertyType(currentName, currentValue.getClass());
  48. }
  49. if(findPropertyType == null) {
  50. throw new BaseException("数据格式异常");
  51. }
  52. String asText = null;
  53. if(node.getNodeType() == JsonNodeType.STRING) {
  54. asText = node.asText();
  55. }else{
  56. asText = node.get("code").asText();
  57. }
  58. BaseEnum valueOf = null;
  59. if(StringUtil.isNotBlank(asText)){
  60. valueOf = BaseEnum.valueOf(asText, findPropertyType);
  61. }
  62. return valueOf;
  63. }
  64.  
  65. }

然后spring cloud之间交互的实体类中的枚举就可以自动正常转换了,缺少的引用根据自身项目更改

spring cloud jackson 枚举json互转 枚举json序列化/反序列化的更多相关文章

  1. SpringBoot 处理 POST Json 传参枚举

    在 Spring 框架中对枚举类型的序列化/反序列化是有限制的. 假设如下面这样在某些情况下就不能正常工作: 123456789 public enum PayChannelEnum implemen ...

  2. Spring Cloud官方文档中文版-Spring Cloud Config(上)

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...

  3. 阿里Dubbo疯狂更新,关Spring Cloud什么事?

    最近,开源社区发生了一件大事,那个全国 Java 开发者使用最广的开源服务框架 Dubbo 低调重启维护,并且 3 个月连续发布了 4 个维护版本. 我上次在写放弃Dubbo,选择最流行的Spring ...

  4. 介绍一下Spring Cloud Config

    Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持.使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring ...

  5. Spring Cloud Config中文文档

    https://springcloud.cc/spring-cloud-config.html 目录 快速开始 客户端使用 Spring Cloud Config服务器 环境库 健康指标 安全 加密和 ...

  6. 转 阿里Dubbo疯狂更新,关Spring Cloud什么事?

    原文地址: http://www.ityouknow.com/springcloud/2017/11/20/dubbo-update-again.html阿里Dubbo疯狂更新,关Spring Clo ...

  7. spring cloud连载第二篇之Spring Cloud Config

    Spring Cloud Config Spring Cloud Config为分布式服务提供了服务侧和客户侧的外部配置支持.通过Spring Cloud Config你可以有一个统一的地方来管理所有 ...

  8. Spring Cloud官方文档中文版-Spring Cloud Config(上)-服务端(配置中心)

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...

  9. Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵动态限流规则

    Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵动态限流规则 前面几篇文章较为详细的介绍了Sentinel的使用姿势,还没看过的小伙伴可以访问以下链接查看: &l ...

随机推荐

  1. CNN的发展

    模型的建立过程: 1959年,Hubel & Wiesel发现动物视觉皮层中的细胞负责检测感受野(receptive fields)中的光线.论文:Receptive fields and f ...

  2. MC资源整理

    MC模拟简介 蒙特卡罗模拟,因摩纳哥著名的赌场而得名.它能够帮助人们从数学上表述物理.化学.工程.经济学以及环境动力学中一些非常复杂的相互作用. 蒙特卡罗(Monte Carlo)方法,又称随机抽样或 ...

  3. [BZOJ4698][SDOI2008]Sandy的卡片(后缀自动机)

    差分之后就是求多串LCS. 对其中一个串建SAM,然后把其它串放在上面跑. 对SAM上的每个状态都用f[x]记录这个状态与当前串的最长匹配长度,res[x]是对每次的f[x]取最小值.答案就是res[ ...

  4. 20162312 2016-2017-2《Java程序设计》课程总结

    一.每周作业链接汇总 预备作业01 写的是有关老师和学生的关系: 预备作业02 如何做中学: 预备作业03 实验楼学习linux环境: 第一周作业 java入门,虚拟机等课前准备: 第二周作业 掌握J ...

  5. [DesignPattern]Builder设计模式

    模式的定义 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 模式的使用场景 相同的方法,不同的执行顺序,产生不同的事件结果时: 多个部件或零件,都可以装配到一个对象中,但是 ...

  6. Educational Codeforces Round 8 A. Tennis Tournament 暴力

    A. Tennis Tournament 题目连接: http://www.codeforces.com/contest/628/problem/A Description A tennis tour ...

  7. centos下防火墙iptables日志学习笔记

    一直找不到日志方面怎么弄,问了同事,同事给了个网址: http://www.thegeekstuff.com/2012/08/iptables-log-packets/ 下面就是我根据这个网址里面的设 ...

  8. mysql设置远程访问密码

    mysql -u root -p Aaa111222 grant all privileges on *.* to root@'%' identified by 'aaa111222; Quit ln ...

  9. Sysfs文件系统与Linux设备模型

    转:http://www.360doc.com/content/11/1218/16/1299815_173168170.shtml sysfs把连接在系统上的设备和总线组织成为一个分级的目录及文件, ...

  10. 【maven】pom.xml文件没错,但是项目有小红叉,Problems中可以看到错误:“Dynamic Web Module 3.0 requires Java 1.6 or newer.”

    解决方法: 1.将 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>m ...