zoukankan      html  css  js  c++  java
  • 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;
        }
    }

    说明

    以上是一些自己的实践

  • 相关阅读:
    169. Majority Element求众数
    567. Permutation in String字符串的排列(效率待提高)
    51. N-Queens N皇后
    52. N-Queens II N皇后II
    layui的form.val无法动态渲染赋值表单问题解决方法
    layui 父页面获取弹窗传递的值 和 父页面传值给子弹窗的方法
    layui搜索框,监听为空,表格重新加载
    layui form表单 input输入框获取焦点后 阻止Enter回车自动提交
    5月17日 日期格式遇到一些问题
    Layui关闭弹出层并刷新父窗口
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/13189459.html
Copyright © 2011-2022 走看看