简体   繁体   中英

Alternatives for HttpServletRequest and HttpServletResponse in Micronaut

I am playing with Micronaut and what I currently miss is access to HttpServletRequest and HttpServletResponse . These normally allow to access things like:

  • request parameters
  • input/output stream (especially writing directly to output stream)
  • getting/setting cookies
  • getting/setting headers
  • getting client IP

Also I am not sure about alternatives for:

  • @RequestParam files: List<MultipartFile>
  • @RequestBody myClass: MyClass

https://docs.micronaut.io/latest/guide/index.html#requestResponse and https://docs.micronaut.io/latest/guide/index.html#binding shows how to bind to request parameters, cookies, headers, etc.

https://docs.micronaut.io/latest/guide/index.html#uploads shows how to handle file uploads.

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

Micronaut does things differently so you don't have access to a stream to write to. You can return a reactive type to have your data pushed to the response as soon as it's available.

getting client IP

Typically available via the host header or https://docs.micronaut.io/latest/api/io/micronaut/http/HttpRequest.html#getRemoteAddress--

Edit: Sending an XML file chunked

@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);
}

You can use a Filter for using 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM