欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper

程序员文章站 2022-07-14 08:51:52
...
当前SpringBoot版本:2.0.2.RELEASE

pom.xml:
<!--redis client:lettuce-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

 

 spring data redis 配置自定义redisTemplate,设置value的序列化方式为GenericJackson2JsonRedisSerializer:

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}

直接报错说jackson的类找不着,很明显是因为我们没有配置jackson的依赖(引入 spring-boot-starter-data-redis 时官方应该配置好依赖的)

解决办法,增加依赖:

<!-- jackson -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-json</artifactId>
</dependency>

 

注意:

如果我们是个web项目(依赖了 spring-boot-starter-web ),这个里面会自动依赖 jackson相关的jar包:

java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper