spring-web模塊爲client和server兩端構建響應式web應用提供了低級別的基礎結構和http抽象概念。全部的公共api都將圍繞着Reactive Streams和Reactor創建和實現。html
server端支持下面兩個層次:java
HttpHandler and server adapters —http請求處理 Reactive Streams back pressure 的最基本、通用的api。react
WebHandler API — 稍高級別的,可是通常用在過濾器式鏈的server web api。git
每個http server都有一些處理http請求的api。HttpHandler 是一個處理簡單的經過一個方法來處理請求和響應的協議。它主要的目的是爲不一樣的server提供一個共用的、Reactive Streams based API。github
spring-web 模塊包含了對全部支持的server的適配器。下面的表顯示了已被使用的server APIs以及Reactive Streams的支持來源:web
Server name | Server API used | Reactive Streams support |
---|---|---|
Nettyspring |
Netty APIapache |
|
Undertowtomcat |
Undertow API |
spring-web: Undertow to Reactive Streams bridge |
Tomcat |
Servlet 3.1 non-blocking I/O; Tomcat API to read and write ByteBuffers vs byte[] |
spring-web: Servlet 3.1 non-blocking I/O to Reactive Streams bridge |
Jetty |
Servlet 3.1 non-blocking I/O; Jetty API to write ByteBuffers vs byte[] |
spring-web: Servlet 3.1 non-blocking I/O to Reactive Streams bridge |
Servlet 3.1 container |
Servlet 3.1 non-blocking I/O |
spring-web: Servlet 3.1 non-blocking I/O to Reactive Streams bridge |
這裏是必須的依賴, supported versions,以及每一個server的代碼片斷:
Server name | Group id | Artifact name |
---|---|---|
Reactor Netty |
io.projectreactor.ipc |
reactor-netty |
Undertow |
io.undertow |
undertow-core |
Tomcat |
org.apache.tomcat.embed |
tomcat-embed-core |
Jetty |
org.eclipse.jetty |
jetty-server, jetty-servlet |
Reactor Netty:
HttpHandler handler = ... ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); HttpServer.create(host, port).newHandler(adapter).block();
Undertow:
HttpHandler handler = ... UndertowHttpHandlerAdapter adapter = new UndertowHttpHandlerAdapter(handler); Undertow server = Undertow.builder().addHttpListener(port, host).setHandler(adapter).build(); server.start();
Tomcat:
HttpHandler handler = ... Servlet servlet = new TomcatHttpHandlerAdapter(handler); Tomcat server = new Tomcat(); File base = new File(System.getProperty("java.io.tmpdir")); Context rootContext = server.addContext("", base.getAbsolutePath()); Tomcat.addServlet(rootContext, "main", servlet); rootContext.addServletMappingDecoded("/", "main"); server.setHost(host); server.setPort(port); server.start();
Jetty:
HttpHandler handler = ... Servlet servlet = new JettyHttpHandlerAdapter(handler); Server server = new Server(); ServletContextHandler contextHandler = new ServletContextHandler(server, ""); contextHandler.addServlet(new ServletHolder(servlet), "/"); contextHandler.start(); ServerConnector connector = new ServerConnector(server); connector.setHost(host); connector.setPort(port); server.addConnector(connector); server.start();
你也能夠將經過包裝處理過將ServletHttpHandlerAdapter轉換成Servlet後的war包部署在servlet3.1容器中。
httphandler 是運行在不一樣server的基礎。而WebHandler API爲exception handlers (WebExceptionHandler), filters (WebFilter),當前處理器(WebHandler)提供了一個稍高級別的api。
全部的組件均可以工做在ServerWebExchange
--http request和response的容器,增長了request attributes, session attributes, access to form data, multipart data,以及更多。
這個處理鏈能夠和WebHttpHandlerBuilder放在一塊兒,當建立在一個能夠運行在server adapter 的HttpHandler上時。若是要增長單獨的組件或者指向ApplicationContext時就須要注意如下的檢測:
Bean name | Bean type | Count | Description |
---|---|---|---|
"webHandler" |
WebHandler |
1 |
Target handler after filters |
<any> |
WebFilter |
0..N |
Filters |
<any> |
WebExceptionHandler |
0..N |
Exception handlers after filter chain |
"webSessionManager" |
WebSessionManager |
0..1 |
Custom session manager; |
"serverCodecConfigurer" |
ServerCodecConfigurer |
0..1 |
Custom form and multipart data decoders; |
"localeContextResolver" |
LocaleContextResolver |
0..1 |
Custom resolver for |
spring-web 模塊提供了 HttpMessageReader 和 HttpMessageWriter 來編解碼Reactive Streams. 的 http請求和響應體。它創建在一個低級別的協議上從 spring-core:
DataBuffer — 抽象化的字節buffer — e.g. Netty ByteBuf
, java.nio.ByteBuffer
Encoder — 序列化一個對象流到數據buffer
Decoder — 反序列化一個數據buffer到對象流
基本的 Encoder 和 Decoder 實現存在於spring-core,可是spring-web增長了更多像XML,JSON以及其餘的格式。你能夠經過EncoderHttpMessageWriter
和 DecoderHttpMessageReader
來包裝encoder和decoder成reader和writer。這裏是附加的信息,web-only reader 和 writer 實現自server-sent事件、表單數據以及更多。
最後,ClientCodecConfigurer
和 ServerCodecConfigurer
能夠用來實例化 readers and writers 的列表。他們支持classpath檢測以及一個默認值隨着須要覆蓋貨替換默認值。