情境
使用遇到了 SpringBoot 1.5.2.RELEASE 版本遇到了 server.context-path 配置不起作用的问题。
进入设计好的 url 对应报错 Whitelabel Error Page 如下:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Nov 29 10:20:28 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available
解决
简单分析一下,根据报错也就是没找到嘛,检查代码没什么问题那就是配置这里的问题了。
错误的配置:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: "amtf"
url: jdbc:mysql://192.168.1.103/amtfxm?characterEncoding=utf-8&userSSL=false
jpa:
show-sql: true
server:
context-path: /sell
正确的配置:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: "amtf"
url: jdbc:mysql://192.168.1.103/amtfxm?characterEncoding=utf-8&userSSL=false
jpa:
show-sql: true
server:
context-path: /sell
总结
spring 和 server 两个配置属同级别,我写错时把 server 写在了 spring下,其实 IDEA 也提示了配置无法解析,但没有飘红同时我也没有仔细看。
2.0以后的版本
查资料也发现 2.0 后的版本也会常出现这种问题,但是原因不同,2.0 之后改成了如下的配置方式:
server:
servlet:
context-path: /example