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 class Usercontroller {@ GetMapping ("") @JsonResponse public responseentity<user> Listuser () {//query users User user = new Us        ER ();        User.setid (1);        User.setname ("Leo");        User.setage ((short) 18);    Return Responseentity.ok (). build (user); } @PutMapping ("/{id}") Public responseentity<?> Putmethod (@PathVariable ("id") int ID, @RequestBody String body    {//update user return Responseentity.status (httpstatus.created). build ();        } @DeleteMapping ("/{id}") Public responseentity<?> DeleteMethod (@PathVariable int id) {//delete user    Return Responseentity.status (httpstatus.no_content). build (); } @PostMapping ("") Public responseentity<?> Postmethod (@RequestBody String body) {//Add user Jsono        Bject JSON = jsonobject.parseobject (body);        User user = new user (); USer.setid (Json.getintvalue ("id"));        User.setname (json.getstring ("name"));        User.setage (Json.getshortvalue ("Age"));    Return Responseentity.status (httpstatus.created). build (user); }}
Interceptor Example:
Public final class Corsinterceptor implements interceptor {@Override public boolean prehandle (Fullhttprequest reque        St, HttpResponse response) throws Exception {//Use Axios to send cookies, not available here *, need to use Web frontend address, 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 void Posthandle (Fullhttprequest request, HttpResponse response) throws Exception {} @Overr IDE public void Aftercompletion (Fullhttprequest request, HttpResponse response) {}}
Start the service:
    @Test    public void test() {        // 忽略指定url        WebServer.getIgnoreUrls().add("/favicon.ico");        // 全局异常处理        WebServer.setExceptionHandler(new ExceptionController());        // 设置监听端口号        WebServer server = new WebServer(2006);        // 设置Http最大内容长度(默认 为10M)        server.setMaxContentLength(1024 * 1024 * 50);        // 设置Controller所在包        server.setControllerBasePackage("org.leo.web.controller");        // 添加拦截器,按照添加的顺序执行。        // 跨域拦截器        server.addInterceptor(new CorsInterceptor(), "/不用拦截的url");        try {            server.start();        } catch (InterruptedException e) {            e.printStackTrace();        }    }

Source code and Instance 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.