Spring Boot升级遇到的问题

注:本文是从2.2.7.RELEASE版本升级到2.6.6遇到的问题

1、报错:java: 程序包javax.validation.constraints不存在

解决方法:引入validation依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

原因:我们在controller接口中有时候会用注解 @Validated @Valid来校验参数,比如校验非空、数据格式等,或者用注解@NotNull @NotBlank来校验实体,这些注解都是javax.validation包下的。该内容在2.3.0版本被官方移除了,2.3.0及以上版本要用这些注解需要引入上述依赖

2、报错:org.springframework.boot.context.config.InvalidConfigDataPropertyException: Property 'spring.profiles.active' imported from location 'class path resource [application-dev.yml]' is invalid in a profile specific resource [origin: class path resource [application-dev.yml] - 20:13]

解决方案:

将spring.profiles.active用spring.config.activate.on-profile替代

原因:spring.profiles.active即显式激活指定的配置文件,SpringBoot在启动加载配置文件时,如没有明确指定spring.profiles.active属性默认是加载application.ymlapplication.properties文件

xml多环境配置方案

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <profileActive>dev</profileActive>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <profileActive>test</profileActive>
        </properties>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <profileActive>prod</profileActive>
        </properties>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
    </profile>
</profiles>

spring boot在2.4版本spring.profiles.active被弃用了,推荐使用spring.config.activate.on-profile,据说是为了更好的支持Kubernetes(官方说明:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-Config-Data-Migration-Guide)

3、报错:org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

解决方案:

这个报错是因为项目中使用了Swagger2,需要在你的SwaggerConfiguration类加上注解@EnableWebMvc

原因:大致是因为spring boot在2.6把请求路径与spring MVC处理应用程序映射进行匹配的默认策略从AntPathMatcher改为了PathPatternParser导致的

原理上我也没懂,springfox的github有关于此的讨论(Spring 5.3/Spring Boot 2.4 support · Issue #3462 · springfox/springfox · GitHub)里边有个提出的hack解决方案需要修改Springfox的源码,有兴趣的可以看一下(反正我是没看太懂)

  • 27
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 升级Spring Boot至2.x版本后,出现"IllegalArgumentException: jdbcUrl is required"异常的问题通常是由于数据库连接URL配置问题引起的。 解决这个问题,可以尝试以下步骤: 1. 检查application.properties或application.yml配置文件,确认数据库连接URL配置是否正确。连接URL的格式通常是以jdbc开头的一串字符串,例如:jdbc:mysql://localhost:3306/mydatabase。确保连接URL正确且没有拼写错误。 2. 检查数据库连接URL配置是否包含了必需的参数。有些数据库连接URL需要指定用户名、密码,以及其他可选参数。根据具体的数据库配置,确保连接URL中包含了必需的参数,并正确设置。 3. 确认数据库驱动依赖是否正确导入。在pom.xml文件中,检查是否导入了正确的数据库驱动依赖。版本不匹配或者缺少数据库驱动依赖可能会引发该异常。可以尝试更新或更换数据库驱动依赖,确保与Spring Boot 2.x版本兼容。 4. 检查数据库服务是否正常运行。尝试连接数据库服务,确认数据库服务是否已正确启动。如果数据库服务未启动或者无法连接,也可能会导致该异常。 5. 如果以上步骤仍然不能解决问题,可以尝试通过调试或查看异常的详细堆栈信息,找到异常抛出的位置和原因,以更好地定位问题并解决。 通过以上步骤,您应该能够解决"IllegalArgumentException: jdbcUrl is required"异常,并使Spring Boot 2.x版本能够正常连接数据库。 ### 回答2: 在升级 Spring Boot 版本至 2.x 的过程中,如果出现了 "IllegalArgumentException: jdbcUrl is required" 异常,这个错误通常是由于数据源配置引起的。 在 Spring Boot 2.x 中,部分数据源相关的配置发生了变化。首先,你需要确定你的项目中是否已经配置了正确的数据库连接信息。你可以打开你的配置文件(比如 application.yml 或 application.properties)检查数据库相关的配置项。 在 Spring Boot 2.x 中,数据源的连接 URL(JDBC URL)的配置项名称从 spring.datasource.url 变更为 spring.datasource.jdbc-url。因此,你需要将配置项名称修改为正确的名称。 例如,在 application.properties 文件中,将原来的配置项名字 spring.datasource.url 调整为 spring.datasource.jdbc-url 。 另外,还需要确保你的配置项值是正确的。比如,连接数据库的 URL 格式是否正确,用户名和密码是否正确,数据库驱动是否正确配置等等。 重新配置完数据库连接信息后,重新启动你的 Spring Boot 应用程序,应该就不会再出现 "IllegalArgumentException: jdbcUrl is required" 异常了。 ### 回答3: 当升级Spring Boot到2.x版本时,遇到"IllegalArgumentException: jdbcUrl is required"异常时,这通常是因为在配置文件中未正确配置数据库的连接URL(jdbcUrl)导致的。 解决这个问题的方法是检查配置文件中是否正确配置了数据源的相关属性。通常,数据源相关的配置都是以"spring.datasource"作为前缀的。确保你的配置文件(如application.properties或application.yml)中包含了以下必需的配置项: - `spring.datasource.url`:指定数据库连接URL。根据你所使用的数据库类型而定,一般为以"jdbc:"开头的URL。 - `spring.datasource.username`:指定数据库用户名。 - `spring.datasource.password`:指定数据库密码。 - `spring.datasource.driver-class-name`:指定数据库驱动类名。 例如,如果你在MySQL数据库上运行,可以在配置文件中添加以下配置项: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=myusername spring.datasource.password=mypassword spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 请注意,你还需要根据实际情况修改上述配置项的值,确保与你的数据库设置相匹配。 如果你已经正确配置了数据源的相关属性,但仍然遇到该异常,请确保你的配置文件能够正确加载。你可以在应用启动时查看日志,确认是否成功加载了配置文件。 总结:当升级Spring Boot到2.x版本时,遇到"IllegalArgumentException: jdbcUrl is required"异常时,这通常是因为在配置文件中未正确配置数据库连接URL导致的。确保正确配置数据库连接URL以及相关属性,并确保配置文件能够正确加载。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值