springboot1.4项目升级2.1采坑记录

本文记录了SpringBoot从1.4版本升级到2.1版本过程中遇到的问题,包括pom文件修改、WebMvcConfigurerAdapter的替代、quartz实例化错误、数据库连接问题、Tomcat版本升级错误、接口返回中文乱码、静态资源访问404、quartz定时任务配置、多数据源操作调整和文件上传失败等,并提供了详细的解决办法。
摘要由CSDN通过智能技术生成


涉及技术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报空指针,去掉上述配置,使用默认配置即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NewTech精选

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值