解决SpringBoot中application.yml配置的server.port和server.servlet.context-path失效以及swagger访问不到的问题

本文探讨了SpringBoot应用中application.yml配置文件中server.port和server.servlet.context-path的失效问题,以及如何避免被xxl-job-admin配置覆盖。提供了通过迁移配置文件、修改文件类型和使用SpringCloud注解等解决方案,并解决了Swagger访问问题。

解决SpringBoot中application.yml配置的服务端口server.port和服务访问路径server.servlet.context-path失效以及swagger访问不到的问题

如图,application.yml的配置
在这里插入图片描述

在服务启动的过程中控制台打印:
在这里插入图片描述

可以看到服务的访问路径变成了"xxl-job-admin",也就是说在application.yml中的配置失效了。这是因为引入xxl-job-admin之后,其自带的配置文件顶替掉了我们在application.yml中的配置(相同的配置会优先加载xxl-job-admin中的配置)

在这里插入图片描述

在这里插入图片描述

这是因为SpringBoot在启动的时候优先加载properties文件,如果properties文件中没有再去读yml文件,而xxl-job-admin中正好自带了application.properties配置文件,覆盖了我们在application.yml中的配置导致自己的配置不生效。

解决方案:

1.在resources文件夹下面新建一个config文件夹,把application.yml移到config文件夹下面

2.将配置文件变为application.properties

3.在依赖库中删除xxl-job-admin自带的配置文件(不推荐)

4.加入SpringCloud的注解,将application.yml变为bootstrap.yml(加载优先级高)

这里采用的是第一种方案,修改之后服务启动正常:

在这里插入图片描述

swagger也不能正常的访问,还是同样的问题:

这是xxl-job-admin中的配置:

在这里插入图片描述

在application.yml中配置为下图,就可以正常访问了。为同类的问题提供一个解决思路

在这里插入图片描述

# 项目相关配置 ruoyi: name: RuoYi version: 4.8.1 copyrightYear: 2025 demoEnabled: true profile: D:/ruoyi/uploadPath addressEnabled: false # 开发环境配置 server: port: 80 servlet: context-path: / tomcat: uri-encoding: UTF-8 accept-count: 1000 threads: max: 800 min-spare: 100 # 日志配置 logging: level: com.ruoyi: debug org.springframework: warn # 用户配置 user: password: maxRetryCount: 5 # Spring数据源配置(已修正) spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/ry?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8 username: root password: 123456 # 修正后的密码 druid: initial-size: 5 min-idle: 5 max-active: 20 max-wait: 60000 validation-query: SELECT 1 test-while-idle: true test-on-borrow: false test-on-return: false # 监控配置 web-stat-filter: enabled: true stat-view-servlet: enabled: true login-username: root login-password: 123456 filter: stat: enabled: true log-slow-sql: true slow-sql-millis: 1000 merge-sql: true thymeleaf: mode: HTML encoding: utf-8 cache: false messages: basename: static/i18n/messages jackson: time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss servlet: multipart: max-file-size: 10MB max-request-size: 20MB devtools: restart: enabled: true # MyBatis mybatis: typeAliasesPackage: com.ruoyi.**.domain mapperLocations: classpath*:mapper/**/*Mapper.xml configLocation: classpath:mybatis/mybatis-config.xml # PageHelper分页插件 pagehelper: helperDialect: mysql supportMethodsArguments: true params: count=countSql # Shiro shiro: user: loginUrl: /login unauthorizedUrl: /unauth indexUrl: /index captchaEnabled: true captchaType: math cookie: domain: path: / httpOnly: true maxAge: 30 cipherKey: session: expireTime: 30 dbSyncPeriod: 1 validationInterval: 10 maxSession: -1 kickoutAfter: false rememberMe: enabled: true # 防止XSS攻击 xss: enabled: true excludes: /system/notice/* urlPatterns: /system/*,/monitor/*,/tool/* # 防止csrf攻击 csrf: enabled: false whites: /druid # Swagger配置 swagger: enabled: true application.yml 配置是否齐全
最新发布
11-22
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

南城☾

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

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

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

打赏作者

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

抵扣说明:

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

余额充值