先定义一个枚举基类

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize; @JsonDeserialize(using = BaseEnumDeserializer.class)
@JsonSerialize(using = BaseEnumSerializer.class)
public interface BaseEnum { /**
* @description:必须在子类的重写方法返回具体的枚举name ,例如return this.name()<br/>
* @author: Binz
*/
String getCode(); String getDisplayName(); static <E extends Enum<E>> BaseEnum valueOf(String enumCode,Class<E> clazz) {
BaseEnum enumm = (BaseEnum) Enum.valueOf(clazz, enumCode);
return enumm;
}
}

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

/**
* 通用状态,所有跟状态相关的都按照这个定义
* @author Binz
* @date 2019-05-14 11:28:25
*/
public enum CommonStatus implements BaseEnum{
CREATE("新建"),
//一般默认都是这个状态,只有特殊情况才会用到新建状态
ENABLED("启用"),
DISABLED("停用"),
DELETE("删除")
; CommonStatus(String displayName){
this.displayName = displayName;
} private String displayName; @Override
public String getDisplayName() {
return displayName;
} @Override
public String getCode() {
return this.name();
} }

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

import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
/**
* 用于BaseEum的子类解析成json格式,一般在api中注入此解析器
* @author Binz
* 2019-05-27 11:29:02
*/
public class BaseEnumSerializer extends JsonSerializer<BaseEnum>{ @Override
public void serialize(BaseEnum value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
Map<String,String> map = new HashMap<>();
map.put("code", value.getCode());
map.put("displayName", value.getDisplayName());
gen.writeObject(map);
} }

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

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection; import org.springframework.beans.BeanUtils; import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonStreamContext;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeType; /**
* 接收BaseEnum反序列化
* @author Binz
*/
public class BaseEnumDeserializer extends JsonDeserializer<BaseEnum>{ @SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public BaseEnum deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode node = jp.getCodec().readTree(jp);
String currentName = jp.currentName();
Object currentValue = jp.getCurrentValue();
Class findPropertyType = null;
if(currentValue instanceof Collection) { JsonStreamContext parsingContext = jp.getParsingContext(); JsonStreamContext parent = parsingContext.getParent();
Object currentValue3 = parent.getCurrentValue();
String currentName3 = parent.getCurrentName();
try {
Field listField = currentValue3.getClass().getDeclaredField(currentName3);
ParameterizedType listGenericType = (ParameterizedType) listField.getGenericType();
Type listActualTypeArguments = listGenericType.getActualTypeArguments()[0];
findPropertyType = (Class) listActualTypeArguments;
} catch (Exception e) {
e.printStackTrace();
}
}else {
findPropertyType = BeanUtils.findPropertyType(currentName, currentValue.getClass());
}
if(findPropertyType == null) {
throw new BaseException("数据格式异常");
}
String asText = null;
if(node.getNodeType() == JsonNodeType.STRING) {
asText = node.asText();
}else{
asText = node.get("code").asText();
}
BaseEnum valueOf = null;
if(StringUtil.isNotBlank(asText)){
valueOf = BaseEnum.valueOf(asText, findPropertyType);
}
return valueOf;
} }

然后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. 关于公众号JavaTokings侵权声明

    该公众号几乎有所有文章都是在未经原作者的同意下私自将文章转移至其公众号.其中 [消息中间件ActiveMQ使用详解](链接是:https://www.cnblogs.com/yanfei1819/p/ ...

  2. Shiro的认证原理(Subject#login的背后故事)

    登录操作一般都是我们触发的: Subject subject = SecurityUtils.getSubject(); AuthenticationToken authenticationToken ...

  3. ZOJ 3949 Edge to the Root(想法)(倍增)

    Edge to the Root Time Limit: 1 Second      Memory Limit: 131072 KB Given a tree with n vertices, we ...

  4. Python生成随机数的一些函数

    头文件: import random 1.生成一个随机浮点数,范围是0-1: print random.random() 2.生成指定范围内的随机浮点数: print random.uniform(a ...

  5. nyoj 151 Biorhythms

    描述 Some people believe that there are three cycles in a person's life that start the day he or she i ...

  6. ES5 Object.defineProperty 方法

    先看一个例子: var o = {}; o.a = 1; // 等待于: Object.defineProperty(o, 'a', { value: 1, writable: true, confi ...

  7. [BZOJ 1857] 传送带

    Link: BZOJ 1857 传送门 Solution: 首先中间的两个拐点$C,D$肯定都在传送带$A,B$上 接下来感性发现固定点A/C,另一个点C/D时间随位置的变化为单峰函数 这样就是三分套 ...

  8. 【贪心】hdu6180 Schedule

    题意:给你n个任务的开始时间和结束时间,一个机器同时最多执行一个任务,问你最少要几个机器.保证机器最少的前提下,问你每个机器的开动时间(最后一次关闭-第一次开启)之和最少是多少. 把这些线段画在数轴上 ...

  9. Problem Z: 百鸡问题

    #include <stdio.h> int main() { int i, j, k; ; i <= ; i++ ) ; j <= ; j++ ) ; k <= ; k ...

  10. 也来讲REST、SOAP

    在GIS网络开发过程中不可避免的的会涉及到REST(Representational State Transfer)的服务.自从Roy Fielding博士在2000年他的博士论文中提出REST风格的 ...