实际上这个并不是一个强制要求,而且如果基于spring cloud 等框架已经基于gateway 做了一层处理
但是还是推荐添加
server.servlet.context-path=/users/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dalongdemo</groupId>
<artifactId>serverroot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>serverroot</name>
<description>Demo project for Spring Boot</description>
?
<properties>
<java.version>1.8</java.version>
</properties>
?
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
?
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
?
</project>
package com.dalongdemo.serverroot;
?
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
?
import java.util.HashMap;
import java.util.Map;
?
@SpringBootApplication
@RestController
public class ServerrootApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ServerrootApplication.class);
}
?
public static void main(String[] args) {
SpringApplication.run(ServerrootApplication.class, args);
}
@RequestMapping(value = "/userlogin")
public Object userLogin(){
Map<String,String> userinfos = new HashMap<>();
userinfos.put("name","dalong");
return userinfos;
}
}
以上是一些自己的实践
spring boot rest api 最好添加servlet.context-path
原文:https://www.cnblogs.com/rongfengliang/p/13189459.html