SpringBoot API 多版本快速入门Demo

1.多版本背景介绍

在以SpringBoot开发Restful接口时,由于模块,系统等业务的变化,需要对同一接口提供不同版本的参数实现(老的接口还有模块或者系统在用,不能直接改,所以需要不同版本)。如何更加优雅的实现多版本接口呢?网上很多自定义版本注解的方法,但是我不太建议这样做,因为硬编码和增加系统的维护成本。而且springboot本身就很好的支持多版本,没有必要再去造轮子。

2.代码工程

pomx.ml

xml 复制代码
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>api-version</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>

controller

typescript 复制代码
package com.et.api.version.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class HelloWorldController {
    //@GetMapping(params = "version=1")
    //@GetMapping(produces = "application/vnd.company.app-v1+json")
    @GetMapping(headers = "API-Version=1")
    public Map<String, Object> showHelloWorldone(){
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "HelloWorld1");
        return map;
    }
    //@GetMapping(params = "version=2")
    //@GetMapping(produces = "application/vnd.company.app-v2+json")
    @GetMapping(headers = "API-Version=2")
    public Map<String, Object> showHelloWorldtwo(){
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "HelloWorld2");
        return map;
    }


}

package com.et.api.version.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/v1/users")
public class HelloWorldOneController {
    @RequestMapping("/hello")
    public Map<String, Object> showHelloWorld(){
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "HelloWorld1吖");
        return map;
    }



}

package com.et.api.version.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/v2/users")
public class HelloWorldTwoController {
    @RequestMapping("/hello")
    public Map<String, Object> showHelloWorld(){
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "HelloWorld2");
        return map;
    }



}

DemoApplication.java

typescript 复制代码
package com.et.api.version;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

application.yaml

yaml 复制代码
server:
  port: 8088

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

3.测试

1.url路径控制

多个类分别设置不同url例如

less 复制代码
@RequestMapping("/v1/users")
public class HelloWorldOneController {
}

@RequestMapping("/v2/users")
public class HelloWorldTwoController {
}

2.请求参数控制

typescript 复制代码
   @GetMapping(params = "version=1")
    public Map<String, Object> showHelloWorldone(){
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "HelloWorld1");
        return map;
    }
    @GetMapping(params = "version=2")
    public Map<String, Object> showHelloWorldtwo(){
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "HelloWorld2");
        return map;
    }

3.通过header控制

typescript 复制代码
    @GetMapping(headers = "API-Version=1")
    public Map<String, Object> showHelloWorldone(){
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "HelloWorld1");
        return map;
    }
   
  @GetMapping(headers = "API-Version=2")
    public Map<String, Object> showHelloWorldtwo(){
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "HelloWorld2");
        return map;
    }

4.通过Media Type控制

typescript 复制代码
@GetMapping(produces = "application/vnd.company.app-v1+json")
public Map<String, Object> showHelloWorldone(){
    Map<String, Object> map = new HashMap<>();
    map.put("msg", "HelloWorld1");
    return map;
}
@GetMapping(produces = "application/vnd.company.app-v2+json")
public Map<String, Object> showHelloWorldtwo(){
    Map<String, Object> map = new HashMap<>();
    map.put("msg", "HelloWorld2");
    return map;
}

4.引用

相关推荐
小张认为的测试13 分钟前
Liunx上Jenkins 持续集成 Java + Maven + TestNG + Allure + Rest-Assured 接口自动化项目
java·ci/cd·jenkins·maven·接口·testng
蘑菇丁42 分钟前
ansible批量生产kerberos票据,并批量分发到所有其他主机脚本
java·ide·eclipse
呼啦啦啦啦啦啦啦啦2 小时前
【Redis】持久化机制
java·redis·mybatis
我想学LINUX3 小时前
【2024年华为OD机试】 (A卷,100分)- 微服务的集成测试(JavaScript&Java & Python&C/C++)
java·c语言·javascript·python·华为od·微服务·集成测试
空の鱼7 小时前
java开发,IDEA转战VSCODE配置(mac)
java·vscode
!!!5258 小时前
日志技术-LogBack入门程序&Log配置文件&日志级别
spring boot
P7进阶路8 小时前
Tomcat异常日志中文乱码怎么解决
java·tomcat·firefox
小丁爱养花9 小时前
Spring MVC:HTTP 请求的参数传递2.0
java·后端·spring
CodeClimb9 小时前
【华为OD-E卷 - 第k个排列 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od