https://github.com/ganchix/versioning-spring-boot-starter
https://github.com/ganchix/versioning-spring-boot-starter
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ganchix/versioning-spring-boot-starter
- Owner: ganchix
- License: mit
- Created: 2020-04-24T09:38:47.000Z (about 5 years ago)
- Default Branch: develop
- Last Pushed: 2020-04-24T09:43:05.000Z (about 5 years ago)
- Last Synced: 2025-02-15T09:36:54.398Z (4 months ago)
- Language: Java
- Size: 60.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Spring boot starter Versioning [](https://travis-ci.org/github/ganchix/versioning-spring-boot-starter) [](https://codecov.io/gh/ganchix/versioning-spring-boot-starter) [](https://gitter.im/ganchix/versioning-spring-boot-starter?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [](https://maven-badges.herokuapp.com/maven-central/io.github.ganchix/versioning-spring-boot-parent) [](https://github.com/ganchix/versioning-spring-boot-starter)
Spring boot starter using for versioning rest easily.
# Table of Contents
- [Overview](#overview)
- [Getting started](#getting-started)
- [License](#license)### Overview
This implementation is based on the idea of this [StackOverflow](http://stackoverflow.com/questions/20198275/how-to-manage-rest-api-versioning-with-spring) thread.
For this approach we think that versioning with accept headers is the most RestFull option.
### Getting started
#### Code exampleAn example from the server side:
```java
import io.github.ganchix.microservice.starter.annotation.VersionedResource;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
@VersionedResource(media = "application/vnd.app.resource")
class TestController {
@RequestMapping
@VersionedResource(from = "1.0", to = "2.0")
public String testVersion1And2() {
return "version-1.0-2.0";
}
@RequestMapping
@VersionedResource(from = "3.0", to = "4.0")
public String testVersion3And4() {
return "version-3.0-4.0";
}
@RequestMapping
@VersionedResource(from = "6.0")
public String testVersion6ToInfinite() {
return "version-6-Infinite";
}
}
```An example from the client side:
```java
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
public class DemoClient {
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
//Version 1
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/vnd.app.resource-v1.0+json");
HttpEntity request = new HttpEntity<>(headers);
ResponseEntity response = restTemplate.exchange("http://localhost:8080/test", HttpMethod.GET, request, String.class);
if(!response.getBody().equals("version-1.0-2.0") ){
throw new RuntimeException("Error in version ");
}
//Version 3
headers = new HttpHeaders();
headers.add("Accept", "application/vnd.app.resource-v3.0+json");
request = new HttpEntity<>(headers);
response = restTemplate.exchange("http://localhost:8080/test", HttpMethod.GET, request, String.class);
if(!response.getBody().equals("version-3.0-4.0") ){
throw new RuntimeException("Error in version ");
}
}
}
```#### Integration using `@SpringBootApplication` or `@EnableAutoConfiguration`
Only add Maven dependency:
```xml
io.github.ganchix
versioning-spring-boot-starter
LATEST_VERSION```
#### Without `@SpringBootApplication` or `@EnableAutoConfiguration`
Add Maven dependency:
```xml
io.github.ganchix
versioning-spring-boot
LATEST_VERSION```
Configure manually `RequestMappingHandlerMapping` in your configuration
```java
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@Configuration
public class WebVersionAutoConfig extends WebMvcConfigurationSupport {
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
VersionRequestMappingHandlerMapping handlerMapping = new VersionRequestMappingHandlerMapping();
handlerMapping.setOrder(1);
return handlerMapping;
}
}
```### License
Spring boot starter versioning is licensed under the MIT License. See [LICENSE](LICENSE.md) for details.
Copyright (c) 2020 Rafael Ríos Moya.