Giter Site home page Giter Site logo

restversioning's Introduction

#Spring REST versioning extension (POC)

This is a Spring extension that allows routing to different methods depending on the version of the resource that is requested. This code is based on the suggestions from a Stack Overflow question I asked - How to manage REST API versioning with spring?.

##Approaches to manage versions There are 2 main approaches to manage versions. One is to make the version explicit as part of the URL (e.g. http://host.com/1.1/resource), but this goes against the purist view that a resource identifier, should be the same regardless of the representation. The more correct approach is to use the Accept header to differentiate the representation of the resource (version and format), for example calling http://host.com/resource with an Accept header with the value application/vnd.app.resource-v1+json. The header mentions both the version and format expected.

Another aspect of versioning is whether to version each individual endpoint with a different version, or to version the whole API (similar to what is done in programming APIs, in which when a new version is release, not all classes have changed).

This approach favours the idea that an API release increases the version on all endpoints (there's no need to change annotation on all endpoints, but only on the endpoints that require the version increase). This removes the need from clients to know exactly what version of each endpoint to call. This approach also allows, for more advanced clients, to cherrypick which version of each endpoint to use.

Code

Here's an example of how a controller looks like with this extension

@Controller
@VersionedResource(media = "application/vnd.app.resource")
public class TestController {

    @RequestMapping(value = {"/resource"}, method = RequestMethod.GET)
    @VersionedResource(from = "1.0", to = "1.0")
    @ResponseBody
    public Resource getResource_v1() {
        return new Resource("1.0");
    }

    @RequestMapping(value = {"/resource"}, method = RequestMethod.GET)
    @VersionedResource(from = "2.0")
    @ResponseBody
    public Resource getResource_v2_onwards() {
        return new Resource("2.0");
    }
}

There are 2 main classes that used to extend spring CustomRequestMappingHandlerMapping and VersionedResourceRequestCondition. CustomRequestMappingHandlerMapping is the entry point of this extension and allows to to extend the default RequestMappingHandlerMapping in order to add a custom RequestCondition. VersionedResourceRequestCondition is responsible for figuring out if a controller method is a candidate to be invoked.

##Drawbacks of this implementation

  1. Given that we need a custom RequestMappingHandlerMapping, instantiating it is non-trivial (see the WebConfiguration class) and there's a danger that this class will change in a follow up version of spring and might cause unexpected behaviour.
  2. This implementation doesn't check for a version upper boundary in the @VersionedResource and also routes higher, non-existent versions to methods without a version upper boundary (see TestControllerTest.shouldReturnUnboundedVersionForCallToVersion32).

##When is this approach a bad idea? I think using versions on the URLs for public APIs is a better approach as the users might not be familiar with HTTP (as an example Twitter and Facebook put the version on the URL). In these cases, making the API as simple as possible to use is quite important.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.