diff --git a/README.md b/README.md index 86ddac1490c21e4df18944827c840b16814940cd..cbc18fa4904c1564ea7854e771a98352ba22ad71 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,14 @@ Eric-Dream === + 本项目是基于Gradle构建的多模块SpringCloud工程。采用`传统线程模型`的SpringWeb框架,mybatis-plus和mysql官方驱动。具体框架如下表: +## gradle安装与配置 +如果IDEA自动下载gradle很慢。那么可以先提前安装好gradle,然后指定下安装目录即可。 +![](./asset/img/gradle-special-location.png) +![](./asset/img/gradle-wrapper.png) + + 技术 | 说明 | 官网 ----|----|---- Spring Boot | 容器+MVC框架 | [https://spring.io/projects/spring-boot](https://spring.io/projects/spring-boot) diff --git a/SERIALIZE.md b/SERIALIZE.md index 3dfd20aa3368a9492e2e2419ed1bfba4a67debed..f04daea6a63d369e55d8785b0e666b41f52a1a49 100644 --- a/SERIALIZE.md +++ b/SERIALIZE.md @@ -79,6 +79,7 @@ public class UserParam { } ``` +注意参数Field的上面校验注解不可以是lombok的,否则会改变字节码信息,导致类的Field与值无法识别,最后枚举反序列化失败 ### Controller POST方法 ```java @@ -467,4 +468,10 @@ mybatis-plus: 以上的操作完成了枚举的从前端接收,反序列化成枚举对象在程序中表达。然后再存储到数据库中。从数据库中取code转成枚举,在程序中表达,再序列化枚举后传输给前端。一个非常完整的循环,基本上满足了程序中对枚举使用的需求。 ---- \ No newline at end of file +--- + + +# fastjson枚举自定义序列化器 + +https://blog.csdn.net/qq_26680031/article/details/83473643 + diff --git a/asset/img/gradle-special-location.png b/asset/img/gradle-special-location.png new file mode 100644 index 0000000000000000000000000000000000000000..485f4d45250bc5cbe8571fe914b0f76a512e6941 Binary files /dev/null and b/asset/img/gradle-special-location.png differ diff --git a/asset/img/gradle-wrapper.png b/asset/img/gradle-wrapper.png new file mode 100644 index 0000000000000000000000000000000000000000..8f2c0fe7c7b9114e55dca5ab048cc7b1770e983f Binary files /dev/null and b/asset/img/gradle-wrapper.png differ diff --git a/common/src/main/java/hxy/dream/common/serializer/BaseEnumDeserializer.java b/common/src/main/java/hxy/dream/common/serializer/BaseEnumDeserializer.java index e85b9b1484e05c6146e36754968996c77d6d7e1f..787966466e1fd202e67807b613ab1e7158655182 100644 --- a/common/src/main/java/hxy/dream/common/serializer/BaseEnumDeserializer.java +++ b/common/src/main/java/hxy/dream/common/serializer/BaseEnumDeserializer.java @@ -24,24 +24,36 @@ public class BaseEnumDeserializer extends JsonDeserializer { public BaseEnum deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { try { - //前端输入的值 + // 前端输入的值 String inputParameter = p.getText(); if (StringUtils.isBlank(inputParameter)) { return null; } JsonStreamContext parsingContext = p.getParsingContext(); - String currentName = parsingContext.getCurrentName();//字段名 - Object currentValue = parsingContext.getCurrentValue();//前端注入的对象(ResDTO) - Field field = ReflectionUtils.getField(currentValue.getClass(), currentName); // 通过对象和属性名获取属性的类型 -// 获取对应得枚举类 - Class enumClass = field.getType(); -// 根据对应的值和枚举类获取相应的枚举值 - BaseEnum anEnum = DefaultInputJsonToEnum.getEnum(inputParameter, enumClass); - log.info("\n====>测试反序列化枚举[{}]==>[{}.{}]", inputParameter, anEnum.getClass(), anEnum); - return anEnum; + // 字段名 + String currentName = parsingContext.getCurrentName(); + // 前端注入的对象(如:ResParam) + Object currentValue = parsingContext.getCurrentValue(); + if (currentValue != null) { + // 通过对象和属性名获取属性的类型 + Field field = ReflectionUtils.getField(currentValue.getClass(), currentName); + // 获取对应得枚举类 + Class enumClass = field.getType(); + // 根据对应的值和枚举类获取相应的枚举值 + BaseEnum anEnum = DefaultInputJsonToEnum.getEnum(inputParameter, enumClass); + if (log.isTraceEnabled()) { + log.trace("\n====>测试反序列化枚举[{}]==>[{}.{}]", inputParameter, anEnum.getClass(), anEnum); + } + return anEnum; + } else { + throw new RuntimeException("枚举反序列化失败,注意该属性不可以使用lombok的注解,如@NonNull等"); + } + } catch (Exception e) { throw new RuntimeException(e); } + + } }