feat(修改): 消息转换器
This commit is contained in:
parent
5f927f2e5a
commit
b295aec0cc
|
@ -7,8 +7,9 @@
|
||||||
<option value="$PROJECT_DIR$/pom.xml" />
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
|
<option name="workspaceImportForciblyTurnedOn" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="corretto-17" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="corretto-18" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
File diff suppressed because it is too large
Load Diff
Binary file not shown.
4
pom.xml
4
pom.xml
|
@ -20,8 +20,8 @@
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>17</maven.compiler.source>
|
<maven.compiler.source>18</maven.compiler.source>
|
||||||
<maven.compiler.target>17</maven.compiler.target>
|
<maven.compiler.target>18</maven.compiler.target>
|
||||||
<mybatis.spring>2.2.0</mybatis.spring>
|
<mybatis.spring>2.2.0</mybatis.spring>
|
||||||
<lombok>1.18.20</lombok>
|
<lombok>1.18.20</lombok>
|
||||||
<fastjson>1.2.76</fastjson>
|
<fastjson>1.2.76</fastjson>
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package com.sky.common.config;
|
package com.sky.common.config;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||||
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
import com.sky.common.interceptor.JwtTokenUserInterceptor;
|
import com.sky.common.interceptor.JwtTokenUserInterceptor;
|
||||||
import com.sky.common.interceptor.RedisTokenAdminInterceptor;
|
import com.sky.common.interceptor.RedisTokenAdminInterceptor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -44,8 +48,12 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
||||||
log.info("扩展消息转换器...");
|
log.info("扩展消息转换器...");
|
||||||
// 创建一个消息转换器对象
|
// 创建一个消息转换器对象
|
||||||
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
|
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||||
|
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||||
|
objectMapper.registerModule(new JavaTimeModule());
|
||||||
// 需要为消息转换器设置一个对象转换器,对象转换器可以将Java对象序列化为json数据
|
// 需要为消息转换器设置一个对象转换器,对象转换器可以将Java对象序列化为json数据
|
||||||
converter.setObjectMapper(new ObjectMapper());
|
converter.setObjectMapper(objectMapper);
|
||||||
// 将自己的消息转化器加入容器中
|
// 将自己的消息转化器加入容器中
|
||||||
converters.add(0, converter);
|
converters.add(0, converter);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.sky.common.json;
|
package com.sky.common.json;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
|
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
|
||||||
|
@ -17,11 +18,12 @@ import java.time.format.DateTimeFormatter;
|
||||||
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
|
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对象映射器,基于Jackson将Java对象转为JSON,或者将JSON转为Java对象
|
* 对象映射器:基于jackson将Java对象转为json,或者将json转为Java对象
|
||||||
* 将JSON转为Java对象过程--JSON反序列化Java对象
|
* 将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象]
|
||||||
* 将Java对象转为JSON--序列化Java对象到JSON
|
* 从Java对象生成JSON的过程称为 [序列化Java对象到JSON]
|
||||||
*/
|
*/
|
||||||
public class JacksonObjectMapper extends ObjectMapper {
|
public class JacksonObjectMapper extends ObjectMapper {
|
||||||
|
|
||||||
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
|
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
|
||||||
// public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
// public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||||
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm";
|
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm";
|
||||||
|
@ -31,16 +33,18 @@ public class JacksonObjectMapper extends ObjectMapper {
|
||||||
super();
|
super();
|
||||||
// 收到未知属性时不报异常
|
// 收到未知属性时不报异常
|
||||||
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
|
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
// 反序列化时,属性不存在的兼容处理
|
|
||||||
this.getDeserializationConfig().withoutFeatures(FAIL_ON_UNKNOWN_PROPERTIES);
|
|
||||||
|
|
||||||
SimpleModule simpleModule = new SimpleModule();
|
// 反序列化时,属性不存在的兼容处理
|
||||||
simpleModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
|
this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||||
|
|
||||||
|
SimpleModule simpleModule = new SimpleModule()
|
||||||
|
.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
|
||||||
.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
|
.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
|
||||||
.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))
|
.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))
|
||||||
.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
|
.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
|
||||||
.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
|
.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
|
||||||
.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
|
.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
|
||||||
|
|
||||||
// 注册功能模块 例如,可以添加自定义序列化器和反序列化器
|
// 注册功能模块 例如,可以添加自定义序列化器和反序列化器
|
||||||
this.registerModule(simpleModule);
|
this.registerModule(simpleModule);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue