欢迎光临Fuwu360     登录 | 免费注册 |
按CTRL+D收藏,下次访问更方便
当前位置: 首页 > 编程 > Spring boot1.5.9升级到2.2.6解决报错的方法

浏览历史

Spring boot1.5.9升级到2.2.6解决报错的方法

龙永超 / 2020-04-12

Spring boot1.5.9项目,升级到2.2.6版本,发生了一系列的报错,解决办法如下

Spring boot1.5.9项目,升级到2.2.6版本,发生了一系列的报错,解决办法如下:

第一个报错是:

java.lang.IllegalArgumentException: Could not find class [org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration]

解决方法是:

把@SpringBootApplication改为

@SpringBootApplication(exclude = SecurityAutoConfiguration.class)

另外是application.yml的配置,凡是带大写字母驼峰命名的,改为带横杠的形式,否则会报错:

Invalid characters: 'L'
Bean: dataInitConfig
Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letter

Redis的配置改为

spring:
redis:
host: localhost
port: 6379
database: 2
password: 123456
lettuce:
pool:
max-active: 300

然后是redis相关的报错

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectMapperConfigurer' defined in class path resource [springfox/documentation/spring/web/SpringfoxWebMvcConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': Unsatisfied dependency expressed through method 'setConfigurers' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConfig': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConnectionFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory]: Factory method 'redisConnectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig

解决办法是,pom文件里添加:

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.8.0</version>
</dependency>

Redis的Java配置也需要修改

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;


/**
* 2. * Redis配置
* 3. *
* 4. * @author longyongchao
* 5. * @date 2020年4月12日 11:49:12
* 6.
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);

/**
* 类名+方法名+参数列表的类型+参数值 再做 哈希散列 作为key
*
* @return
*/
@Bean
@Override
public KeyGenerator keyGenerator() {
logger.info("RedisCacheConfig.keyGenerator()");
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... objects) {
// This will generate a unique key of the class name, the method name
//and all method parameters appended.
/* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String token = request.getHeader("token");*/
StringBuilder sb = new StringBuilder();
sb.append(o.getClass().getSimpleName())
.append(":")
.append(method.getName())
.append(":");
for (Object obj : objects) {
if (null != obj) {// 替换字符串
String objKey = JSON.toJSONString(obj);
objKey = objKey.replace(":", "=");
sb.append(objKey);
}
}
logger.info("keyGenerator=" + sb.toString());
return sb.toString();
}
};
}


//缓存生存时间
private Duration timeToLive = Duration.ofHours(1);

@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
//redis缓存配置
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(this.timeToLive)
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()))
.disableCachingNullValues();
//缓存配置map
Map<String, RedisCacheConfiguration> cacheConfigurationMap = new HashMap<>();
//自定义缓存名,后面使用的@Cacheable的CacheName
cacheConfigurationMap.put("default", config);
//根据redis缓存配置和reid连接工厂生成redis缓存管理器
RedisCacheManager redisCacheManager = RedisCacheManager.builder(connectionFactory)
.cacheDefaults(config)
.transactionAware()
.withInitialCacheConfigurations(cacheConfigurationMap)
.build();
logger.debug("自定义RedisCacheManager加载完成");
return redisCacheManager;
}

/**
* redisTemplate模板提供给其他类对redis数据库进行操作
*
* @param redisConnectionFactory
* @return
*/

@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(keySerializer());
redisTemplate.setHashKeySerializer(keySerializer());
redisTemplate.setValueSerializer(valueSerializer());
redisTemplate.setHashValueSerializer(valueSerializer());
logger.debug("自定义RedisTemplate加载完成");
return redisTemplate;
}

/**
* redis键序列化使用StrngRedisSerializer
*
* @return
*/
private RedisSerializer<String> keySerializer() {
return new StringRedisSerializer();
}

/**
* redis值序列化使用json序列化器
*
* @return
*/

private RedisSerializer<Object> valueSerializer() {
return new GenericJackson2JsonRedisSerializer();
}

}

解决的swagger报错

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method linkDiscoverers in org.springframework.hateoas.config.HateoasConfiguration required a single bean, but 15 were found:
- modelBuilderPluginRegistry: defined in null
- modelPropertyBuilderPluginRegistry: defined in null
- typeNameProviderPluginRegistry: defined in null
- documentationPluginRegistry: defined in null
- apiListingBuilderPluginRegistry: defined in null
- operationBuilderPluginRegistry: defined in null
- parameterBuilderPluginRegistry: defined in null
- expandedParameterBuilderPluginRegistry: defined in null
- resourceGroupingStrategyRegistry: defined in null
- operationModelsProviderPluginRegistry: defined in null
- defaultsProviderPluginRegistry: defined in null
- pathDecoratorRegistry: defined in null
- relProviderPluginRegistry: defined by method 'relProviderPluginRegistry' in class path resource [org/springframework/hateoas/config/HateoasConfiguration.class]
- linkDiscovererRegistry: defined in null
- entityLinksPluginRegistry: defined by method 'entityLinksPluginRegistry' in class path resource [org/springframework/hateoas/config/WebMvcEntityLinksConfiguration.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

升级swagger版本到2.9.2就没事了




微信或浏览器扫码阅读
用户评论 (共0条评论)

暂时还没有任何用户评论

总计 0 个记录,共 1 页。
亲,赐个留言吧!
captcha