num (Netty URL Mapping) is a Java library that adds request mapping to the Netty library.
Add dependency to start using num library:
compile group: 'com.github.vanbv', name: 'num', version: '0.0.3', ext: 'jar'
Besides num library you need to add Netty library to the project.
To handle a request, you must create a handler class. This class will handle incoming http requests to the server. For that end you need to extends class AbstractHttpMappingHandler, create the method that returns FullHttpResponse and annotate it as:
@Get
@Post
@Put
@Delete
Only GET, POST, PUT and DELETE requests are supported now.
It is necessary to specify the URL-address in the value
parameter of the annotation. The method is called referring to URL-address.
@Get("/test/get")
public DefaultFullHttpResponse get() {
…
}
You can use annotations to pass parameters in methods:
@PathParam
- for path parameters
@Get("/test/get/{message}")
public DefaultFullHttpResponse get(@PathParam(value = "message") String message) {
…
}
@QueryParam
- for query parameters
@Get("/test/get")
public DefaultFullHttpResponse get(@QueryParam(value = "message") String message) {
…
}
@RequestBody
- forPOST
requests body. It is allowed to use only inPOST
requests.
@Post("test/post")
public DefaultFullHttpResponse post(@RequestBody Message message) String message) {
…
}
To use @RequestBody
, you must pass the implementation of theJsonParser
interface, which parses data from the request body, to the constructor of the handler class. The library already has a default implementation of JsonParserDefault
. The implementation uses a jackson
parser. Therefore, if you use this implementation you must add a dependency com.fasterxml.jackson.core:jackson-databind
to the project.
You can look at a demonstration of the use of num library in the project.
Licensed under the Apache License, Version 2.0.