springboot1.4项目升级2.1采坑记录
涉及技术springboot,tomcat,quartz,多数据源RelaxedPropertyResolver,RelaxedDataBinder升级
1.修改pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath />
</parent>
Spring Boot 发布了一个新spring-boot-properties-migrator模块。一旦作为该模块作为依赖被添加到你的项目中,它不仅会分析应用程序的环境,而且还会在启动时打印诊断信息,而且还会在运行时为您暂时迁移属性。
1.org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter’ 已经过时了
//新的实现是:
@Configuration
//public class WebMvcConfg implements WebMvcConfigurer {
//省略
}
@Configuration
public class WebMvcConfg extends WebMvcConfigurationSupport {
//省略
}
2.quartz实例化报错
Caused by: java.lang.IllegalStateException: Active Scheduler of name ‘DefaultQuartzScheduler’ already registered in Quartz SchedulerRepository. Cannot create a new Spring-managed Scheduler of the same name! at org.springframework.scheduling.quartz.SchedulerFactoryBean.createScheduler(SchedulerFactoryBean.java:678) ~[spring-context-support-5.1.2.RELEASE.jar:5.1.2.RELEASE]
修改quartz配置文件中的
org.quartz.scheduler.instanceName,不能是DefaultQuartzScheduler
3.数据库连接报错
java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
&serverTimezone=GMT
4.tomcat版本升级报错
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
a.可以重写Tomcat配置。
@Configuration
public class TomcatConfig {
@Bean
public TomcatServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addConnectorCustomizers((Connector connector) -> {
connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}");
connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}");
});
return factory;
}
}
b.可以降低tomcat版本
5.springboot2 接口返回中文乱码
@ResponseBody标注的String返回值,没有使用默认消息转换器而是是用了其他工具类将对象转为String。
6.访问静态资源404
WebMvcConfigurationSupport 导致自动装配失效
改为WebMvcConfigurer 实现
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
是否配置拦截器
7.quartz2.3.0,springboot2.1.0集成,之前配置的定时任务数据库持久化配置无法修改,删除,及执行。而且无报错。
org.quartz.scheduler.instanceName = SchedulerFactory
保证quartz配置的实例名与数据库qrtz_triggers中SCHED_NAME一致即可解决。
8.多数据源操作修改
多数据源切换之前使用的RelaxedPropertyResolver,RelaxedDataBinder 配置解析数据绑定对象已无法使用。改用如下方法:
private Properties loadPropByPrefix(Environment env, String prefixProp) {
Iterable sources = ConfigurationPropertySources.get(env);
Binder binder = new Binder(sources);
BindResult bindResult = binder.bind(prefixProp, Properties.class);
return (Properties) bindResult.get();
}
9.文件上传失败
低版本springboot1.4显示声明CommonsMultipartResolver为mutipartResolver并
排除org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration自动装配
@Bean(name = "multipartResolver")
public MultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("UTF-8");
// resolveLazily属性启用是为了推迟文件解析,以在在UploadAction中捕获文件大小异常
resolver.setResolveLazily(true);
resolver.setMaxInMemorySize(40960);
// 上传文件大小 5M 5*1024*1024
resolver.setMaxUploadSize(5 * 1024 * 1024);
return resolver;
}
新版本2.0之后MultipartFile报空指针,去掉上述配置,使用默认配置即可。