Springboot 1.5.3 升级2.7.18版本踩坑记录

从1.5.13
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.13.RELEASE</version>
    <relativePath/>
</parent>

升级2.7.18
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.18</version>
    <relativePath/>
</parent>


<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.5.2</version> <!-- 选择与 Spring Boot 2.7.x 兼容的版本 -->
</dependency>

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

如引入了cloud,也需要升级与2.7.18兼容的版本
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
           <!-- <version>Edgware.SR5</version>-->
            <version>2021.0.8</version><!-- 升级-->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>


编译期间报错:
1.
ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.xx.xx.BUIApplication]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist

解决方案:
排除 spring-cloud-netflix-core-1.4.6.RELEASE.jar 依赖
 <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-netflix-core</artifactId>

 升级依赖 spring-boot-autoconfigure-2.7.18.jar ,boot2.7.18后 不在使用ServerPropertiesAutoConfiguration,取而代之的是ServerProperties
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>2.7.18</version>
</dependency>


2.
Error:java: 无法访问org.springframework.cloud.netflix.feign.EnableFeignClients
  找不到org.springframework.cloud.netflix.feign.EnableFeignClients的类文件
  有关详细信息, 请参阅以下堆栈跟踪。
  com.sun.tools.javac.code.Symbol$CompletionFailure: 找不到org.springframework.cloud.netflix.feign.EnableFeignClients的类文件

解决方案:
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-feign</artifactId>
   </dependency>
升级
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>


以上两个报错都是 依赖冲突报错,排查命令:
mvn dependency:tree --检查maven 依赖冲突
--用于生成项目的依赖树。它会列出项目中所有直接和传递性依赖的详细信息,包括依赖的版本、作用范围(如 compile、test、provided 等)
mvn dependency:tree -Dincludes=*tomcat*


SpringBoot2.7,启动报错:

