栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

引入RedisTemplate时的异常:NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

引入RedisTemplate时的异常:NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException

异常场景

今天想测一个RedisTemplate的问题,于是用 Spring Initializr 创建了一个 Spring Boot 项目,并引入了 Spring Data Redis。

pom文件中会给我引入对应的包:


 	org.springframework.boot
    spring-boot-starter-data-redis

自定义 RedisTemplate 配置,设置 Value 序列化方式为 GenericJackson2JsonRedisSerializer

@Configuration
public class RedisTemplateConfig {

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }
}

简单配好后启动项目,直接提示找不到Jackson相关类的异常:java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException

下面截取了部分错误日志

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisTemplate' defined in class path resource [com/example/redistemplatesourcecodedemo/config/RedisTemplateConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.core.RedisTemplate]: Factory method 'redisTemplate' threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
解决方法

看了下spring-boot-starter-data-redis的pom,确实没有引入Jackson,那我们就引入。
Spring 官方已经有了一个spring-boot-starter-json依赖,我们直接引进来:


    org.springframework.boot
    spring-boot-starter-json

再次启动,问题解决。

问题回顾

之前项目中没有增加此依赖,也正常啊,是为什么呢?
经过一番排查,原来之前项目引入了spring-boot-starter-web依赖,它是pom已经引入了该依赖了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/737595.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号