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.引用

相关推荐
珹洺1 小时前
C++从入门到实战(十)类和对象(最终部分)static成员,内部类,匿名对象与对象拷贝时的编译器优化详解
java·数据结构·c++·redis·后端·算法·链表
一 乐1 小时前
网红酒店|基于java+vue的网红酒店预定系统(源码+数据库+文档)
java·开发语言·数据库·毕业设计·论文·springboot·网红酒店预定系统
xyliiiiiL3 小时前
从责任链模式聊到aware接口
java·开发语言
码农老起6 小时前
与Aspose.pdf类似的jar库分享
java·pdf·jar
程序猿小D6 小时前
第三百八十九节 JavaFX教程 - JavaFX WebEngine
java·eclipse·intellij-idea·vr·javafx
self-discipline6348 小时前
【Java】Java核心知识点与相应面试技巧(七)——类与对象(二)
java·开发语言·面试
潘多编程8 小时前
SpringBoot分布式项目订单管理实战:Mybatis最佳实践全解
spring boot·分布式·mybatis
wei3872452328 小时前
java笔记02
java·开发语言·笔记
qq_485015218 小时前
Spring Boot 非web应用程序
spring boot
zjj5879 小时前
Docker使用ubuntu
java·docker·eureka