spring boot rest api 最好添加servlet.context-path


實際上這個並不是一個強制要求,而且如果基於spring cloud 等框架已經基於gateway 做了一層處理
但是還是推薦添加

幾個原因

  • servlet.context-path 類似一個gateway 聚合,因為我們很多時候api 是很多的,而且大家的RequestMapping 也是比較亂的,統一一個方便后期維護(問題排查,尤其是在有很多接口的時候)
  • 很多時候我們會基於nginx 做接口的lb,如果提供了唯一區分的servlet.context-path,我們可以更好的控制api 的請求規則(cache,auth,rewrite。。。)
  • 還是關於nginx 配置的,有了唯一區別,nginx 的location 不用寫那么多rewrite 規則了,同時也避免了多服務接口的重名問題
  • 可以方便的與tomcat 部署模式保持接口一致,同時我們也可以提供java -jar 模式運行war包

參考配置

  • application.properties
 
server.servlet.context-path=/users/
  • pom.xml
    兼容war 模式
 
<?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>
  • 入口
    ServerrootApplication.java
 
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;
    }
}

說明

以上是一些自己的實踐


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM