Giter Club home page Giter Club logo

custom-version-annotation's Introduction

Spring Version Handler Annotation

Java CI with Maven

Features

There are two end-point

  • GET /{version}/greeting
  • GET /{version}/greeting/{message}
  • POST /{version/greeting request body sample:
{
  "message": "Hello World"
}

There are two versions 1.0 and 2.0


Documentation

Step 1, create the class that will be versioned, then ensure to put annotation of @VersionHandler this interface accepts a String as such you can put value such as 1.0 and 2.0

Step 2, create sub method that are needs to do the processing, ensure to annotate them with@VersionGetHandler this tells that this method is for All HTTP Get methods.

Step 3, In your rest controller, you will have use VersionHandlerController that is a class with one method findVersionHandler which has the following signature:

public ResponseEntity findVersionHandler(String endpoint, String version, HttpMethod httpMethod, Object... args)

Parameters Description:

version This field denotes the use of version and matches @VersionHandler version value.

httpMethod This field denotes the use of HTTP Methods such as GET, POST, PUT, and it will be mapped to equivalent annotation such as @VersionGetHandler

endpoint This field is the last check to drill down to only one method, as such if there are more than one method, the annotation logic will not work.

Sample Code

File: Controller.java

@RestController
public class HomeController {

    @Autowired
    VersionHandlerController versionController;

    @GetMapping("${bean.get.endpoint}")
    public ResponseEntity getGreeting(@PathVariable String version){
        return versionController.findVersionHandler("${bean.get.endpoint}", version, HttpMethod.GET);
    }
}

As you can see there is a versionController that is designed to do the processing for you. You should always call findVersionHandler method in the versionController with the needed parameters.

In this example, I am passing the endpoint (its always good to put the enpoint in properties to ensure all the places where this endpoint is used - its consistent), followed by version that you will be either getting from path, query, header, or body. It should be following the format that is stated in the documentation above.

Finally, the HttpMethod again it should be also correct, as the method will be mappted to its equivalent Handler annotation.

*handler.java

@Component
@VersionHandler(version = "1.0")
public class HomeHandler {

    @VersionGetHandler(endpoint = "${bean.get.endpoint}")
    public String getGreeting(){
        return "Greeting from v1.0";
    }
}

First its needs to have @Component or else the logic would not be able to locate the bean. Secondly, it needs to have @VersionHandler annotation as it is the another important annotation that will help with narrowing the search for the particular method to execute.

After which, in the method, should be annotated with @VersionGetHandler or @VersionPostHandler based on HttpMethod and the endpoint should match the one stated in the controller abvoe.


Examples

For the endpoint /1.0/greeting the project will return Greeting from v1.0

For the endpoint /2.0/greeting the project will return Greeting from v2.0

For the endpoint /1.0/greeting/Hi from the project will return Hi from v1.0

For the endpoint /2.0/greeting/Hello from the project will return Hello from v2.0


Others

Pre-Commit Scripts

#!/bin/sh

echo "Precommiting script started"

mvn clean test

nohup mvn spring-boot:run &

sleep 10

cd karate/

mvn test -Dtest=testRunner

cd ..

kill $(lsof -ti:8081)

echo "Successfully Completed"

custom-version-annotation's People

Contributors

shah-smit avatar

Watchers

 avatar

custom-version-annotation's Issues

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.