A restful framework based on Netty implementation--netty-rest-server

Source: Internet
Author: User
Tags send cookies

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.