Reputation: 12416
I am playing with Micronaut and what I currently miss is access to HttpServletRequest
and HttpServletResponse
. These normally allow to access things like:
Also I am not sure about alternatives for:
@RequestParam files: List<MultipartFile>
@RequestBody myClass: MyClass
Upvotes: 1
Views: 3616
Reputation: 56
Turns out I got stuck with this problem tonight but there's a way around it.
Create a generic Bean that stores a HttpHeaders property.
public class HttpHeaderHolder {
private HttpHeaders headers;
public HttpHeaderHolder() {
}
public HttpHeaders getHeaders() {
return headers;
}
public void setHeaders(HttpHeaders headers) {
this.headers = headers;
}
}
Once you have this, create a Factory that creates a RequestScoped bean of that class.
@Factory
public class HttpHeaderFactory {
@Bean
@RequestScope
public HttpHeaderHolder requestHeaders() {
return new HttpHeaderHolder();
}
}
Now, you can make a Filter that has this bean injected to which to can then inject the headers from the request.
@Filter("/**")
public class SomeFilter implements HttpFilter {
@Inject
private HttpHeaderHolder instance;
@Override
public Publisher<? extends HttpResponse<?>> doFilter(HttpRequest<?> request, FilterChain chain) {
instance.setHeaders(request.getHeaders());
return chain.proceed(request);
}
}
Now you have a bean you can inject into anywhere to get the headers for the current request. It seems to work because the filter will intercept the request and push the HttpHeaders into the bean before reaching the Controllers.
Upvotes: 0
Reputation: 12238
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);
}
Upvotes: 1
Reputation: 58882
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
Upvotes: 0
Reputation: 1538
@RequestBody -> @Body (https://docs.micronaut.io/latest/guide/index.html#bodyAnnotation)
@RequestParam -> @QueryValue
https://micronaut-projects.github.io/micronaut-spring/latest/guide/index.html
Upvotes: 1