In the work with Netty do a few services, feel Netty do good program performance, less resources, but the implementation of HTTP service is more troublesome, so reference spring MVC annotations based on Netty implementation of a lightweight restful framework.
The framework provides functions such as controller annotations, global exception controllers, interceptors, and so on.
Note names refer to spring MVC, compilation comprehension and memory, mainly including the following annotations:
@RestController
@RequestMapping
@GetMapping
@PostMapping
@DeleteMapping
@PutMapping
@PatchMapping
@JsonResponse
@RequestParam
@PathVariable
@RequestBody
@UploadFile
@UrlEncodedForm
@RequestHeader
Controller example:
//The default is singleton, Singleton = False to enable multiple cases. //@RestController (singleton = false)@RestController @requestmapping ("/users") Public classUsercontroller {@GetMapping ("") @JsonResponse PublicResponseentity<user>Listuser () {//Query UsersUser User =NewUser (); User.setid (1); User.setname ("Leo"); User.setage (( Short) 18); returnResponseentity.ok (). build (user); } @PutMapping ("/{id}") PublicResponseentity<?> Putmethod (@PathVariable ("id")intID, @RequestBody String body) { //Update User returnresponseentity.status (httpstatus.created). build (); } @DeleteMapping ("/{id}") PublicResponseentity<?> DeleteMethod (@PathVariableintID) {//Delete User returnresponseentity.status (httpstatus.no_content). build (); } @PostMapping ("") PublicResponseentity<?>Postmethod (@RequestBody String body) {//Add UserJsonobject JSON =Jsonobject.parseobject (body); User User=NewUser (); User.setid (Json.getintvalue ("id")); User.setname (Json.getstring ("Name")); User.setage (Json.getshortvalue ("Age")); returnresponseentity.status (httpstatus.created). build (user); }}
Interceptor Example:
Public Final classCorsinterceptorImplementsInterceptor {@Override Public BooleanPrehandle (fullhttprequest request, HttpResponse response)throwsException {//use Axios to send cookies, which are not available here * and need to use Web frontend addresses such as:http://localhost: 8080//response.getheaders (). Put ("Access-control-allow-origin", "*");Response.getheaders (). Put ("Access-control-allow-origin", System.getproperty ("Http.origin")); Response.getheaders (). Put ("Access-control-allow-methods", "POST, PUT, GET, OPTIONS, DELETE, PATCH"); Response.getheaders (). Put ("Access-control-max-age", "3600"); Response.getheaders (). Put ("Access-control-allow-headers", "Content-type,x-token"); Response.getheaders (). Put ("Access-control-allow-credentials", "true"); return true; } @Override Public voidPosthandle (fullhttprequest request, HttpResponse response)throwsException {} @Override Public voidaftercompletion (fullhttprequest request, HttpResponse response) {}}
Start the service
@Test Public voidTest () {//Ignore specified URLWebserver.getignoreurls (). Add ("/favicon.ico"); //Global Exception HandlingWebserver.setexceptionhandler (NewExceptioncontroller ()); //set the listening port numberWebServer Server =NewWebServer (2006); //set HTTP maximum content length (default is 10M)Server.setmaxcontentlength (1024 * 1024 * 50); //set up the controller packageServer.setcontrollerbasepackage ("Org.leo.web.controller"); //add interceptors, which are executed in the order added. //cross-domain interceptorsServer.addinterceptor (NewCorsinterceptor (), "/No URL to intercept"); Try{Server.start (); } Catch(interruptedexception e) {e.printstacktrace (); } }
Source Code and sample program
Typical applications
A restful framework based on Netty implementation--netty-rest-server