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;
    }
}

说明

以上是一些自己的实践

posted on   荣锋亮  阅读(2520)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2018-06-24 nsq 安装试用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示