Search code examples
javarestapache-camelesbapache-servicemix

API Version management in REST based service integrated with Camel


I could successfully create Camel based app which exposes set of web services using REST. (As of now I have used servlet based endpoint). In future, we may observe various changes and still support old version of API for sometime so got to support versioning.

In the past, we usually add param in URL to track API version of incoming request. I believe HTTP headers can also be used for the same purpose.

Now in camel world, how we can track version of API. (abja.com/v1/getOrders for example) ?

Just for curiosity, can cxfrs or restlet has in-built support?


Solution

  • Possibility 1: Define separate entry points for the different versions

    E.g. version 1:

    from("restlet:http://localhost:8080/1/users/{id}/like/{beer}")
        .process(new Processor() {
            public void process(Exchange exchange) {
                // version 1 processing
            }
        );
    

    E.g. version 2:

    from("restlet:http://localhost:8080/2/users/{id}/like/{beer}")
        .process(new Processor() {
            public void process(Exchange exchange) {
                // version 2 processing
            }
        );
    

    Possibility 2: Define one entry point and pass the version number to the processor

    E.g.:

    from("restlet:http://localhost:8080/{version}/users/{id}/like/{beer}")
        .process(new Processor() {
            public void process(Exchange exchange) {
                int version = exchange.getIn().getHeader("version", Integer.class);
                if (version == 1) {
                    // version 1 processing
                } else {
                    // version 2 processing
                }
            }
        );