先定义一个枚举基类
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之间交互的实体类中的枚举就可以自动正常转换了,缺少的引用根据自身项目更改