D10004 - JSR311, @GET, @POST…Http Method

This tutorial will focus on different Http Method usage based on JSR311. Here are annotations of JSR311 for Http Method.

Zero system support all method mounting on routing system, the common usage of restful api contains GET, DELETE, POST, PUT methods.

Demo projects:

1. Method usage

Please refer following entire class source code for different usage of Http Method

package up.god.micro.method;

import io.vertx.up.annotations.EndPoint;

import javax.ws.rs.*;

@EndPoint
@Path("/api")
public class MethodUsage {

    @GET
    @Path("/method")
    public String sayGet() {
        return "Hi, HttpMethod = GET";
    }

    @POST
    @Path("/method")
    public String sayPost() {
        return "Hi, HttpMethod = POST";
    }

    @DELETE
    @Path("/method")
    public String sayDelete() {
        return "Hi, HttpMethod = DELETE";
    }

    @PUT
    @Path("/method")
    public String sayPut() {
        return "Hi, HttpMethod = PUT";
    }

    @OPTIONS
    @Path("/method")
    public String sayOptions() {
        return "Hi, HttpMethod = OPTIONS";
    }

    @PATCH
    @Path("/method")
    public String sayPatch() {
        return "Hi, HttpMethod = PATCH";
    }
}

Above example contains 6 http methods usage in zero system, once you have finished, you can start up zero.

2. Console

You should see following information in console when starting zero:

......
[ ZERO ] ( 6 Event ) The endpoint up.god.micro.method.MethodUsage scanned 6 events of Event, \
    will be mounted to routing system.
......
[ ZERO ] ( Uri Register ) "/api/method" has been deployed by ZeroHttpAgent, Options = Route... \
    ...methods:[PUT]]@685473710.
[ ZERO ] ( Uri Register ) "/api/method" has been deployed by ZeroHttpAgent, Options = Route... \
    ...methods:[PATCH]]@1243852804.
[ ZERO ] ( Uri Register ) "/api/method" has been deployed by ZeroHttpAgent, Options = Route... \
    ...methods:[OPTIONS]]@766024960.
[ ZERO ] ( Uri Register ) "/api/method" has been deployed by ZeroHttpAgent, Options = Route... \
    ...methods:[POST]]@675804188.
[ ZERO ] ( Uri Register ) "/api/method" has been deployed by ZeroHttpAgent, Options = Route... \
    ...methods:[DELETE]]@935640745.
[ ZERO ] ( Uri Register ) "/api/method" has been deployed by ZeroHttpAgent, Options = Route... \
    ...methods:[GET]]@1098033693.
......

You could see the suffix of each logging line for routing to describe the method. different method could be triggered by different Http method in request.

3. Testing

Once zero is up, you can test the same uri with different Http Method.

3.1. GET Request

URL: http://localhost:6083/api/method

Method: GET

Response:

{
    "data": "Hi, HttpMethod = GET"
}

3.2. POST Request

URL: http://localhost:6083/api/method

Method: POST

Response:

{
    "data": "Hi, HttpMethod = POST"
}

3.3. PUT Request

URL: http://localhost:6083/api/method

Method: PUT

Response:

{
    "data": "Hi, HttpMethod = PUT"
}

3.4. DELETE Request

URL: http://localhost:6083/api/method

Method: DELETE

Response:

{
    "data": "Hi, HttpMethod = DELETE"
}

3.5. OPTIONS Request

URL: http://localhost:6083/api/method

Method: OPTIONS

Response:

{
    "data": "Hi, HttpMethod = OPTIONS"
}

3.6. PATCH Request

URL: http://localhost:6083/api/method

Method: PATCH

Response:

{
    "data": "Hi, HttpMethod = PATCH"
}

4. Summary

Current tutorial described different http method usage in zero system based on JSR311, JSR311 only support 7 annotations for GET, POST, DELETE, OPTIONS, PATCH, PUT, HEAD. They are all supported by zero system.