转载

Spring Boot API 多版本快速入门Demo

 1.多版本背景介绍

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

2.代码工程

pomx.ml

<?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

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

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

server:
  port: 8088

3.测试

1.url路径控制

多个类分别设置不同url例如
@RequestMapping("/v1/users")
public class HelloWorldOneController {
}
@RequestMapping("/v2/users")
public class HelloWorldTwoController {
}
  • http://127.0.0.1:8088/v1/users/hello
  • http://127.0.0.1:8088/v2/users/hello

2.请求参数控制

   @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;
    }
  • http://127.0.0.1:8088/users?version=1
  • http://127.0.0.1:8088/users?version=2

3.通过header控制

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

4.通过Media Type控制

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

4.引用

 
正文到此结束
Loading...