Module io.inverno.mod.web.server


module io.inverno.mod.web.server

The Inverno framework Web server module provides a Web enabled HTTP1.x and HTTP/2 server.

It defines a complete APIs for request routing and the creation of REST APIs using a collection of annotations.

It defines the following sockets:

mediaTypeConverters (required)
the media type converters used to encode and decode payloads based on the content type of the request or the response
netService (required)
the net service used to create the HTTP server
reactor (required)
the reactor providing the event loops processing the requests
resourceService (required)
the resource service used to load static resources
configuration
the Web server module configuration
headerCodecs
custom header codecs used to decode specific headers
parameterConverter
a parameter converter used in Parameter instances to convert their values

It exposes the following beans:

configuration
the Web server module configuration
webServerBoot
the Web server boot used to initialize the Web server

A basic Web server exposing two routes with white labels error handlers can be started as follows:


 List<MediaTypeConverter<ByteBuf>> mediaTypeConverters = null;
 NetService netService = null;
 Reactor reactor = null;
 ResourceService resourceService = null;

 Application.run(new Server.Builder(mediaTypeConverters, netService, reactor, resourceService)
     .setConfiguration(WebServerConfigurationLoader.load(conf -> conf.http_server(http_conf -> http_conf.server_port(8080))))
 )
 .webServerBoot().webServer()                                                           // Initialize the Web server with an empty context factory
     .intercept().interceptor(new HttpAccessLogsInterceptor<>())                        // log 2xx HTTP requests
     .route()                                                                           // route /path/to/resource1 requests to "Resource 1"
         .path("/path/to/resource1")
         .method(Method.GET)
         .produce(MediaTypes.APPLICATION_JSON)
         .produce(MediaTypes.TEXT_PLAIN)
         .handler(exchange -> exchange.response().body().encoder().value("Resource 1"))
     .route()                                                                           // route /path/to/resource2 requests to "Resource 2"
         .path("/path/to/resource2")
         .method(Method.GET)
         .produce(MediaTypes.APPLICATION_JSON)
         .produce(MediaTypes.TEXT_PLAIN)
         .handler(exchange -> exchange.response().body().encoder().value("Resource 2"))
     .interceptError().interceptor(new HttpAccessLogsInterceptor<>())                   // log 4xx, 5xx HTTP requests
     .configureErrorRoutes(new WhiteLabelErrorRoutesConfigurer<>());                    // White label error routes
 

Web routes can also be declared in a declarative way using the @WebController as follows:


 @WebController( path = "/book" )
 @Bean( visibility = Bean.Visibility.PRIVATE )
 public class BookController {

     @WebRoute(method = Method.POST, consumes = MediaTypes.APPLICATION_JSON)
     void create(@Body Book book) {
         ...
     }

     @WebRoute(path = "/{id}", method = Method.PUT, consumes = MediaTypes.APPLICATION_JSON)
     void update(@PathParam String id, @Body Book book);

     @WebRoute(method = Method.GET, produces = MediaTypes.APPLICATION_JSON)
     Flux<Book> list();

     @WebRoute(path = "/{id}", method = Method.GET, produces = MediaTypes.APPLICATION_JSON)
     Mono<Book> get(@PathParam String id);

     @WebRoute(path = "/{id}", method = Method.DELETE)
     void delete(@PathParam String id);
 }
 

The Web compiler will generate corresponding route configurers at compile time.

Since:
1.0
Author:
Jeremy Kuhn