1.
2025-02-13 16:00:48.512  WARN 3576 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Invalid mapping on handler class [com.xx.xx.bp.common.web.controller.MrWebDispatcherServlet]: public void com.xx.xx.bp.common.web.controller.MrWebDispatcherServlet.process(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws javax.servlet.ServletException,com.xx.xx.xx.core.common.exception.YGException
2025-02-13 16:00:48.516  INFO 3576 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2025-02-13 16:00:48.592  INFO 3576 --- [           main] ConditionEvaluationReportLoggingListener : 

Fix this pattern in your application or switch to the legacy parser implementation with 'spring.mvc.pathmatch.matching-strategy=ant_path_matcher'.

解释:路径配置问题,springboot升级到2.6.2后路径配置发生变化
In Spring MVC, the path was previously analyzed by AntPathMatcher, but it was changed to use PathPatternParser introduced in WebFlux from Spring 5.3.0.”通配符发生了变化。把/**/* 改为 /*/*即可

解决方案:
或在application.properties切换匹配策略
#切换AntPathMatcher匹配策略
spring.mvc.pathmatch.matching-strategy=ant_path_matcher


2.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'freeMarkerConfigurer' defined in class path resource [org/springframework/boot/autoconfigure/freemarker/FreeMarkerServletWebConfiguration.class]: Invocation of init method failed; nested exception is freemarker.core.Configurable$UnknownSettingException: Unknown FreeMarker configuration setting: "recognize_standard_file_extensions"

解决方案:
如果当前版本的 FreeMarker 不支持 "recognize_standard_file_extensions" 设置,升级FreeMarker版本
如:
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <!--20250213-->
  <!--  <version>2.3.23</version>-->
    <version>2.3.31</version>
</dependency>

3.
Connection refused: connect; nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8761 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect stacktrace=org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8761/eureka/apps/": Connect to localhost:8761 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect; nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8761 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:791) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:717) at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:608) at org.springframework.cloud.netflix.eureka.http.RestTemplateEurekaHttpClient.getApplicationsInternal(RestTemplateEurekaHttpClient.java:145)

解决方案:

yml:
eureka:
  client:
    register-with-eureka: false  # 禁用注册到Eureka
    fetch-registry: false        # 禁用获取注册信息
    service-url:
      defaultZone: http://localhost:8761/eureka/

properties:

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/


4.
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) ~[spring-webmvc-5.3.31.jar:5.3.31]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.31.jar:5.3.31]
Caused by: java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
    at org.springframework.web.cors.CorsConfiguration.validateAllowCredentials(CorsConfiguration.java:495) ~[spring-web-5.3.31.jar:5.3.31]
    at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:532) ~[spring-webmvc-5.3.31.jar:5.3.31]
    at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1266) ~[spring-webmvc-5.3.31.jar:5.3.31]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1048) ~[spring-webmvc-5.3.31.jar:5.3.31]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:965) ~[spring-webmvc-5.3.31.jar:5.3.31]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.31.jar:5.3.31]
    ... 37 common frames omitted

解决方案:
registry.addMapping("/**")
        .allowedOriginPatterns("*") // 将allowedOrigins替换为 allowedOriginPatterns
        .allowCredentials(true)
        .allowedMethods("*")
        .allowedHeaders("*")
        .maxAge(3600);


5.
我的SpringBoot从 1.5.x升级到 2.7.18的版本了,现在启动项目时发现在Servlet init方法中通过 this.getServletContext().getContextPath(); 获取Application.properties 配置文件中定义的 spring.application.name=omsd ,我的oms项目名称,现在拿到不到了 获取出来是空的。
(启动正常,项目访问不了 springboot启动默认带有项目名称,yml中是有对应配置。这里配置了的话 就加上,未配置的话 就不需要加了)

解决方案:
server.servlet.context-path=/omsd

6.
我的前后端是分离的,启了两个端口。前端去跨域请求后端在Springboot2.7.18中配置了 server.servlet.context-path=/omsd,我的前端访问路径就是/ 没有加项目名称oms 这样后端生成JSESSIONID值又是指向 /omsd 下的,导致浏览器因路径 不匹配 会阻止向后端 发送 JSESSIONID,后端也就拿不到session对象。这应该在那一环节去解决问题?

解决方案:
可以通过`server.servlet.session.cookie.path`属性来设置session cookie的路径:server.servlet.session.cookie.path=/

后端用的是Spring Boot 2.7.18,配置了`server.servlet.context-path=/oms`,
也就是后端的根路径是`/oms`。而前端访问路径是根路径`/`,没有加`oms`,导致后端生成的JSESSIONID的路径是`/oms`,
而前端请求的时候因为路径不同,浏览器不会发送这个cookie,所以后端拿不到session。

那问题出在哪儿呢?首先,cookie的Path属性决定了浏览器在发送请求时是否会携带该cookie。如果前端请求的路径和后端设置的cookie路径不一致,浏览器就不会发送cookie,导致session无法维持。用户的后端设置了context-path为`/oms`,所以Spring Session生成的JSESSIONID默认的Path应该是`/oms`。
而前端请求的路径可能没有这个前缀,导致路径不匹配。
接下来,我需要考虑在哪个环节解决这个问题。可能的解决方案有几个方向:
1. **前端请求路径加上`/oms`前缀**:这样前端请求的路径和后端cookie的Path一致,浏览器就会发送JSESSIONID。但用户提到前端访问路径没有加项目名称,可能他们希望保持前端URL简洁,或者有其他原因不想改前端路径。
2. **后端调整cookie的Path**:让后端生成的JSESSIONID的Path设置为`/`,这样无论前端请求哪个路径,只要在同一个域名下,都会发送这个cookie。这可能需要配置Spring Session或Tomcat的相关设置。
3. **CORS配置中添加允许凭证**:跨域请求时,如果涉及到cookie,需要前端设置`withCredentials`为true,同时后端CORS配置允许凭证。不过用户的问题可能已经解决了跨域的基本问题,现在主要是路径导致的cookie不发送。
4. **修改后端的context-path**:如果后端不需要`/omsd`这个上下文路径,可以考虑去掉,但可能影响现有的其他配置。
现在需要分析哪种方法更合适。假设用户不想修改前端请求路径,也不想改变后端的context-path,那么调整cookie的Path可能是关键。这时候需要查看Spring Boot中如何配置session cookie的路径。
在Spring Boot中,可以通过`server.servlet.session.cookie.path`属性来设置session cookie的路径。例如,在`application.properties`或`application.yml`中设置:
```
server.servlet.session.cookie.path=/
```
这样,无论后端的context-path是什么,生成的JSESSIONID的Path都会被设为根路径,这样前端请求任何路径都会携带这个cookie。不过需要注意,这样可能会导致其他应用在同一域名下的cookie冲突,但如果是独立部署的话可能没问题。
另外,如果用户使用的是Spring Security,还需要检查相关的配置,确保session管理正确,并且CORS配置允许携带凭证


7.
Springboot 从1.5升级到2.7 打包vue一起 运行,访问项目报错404,访问index.html报304,是这个spring.resources.static-locations=file:/app/xxx/xxx/vue属性不能用了吗?跟这个有关?

解决方案:
在application.properties中增加配置:spring.web.resources.static-locations: file:/app/xxx/xxx/vue/

1. Spring Boot 版本升级的影响
Spring Boot 从 1.5 升级到 2.7,其中一个重要的变化是静态资源处理方式的调整。在 Spring Boot 2.0 之后,spring.resources.static-locations 属性的使用方式发生了变化。
Spring Boot 1.5: 该属性主要用于指定静态资源的查找路径,可以配置多个路径,例如: 
YAML
spring.resources.static-locations: file:/app/xxx/xxx/vue, classpath:/static

Spring Boot 2.7: 该属性依然可以使用,但是推荐使用 spring.web.resources.static-locations 属性,并且配置方式有所不同。例如: 
YAML
spring.web.resources.static-locations: file:/app/xxx/xxx/vue/, classpath:/static/

注意,路径末尾需要添加 /,这表示一个目录


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值