Springboot-1.5.10.RELEASE 升级到 2.2.4.RELEASE版本遇到的一些问题总结
由于现在springboot的主流版本都已经是2.x的了,因此自己也打算将项目中使用的springboot1.5.10升级到2.2.4这个版本。
自己也能够预料得到,在升级过程中肯定会遇到各种各样的问题,自己也做好了准备去面对这些问题。
问题一:JedisPoolConfig这个类找不到
这个类所在的包为 redis.clients.jedis.JedisPoolConfig;由于springboot版本的切换,redis的版本也跟着进行了切换,本人使用的这个版本中不包含这个类,
因此报错。
解决办法:使用搜索引擎查找问题的原因,在下面这两篇博文中找到了解决办法。
按照这篇博文找到一个解决方案
https://blog.csdn.net/wangxinyao1997/article/details/88956674
https://stackoverflow.com/questions/22704518/jedispoolconfig-is-not-assignable-to-genericobjectpoolconfig
解决方式为添加一个redis.clients
的依赖,
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.4.2</version>
</dependency>
重新启动,项目报错,错误信息为:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'JedisConnectionFactory' defined in class path resource [applets/common/config/RedisConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.jedis.JedisConnectionFactory]: Factory method 'JedisConnectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/util/SafeEncoder
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:885)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:789)
... 32 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.jedis.JedisConnectionFactory]: Factory method 'JedisConnectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/util/SafeEncoder
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651)
... 46 common frames omitted
Caused by: java.lang.NoClassDefFoundError: redis/clients/jedis/util/SafeEncoder
at org.springframework.data.redis.connection.jedis.JedisConverters.lambda$static$1(JedisConverters.java:129)
at org.springframework.data.redis.connection.jedis.JedisConverters.toBytes(JedisConverters.java:344)
at org.springframework.data.redis.connection.jedis.JedisConverters.<clinit>(JedisConverters.java:136)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.<clinit>(JedisConnectionFactory.java:85)
at applets.common.config.RedisConfig.JedisConnectionFactory(RedisConfig.java:117)
at applets.common.config.RedisConfig$$EnhancerBySpringCGLIB$$7312b2a1.CGLIB$JedisConnectionFactory$1(<generated>)
at applets.common.config.RedisConfig$$EnhancerBySpringCGLIB$$7312b2a1$$FastClassBySpringCGLIB$$99f19937.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
at applets.common.config.RedisConfig$$EnhancerBySpringCGLIB$$7312b2a1.JedisConnectionFactory(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 47 common frames omitted
Caused by: java.lang.ClassNotFoundException: redis.clients.jedis.util.SafeEncoder
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 62 common frames omitted
继续查找问题的解决办法,参考博文为 https://www.cnblogs.com/renjianjun/p/13611140.html
修改方式为将redis.clients的版本切换为3.3.0即可,重新启动,报了一个警告信息如下:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
大致意思就是com.mysql.jdbc.Driver这个驱动类已经弃用了,需要使用新的驱动类com.mysql.cj.jdbc.Driver。
修改配置,将com.mysql.jdbc.Driver 修改为 com.mysql.cj.jdbc.Driver,从新启动还是有问题。
问题二:配置的改动,有好几个地方的配置都有改动,例举如下:
server.context-path=/applets 修改为server.servlet.context-path=/applets
spring文件上传大小配置和http请求大小配置
spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=1000Mb
修改为
spring.servlet.multipart.max-file-size=100Mb
spring.servlet.multipart.max-request-size=100Mb
参考文章 https://blog.csdn.net/weixin_40816738/article/details/119878720
问题三:单位换算问题
启动继续报错,报错信息为
Failed to bind properties under 'spring.servlet.multipart.max-request-size' to org.springframework.util.unit.DataSize:
Property: spring.servlet.multipart.max-request-size
Value: 100Mb
Origin: "spring.servlet.multipart.max-request-size" from property source "applicationConfig: [classpath:/application-dev.properties]"
Reason: failed to convert java.lang.String to org.springframework.util.unit.DataSize
大致意思就是底层读取配置文件中最大文件上传大小和请求大小信息时,使用的是字节来读取,不能添加单位Mb
参考文章https://blog.csdn.net/guocunlei25/article/details/90232243
将100Mb修改为对应的字节单位,并且不添加单位即可。
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=104857600
spring.servlet.multipart.max-request-size=104857600
单位在线换算的网址为http://www.bejson.com/convert/filesize/
配置全部修改好后,重新启动项目,正常运行。至此项目升级改造完毕,从此开启springboot2.x时代。