阅读文档

This commit is contained in:
asahi
2025-03-10 20:50:50 +08:00
parent 142b4deb8a
commit d2e278dcce

View File

@@ -178,5 +178,101 @@ server.start();
### WebHandler API ### WebHandler API
`org.springframework.web.server` package基于`HttpHandler`构建提供了通用的Web API。Web Api由多个`WebException`, 多个`WebFilter`, 一个`WebHandler`组件构成组成了一个chain。 `org.springframework.web.server` package基于`HttpHandler`构建提供了通用的Web API。Web Api由多个`WebException`, 多个`WebFilter`, 一个`WebHandler`组件构成组成了一个chain。
相比于`HttpHandler`仅仅是对不同http server的抽象`WebHandler`提供了一个更加通用、更加广泛的功能集合:
- user sessions attributes
- request attributes
- resolved `Locale` or `Principal` for request
- abstractions for multipart data
#### bean types for WebHttpHandlerBuilder auto-detect
在spring上下文中`WebHttpHandlerBuilder`可以自动探测到如下类型的components:
<table class="tableblock frame-all grid-all stripes-odd stretch">
<colgroup>
<col style="width: 25%;">
<col style="width: 25%;">
<col style="width: 12.5%;">
<col style="width: 37.5%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Bean name</th>
<th class="tableblock halign-left valign-top">Bean type</th>
<th class="tableblock halign-left valign-top">Count</th>
<th class="tableblock halign-left valign-top">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">&lt;any&gt;</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>WebExceptionHandler</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">0..N</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Provide handling for exceptions from the chain of <code>WebFilter</code> instances and the target
<code>WebHandler</code>. For more details, see <a href="#webflux-exception-handler">Exceptions</a>.</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">&lt;any&gt;</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>WebFilter</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">0..N</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Apply interception style logic to before and after the rest of the filter chain and
the target <code>WebHandler</code>. For more details, see <a href="#webflux-filters">Filters</a>.</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>webHandler</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>WebHandler</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The handler for the request.</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>webSessionManager</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>WebSessionManager</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">0..1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The manager for <code>WebSession</code> instances exposed through a method on <code>ServerWebExchange</code>.
<code>DefaultWebSessionManager</code> by default.</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>serverCodecConfigurer</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>ServerCodecConfigurer</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">0..1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">For access to <code>HttpMessageReader</code> instances for parsing form data and multipart data that is then
exposed through methods on <code>ServerWebExchange</code>. <code>ServerCodecConfigurer.create()</code> by default.</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>localeContextResolver</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocaleContextResolver</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">0..1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">The resolver for <code>LocaleContext</code> exposed through a method on <code>ServerWebExchange</code>.
<code>AcceptHeaderLocaleContextResolver</code> by default.</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>forwardedHeaderTransformer</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>ForwardedHeaderTransformer</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">0..1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">For processing forwarded type headers, either by extracting and removing them or by removing them only.
Not used by default.</p></td>
</tr>
</tbody>
</table>
#### Form Data
`ServerWebExchange`将会向外暴露如下方法用于访问form data
```java
Mono<MultiValueMap<String, String>> getFormData();
```
`DefaultServerWebExchange`使用`HttpMessageReader`来对form dataapplication/x-www-form-urlencoded进行parse操作将formdata转化为`MultiValueMap`
#### Multipart Data
`ServerWebExchange`向外暴露如下方法用于访问multipart data。
```java
Mono<MultiValueMap<String, Part>> getMultipartData();
```
`DefaultServerWebExchange`将会使用`HttpMessageReader<MultiValueMap<String, Part>>`来对`multipart/form-data``multipart/mixed``multipart/related`数据进行转换,数据将会被转化为`MultiValueMap`类型。
#### Filter
`WebHandler API`中,可以使用`WebFilter`来实现拦截式的逻辑,当使用`Webflux Config`时,`WebFilter`的注可以通过将其注册为bean来实现。
对于`WebFilter`的优先级,欸可以通过使用`@Order`注解或实现`Ordered`接口来实现。
#### CORS
spring webflux支持通过@CORSLAI1