Springboot upgrade to 2.x where changes are needed

Source: Internet
Author: User
Tags propertyaccessor

Due to the need to follow up the pace of technological development, The original Project Springboot 2.0 upgrade, but upgrade is not to say change the version is done, springboot2.0 change more, detailed changes can Baidu, the following for upgrade Springboot encountered problems record, hope to upgrade students useful:

1.Redis

To add a POM configuration:

        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId> Spring-boot-starter-data-redis</artifactid>        </dependency>

Configuration class:

 PackageCom.ts.config;Importjava.time.Duration;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.concurrent.ConcurrentHashMap;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;ImportOrg.springframework.beans.factory.annotation.Value;ImportOrg.springframework.cache.CacheManager;ImportOrg.springframework.cache.annotation.CachingConfigurerSupport;Importorg.springframework.cache.annotation.EnableCaching;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.data.redis.cache.RedisCacheConfiguration;ImportOrg.springframework.data.redis.cache.RedisCacheManager;ImportOrg.springframework.data.redis.cache.RedisCacheWriter;Importorg.springframework.data.redis.connection.RedisConnectionFactory;ImportOrg.springframework.data.redis.connection.RedisPassword;Importorg.springframework.data.redis.connection.RedisStandaloneConfiguration;Importorg.springframework.data.redis.connection.jedis.JedisConnectionFactory;Importorg.springframework.data.redis.core.RedisTemplate;ImportOrg.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;ImportOrg.springframework.data.redis.serializer.RedisSerializationContext;ImportOrg.springframework.data.redis.serializer.RedisSerializer;ImportOrg.springframework.data.redis.serializer.StringRedisSerializer;Importcom.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;ImportCom.fasterxml.jackson.annotation.PropertyAccessor;ImportCom.fasterxml.jackson.databind.ObjectMapper;ImportRedis.clients.jedis.JedisPool;Importredis.clients.jedis.JedisPoolConfig, @Configuration @enablecaching Public classRediscacheconfigextendscachingconfigurersupport {Logger Logger= Loggerfactory.getlogger (rediscacheconfig.class); @Value ("${spring.redis.database}")    Private intdatabase; @Value ("${spring.redis.host}")    PrivateString host; @Value ("${spring.redis.port}")    Private intPort; @Value ("${spring.redis.timeout}")    Private inttimeout; @Value ("${spring.redis.pool.max-active}")    Private intmaxactive; @Value ("${spring.redis.pool.max-idle}")    Private intMaxidle; @Value ("${spring.redis.pool.min-idle}")    Private intMinidle; @Value ("${spring.redis.pool.max-wait}")    Private LongMaxwaitmillis; @Value ("${spring.redis.password}")    PrivateString password; @Bean Publicredisconnectionfactory jedisconnectionfactory () {redisstandaloneconfiguration redisstandaloneconfiguration 
    =Newredisstandaloneconfiguration (host, Port);        Redisstandaloneconfiguration.setdatabase (database);        Redisstandaloneconfiguration.sethostname (host);                Redisstandaloneconfiguration.setpassword (redispassword.of (password)); Jedisconnectionfactory jedisconnectionfactory=Newjedisconnectionfactory (redisstandaloneconfiguration); //other configurations that can be extended again        returnjedisconnectionfactory; } @Bean PublicJedispool redispoolfactory () {Logger.info ("Jedispool Injection success!! "); Logger.info ("Redis address: {}:{}", host, Port); Jedispoolconfig Jedispoolconfig=NewJedispoolconfig ();        Jedispoolconfig.setmaxidle (Maxidle);        Jedispoolconfig.setmaxwaitmillis (Maxwaitmillis); Jedispool Jedispool=NewJedispool (Jedispoolconfig, host, port, timeout, password); returnJedispool; } @SuppressWarnings ({"Rawtypes", "unchecked"}) @Bean PublicRedistemplate<string, string>redistemplate (Redisconnectionfactory CF) {redistemplate<string, string> redistemplate =NewRedistemplate<string, string>();                Redistemplate.setconnectionfactory (CF); Jackson2jsonredisserializer Jackson2jsonredisserializer=NewJackson2jsonredisserializer (Object.class); Objectmapper om=NewObjectmapper ();        Om.setvisibility (Propertyaccessor.all, Visibility.any);        Om.enabledefaulttyping (ObjectMapper.DefaultTyping.NON_FINAL);                Jackson2jsonredisserializer.setobjectmapper (OM); Redisserializer<String> Redisserializer =NewStringredisserializer (); //key Serialization ModeRedistemplate.setkeyserializer (Redisserializer); //Value SerializationRedistemplate.setvalueserializer (Jackson2jsonredisserializer); //Value HashMap serializationRedistemplate.sethashvalueserializer (Jackson2jsonredisserializer); returnredistemplate; } @Bean PublicCacheManager CacheManager (redisconnectionfactory redisconnectionfactory) {return NewRediscachemanager (Rediscachewriter.nonlockingrediscachewriter (redisconnectionfactory), This. Getrediscacheconfigurationwithttl (600),//The default policy, which is not configured by the key, will use this             This. Getrediscacheconfigurationmap ()//Specify the key policy        ); }    PrivateMap<string, rediscacheconfiguration>Getrediscacheconfigurationmap () {Map<string, rediscacheconfiguration> rediscacheconfigurationmap =NewHashmap<>(); Rediscacheconfigurationmap.put ("Merchantproductaccess", This. Getrediscacheconfigurationwithttl (30)); Rediscacheconfigurationmap.put ("Userinfolistanother", This. GETREDISCACHECONFIGURATIONWITHTTL (180)); returnRediscacheconfigurationmap; }    Privaterediscacheconfiguration Getrediscacheconfigurationwithttl (Integer seconds) {Jackson2jsonredisserializer<Object> Jackson2jsonredisserializer =NewJackson2jsonredisserializer<> (Object.class); Objectmapper om=NewObjectmapper ();        Om.setvisibility (Propertyaccessor.all, Visibility.any);        Om.enabledefaulttyping (ObjectMapper.DefaultTyping.NON_FINAL);        Jackson2jsonredisserializer.setobjectmapper (OM); Rediscacheconfiguration rediscacheconfiguration=Rediscacheconfiguration.defaultcacheconfig (); Rediscacheconfiguration=Rediscacheconfiguration.serializevalueswith (Redisserializationcontext. Serializationpair. Fromserializer (Jackson2jsonredisserializer)). Entryttl (Duration.ofseconds (seconds        )); returnrediscacheconfiguration; }}
View Code

2.WebMvcConfiguarationSupport

Code:

 PackageCom.ts.config;ImportJava.nio.charset.Charset;Importjava.util.List;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.context.annotation.Bean;ImportOrg.springframework.context.annotation.ComponentScan;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.core.env.Environment;ImportOrg.springframework.http.converter.HttpMessageConverter;ImportOrg.springframework.http.converter.StringHttpMessageConverter;ImportOrg.springframework.web.servlet.config.annotation.CorsRegistry;ImportORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;ImportOrg.springframework.web.servlet.config.annotation.InterceptorRegistry;ImportOrg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;ImportOrg.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;ImportCom.ts.common.interceptor.AuthorizeInterceptor;/*** Set Interceptor and static resource access Path * * customwebmvcconfigureradapter<br> * Creator: WBD <BR> * Time: December 27, 2017-PM 6:45:42 <br& Gt * @version2.0 **/@Configuration//If you label this file as a configuration item, spring boot will not scan to that configuration. This annotation is similar to the previous configuration using XML Public classCustomwebmvcconfigureradapterextendsWebmvcconfigurationsupport {@AutowiredPrivateEnvironment env; @Bean PublicHttpmessageconverter<string>Responsebodyconverter () {Stringhttpmessageconverter converter=NewStringhttpmessageconverter (Charset.forname ("UTF-8")); returnConverter; } @Override Public voidConfiguremessageconverters (listconverters) {        Super. Configuremessageconverters (converters);    Converters.add (Responsebodyconverter ()); }        /*** Set Interceptor*/@Override Public voidaddinterceptors (Interceptorregistry registry) {Registry.addinterceptor (NewAuthorizeinterceptor ()). Addpathpatterns ("/**");//interception of requests from/user/** this link    }        /*** Set static resources and their system paths*/@Override Public voidaddresourcehandlers (Resourcehandlerregistry registry) {Registry.addresourcehandler ("/resources/**"). Addresourcelocations ("File:" + env.getproperty ("Upload.filepath"))); Super. Addresourcehandlers (registry); }        /*** cross-domain setting/** represents all Paths*/@Override Public voidaddcorsmappings (Corsregistry registry) {registry.addmapping ("/**"); }}
View Code

3. Need to add Fasterxml jar package

        <dependency>            <groupId>com.fasterxml</groupId>            <artifactid>classmate</ artifactid>            <version>1.4.0</version>        </dependency>

Springboot upgrade to 2.x where changes are needed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.