由于需要跟进技术发展的脚步,对原有项目springboot进行2.0升级,但升级并不是说改一下版本就完事了,springboot2.0变动比较多,详细变化可以百度一下,下面针对升级springboot遇到的问题记录一下,希望能对升级的同学有用:
1.Redis
添加pom配置:
org.springframework.boot spring-boot-starter-data-redis
配置类:
package com.ts.config; import java.time.Duration; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; 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.cache.RedisCacheWriter; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisPassword; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @Configuration @EnableCaching public class RedisCacheConfig extends CachingConfigurerSupport { Logger logger = LoggerFactory.getLogger(RedisCacheConfig.class); @Value("${spring.redis.database}") private int database; @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.timeout}") private int timeout; @Value("${spring.redis.pool.max-active}") private int maxActive; @Value("${spring.redis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.pool.min-idle}") private int minIdle; @Value("${spring.redis.pool.max-wait}") private long maxWaitMillis; @Value("${spring.redis.password}") private String password; @Bean public RedisConnectionFactory jedisConnectionFactory() { RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host, port); redisStandaloneConfiguration.setDatabase(database); redisStandaloneConfiguration.setHostName(host); redisStandaloneConfiguration.setPassword(RedisPassword.of(password)); JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration); //其他配置,可再次扩展 return jedisConnectionFactory; } @Bean public JedisPool redisPoolFactory() { logger.info("JedisPool注入成功!!"); logger.info("redis地址:{}:{}", host, port); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password); return jedisPool; } @SuppressWarnings({ "rawtypes", "unchecked" }) @Bean public RedisTemplateredisTemplate(RedisConnectionFactory cf) { RedisTemplate redisTemplate = new RedisTemplate (); redisTemplate.setConnectionFactory(cf); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); RedisSerializer redisSerializer = new StringRedisSerializer(); //key序列化方式 redisTemplate.setKeySerializer(redisSerializer); //value序列化 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); //value hashmap序列化 redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); return redisTemplate; } @Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { return new RedisCacheManager( RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), this.getRedisCacheConfigurationWithTtl(600), // 默认策略,未配置的 key 会使用这个 this.getRedisCacheConfigurationMap() // 指定 key 策略 ); } private Map getRedisCacheConfigurationMap() { Map redisCacheConfigurationMap = new HashMap<>(); redisCacheConfigurationMap.put("MerchantProductAccess", this.getRedisCacheConfigurationWithTtl(30)); redisCacheConfigurationMap.put("UserInfoListAnother", this.getRedisCacheConfigurationWithTtl(180)); return redisCacheConfigurationMap; } private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) { Jackson2JsonRedisSerializer
2.WebMvcConfiguarationSupport
代码:
package com.ts.config; import java.nio.charset.Charset; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import com.ts.common.interceptor.AuthorizeInterceptor; /** * 设置拦截器和静态资源访问路径 * * CustomWebMvcConfigurerAdapter
* 创建人:wbd
* 时间:2017年12月27日-下午6:45:42
* @version 2.0 * */ @Configuration //标注此文件为一个配置项,spring boot才会扫描到该配置。该注解类似于之前使用xml进行配置 public class CustomWebMvcConfigurerAdapter extends WebMvcConfigurationSupport { @Autowired private Environment env; @Bean public HttpMessageConverterresponseBodyConverter(){ StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8")); return converter; } @Override public void configureMessageConverters(List > converters) { super.configureMessageConverters(converters); converters.add(responseBodyConverter()); } /** * 设置拦截器 */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new AuthorizeInterceptor()).addPathPatterns("/**"); //对来自/user/** 这个链接来的请求进行拦截 } /** * 设置静态资源及其系统路径 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("file:" + env.getProperty("upload.filePath")); super.addResourceHandlers(registry); } /** * 跨域设置 /**代表所有路径 */ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**"); } }
3.需要添加fasterxml jar包
com.fasterxml classmate 1.4.0