Micronaut 中 HttpServletRequest 和 HttpServletResponse 的替代方案

Alternatives for HttpServletRequest and HttpServletResponse in Micronaut

我正在玩 Micronaut,我目前想念 HttpServletRequestHttpServletResponse。这些通常允许访问以下内容:

我也不确定以下的替代方案:

@RequestBody -> @Body (https://docs.micronaut.io/latest/guide/index.html#bodyAnnotation)

@RequestParam -> @QueryValue

https://micronaut-projects.github.io/micronaut-spring/latest/guide/index.html

https://docs.micronaut.io/latest/guide/index.html#requestResponse and https://docs.micronaut.io/latest/guide/index.html#binding 显示如何绑定到请求参数、cookie、headers 等

https://docs.micronaut.io/latest/guide/index.html#uploads 展示了如何处理文件上传。

input/output stream (especially writing directly to output stream)

Micronaut 做事的方式不同,因此您无权访问要写入的流。您可以 return 一种反应类型,以便在数据可用时立即将其推送到响应中。

getting client IP

通常可通过主机 header 或 https://docs.micronaut.io/latest/api/io/micronaut/http/HttpRequest.html#getRemoteAddress--

编辑:发送 XML 分块文件

@Get(uri = "/xml", produces = MediaType.TEXT_XML)
Flowable<String> getXml() {
    return Flowable.create(emitter -> {
        emitter.onNext("<<xml header>>");
        //do some work
        emitter.onNext("more xml");
        emitter.onNext("<<xml footer>>");
    }, BackpressureStrategy.BUFFER);
}

您可以使用 Filter 来使用 HTTP request/response

@Singleton
public class TraceService {

Flowable<Boolean> yourFilter(HttpRequest<?> request) {

The Micronaut HTTP server supports the ability to apply filters to request/response processing in a similar, but reactive, way to Servlet filters in traditional Java applications.

Filters provide the ability to support the following use cases:

Decoration of the incoming HttpRequest

Modification of the outgoing HttpResponse