在做一个前后端分离的项目的登录功能,Jackson默认不支持Java 8中的日期时间类型LocalDateTime。
下面是报错:
乱码检查序列化,报500
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.redis.serializer.SerializationException: Could not write JSON: Java 8 date/time type java.time.LocalDateTime
not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: com.zzg.sys.entity.MyUser["myCreateTime"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type java.time.LocalDateTime
not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: com.zzg.sys.entity.MyUser["myCreateTime"])] with root cause
idea报错信息:
postman返回的信息:
"status": 500,
"error": "Internal Server Error",
"path": "/myUser/login"
这个异常是因为Jackson默认不支持Java 8中的日期时间类型LocalDateTime。
这个错误发生在一个名为 servlet.service()
的方法中,该方法位于名为 dispatcherServlet
的上下文路径下。该错误的原因是请求处理失败,并引发了一个名为 org.springframework.data.redis.serializer.SerializationException
的异常,其中包含一条消息:“Could not write JSON: Java 8 date/time type java.time.LocalDateTime not supported by default: add Module 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310' to enable handling (through reference chain: com.zzg.sys.entity.MyUser["myCreateTime"])”。
根据错误消息,问题出在尝试将 Java 8 的日期时间类型 java.time.LocalDateTime
序列化为 JSON 时。这是因为默认情况下,com.fasterxml.jackson.databind
库不支持这种类型
解决方法:
1、在项目的 pom.xml
文件中,添加 com.fasterxml.jackson.datatype:jackson-datatype-jsr310
依赖:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.12.4</version>
</dependency>
2、在Spring Boot应用程序的配置类(通常是一个带有 @Configuration
注解的类)中,配置Jackson的 ObjectMapper
bean,并注册 JavaTimeModule
:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
return objectMapper;
}
}
我的最终解决方法:
意思是上面的方法我用了,但是没用,不过可以试试。
@Configuration
public class MyRedisConfig {
@Resource
private RedisConnectionFactory factory;
@Bean
public RedisTemplate redisTemplate(){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
redisTemplate.setValueSerializer(serializer);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
om.setTimeZone(TimeZone.getDefault());
om.configure(MapperFeature.USE_ANNOTATIONS, false);
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
om.registerModule(new JavaTimeModule());//redis的报错了,这是我新加的
serializer.setObjectMapper(om);
return redisTemplate;
}
}
om.registerModule(new JavaTimeModule());//redis的报错了,这是我新加的
mybatis-plus:
mapper-locations: classpath:mappersys/*Mapper.xml
type-aliases-package: com.zzg.**.entity
最终解决了。
感想:
这个bug困扰了我几天,卡在这不能进行下去。我也是刚学,有问题只能自己解决。
希望能对有相同问题的人有帮助。