Integrating spring Websession with spring reactive web flux - spring

There is a new implementation of http session for spring new reactive web flux api located here.
I would like to integrate the latest spring web session in the new spring reactive web flux. I can't seem to get it, I tried injecting it as a bean, but it does not work. I would like to inject it like I usually do with HttpSession
something like
#Autowired
Websession webSession;

Because Spring WebFlux is a reactive web framework, you can't expect the Web Session to be injected as a bean (even in the request scope). In the Servlet world, each request/response is processed in a single thread, which enables those approaches (i.e. the "request" scope). With WebFlux, a given request can be processed by multiple threads.
The WebSession instance associated with the current request/response is actually attached to the ServerWebExchange (see getSession). Because of the nature of the reactive programming model, you're very likely to access that session within a Reactor operator - so you can't expect to inject this instance somewhere else in your application.

Related

calling a rest endpoint in a springboot application

which is better alternative for calling REST endpoint in springboot application, calling REST endpoints using WebClient or calling REST endpoints using RestTemplate ?
Spring’s documentation recommends using WebClient, but that’s only a valid recommendation for reactive apps. If you aren’t writing a reactive app, use OpenFeign instead. Like anything else in software, it fits well for some cases, but might complicate things for others. Choosing WebClient to implement the REST endpoint calls is strongly coupled to making your app reactive
RestTemplate gives many advantages if you are using it from within Springboot application, i.e. in your server side to another part of your own app - sort of like an internal call. Because the RestTemplate "knows" all your entities and beans and so if you need to send over or receive an object which is known within your springboot application RestTemplate can map them automatically which is a very nice advantage. If you sending a request to some third party api and do not pass or receive your known entities RestTemplate is still a valid option but it just becomes just another Http client. Its just simply there as part of Springboot provided tools. But in this case you may use any other client as well.

How to organize multithreaded access via the rest api to a resource using Spring Boot

I would want organize multithreaded access via the rest api to a resource using Spring Boot.
There is information (I found it here) that SrpingBoot can parallelize requests (which I doubt) to the controller with scope-singlotone.
How to start the design of such a controller, or what approach to apply for this?
Spring handles requests in parallel - that means your singleton controllers have to be thread-safe.
If you have a single resource which cannot handle parallel access you have to use Java's synchronized or locks to serialize the access.
Spring Rest Controllers do handle request using a thread pool. Since Spring beans are singleton, your Spring Beans should be Stateless or we can say bean should have a sheared state. Which means you cannot have a state that will change with the time.

How to make Spring Data REST endpoints asynchronous?

Does anyone know how to make Spring Data REST endpoints asynchronous?
I saw that we can add the annotation #Async with CompletableFuture<?> as returned object on the service methods.
But doing this, makes the use of the interface RepositoryRestResource pointless as we need to implement both the service and controller layers manually...
Or am I missing something here?
Currently Spring Data REST supports blocking I/O only. See this Jira Issue of Spring Data REST support for Spring WebFlux.
Spring MVC has an integration with Servlet 3.0 async request processing.
Although Spring MVC has async support, for non-blocking I/O, Spring WebFlux is recommended, because Spring WebFlux is async by design. See Spring Web MVC Async Request Compared to WebFlux

How to Return Flux as response when using Spring Reactor and Spring Boot?

I am trying to use Spring Reactor with my Spring Boot application.
I am using Project Reactor 3.0.7.RELEASE and Spring Boot 1.5.3.RELEASE.
I have a method in my Service class which returns Flux.
I want to return the value to controller in web layer. But, I do not see the values returns in the json response.
When I invoke http://localhost:8080 from browser, I get response as,
{"prefetch":-1}
I am not sure if I should I do some conversion from Flux to String before returning the response.
I have my code shared in ,
https://github.com/bsridhar123/spring-reactor-demo/blob/master/src/main/java/com/demo/reactor/ReactiveApp.java
Can you please help me on the correct approach for it.
Full reactive support using Reactor is only implemented in Spring 5 (currently in RC phase) and Spring Boot 2 (currently in Milestone phase).
You can use Reactor independently of Spring, but that doesn't make the framework reactive and asynchronous so you lose some benefit. You cannot simply return a Flux as the framework doesn't yet understand this type.
However, I believe it can still be useful in the service layer, if you have to orchestrate a lot of service calls.
What you can do in this case is to use Flux and Mono throughout your service layer and convert those to Spring 4's DeferredResult. Something like:
DeferredResult<ResponseEntity<String>> result = new DeferredResult<>();
service.getSomeStringMono()
.map(n -> ResponseEntity.ok(n))
.defaultIfEmpty(ResponseEntity.notFound().build()
.subscribe(re -> result.setResult(re),
error -> result.setErrorResult(error));
return result;

How to access request object in #MessageMapping method with spring websocket implementation

I am integrating an existing spring MVC web application with spring websockets. I was successfully able to integrate by following the instructions in
https://spring.io/guides/gs/messaging-stomp-websocket/
The existing web application has a filter, which sets a few of the attributes. I have a requirement to access the attributes set by the filter in the controller i,e in #MessageMapping method.
Could some one tel how can we access the request object in the #MessageMapping method?
When a STOMP client connects to the application, it first has to request a protocol upgrade to switch to websocket. Once using that websocket connection, the messages sent/received don't go through your regular Servlet filter - only the first HTTP request (the "Handshake") did.
Depending on your use case, there are several ways to achieve this.
If it's related to Authentication, then there are existing features for this in the Spring Framework, but also in Spring Security.
If it's related to the HTTP session, you can easily ask for all HTTP session attributes to be copied into the websocket session - or even customize the Handshake for your own needs (see reference doc). Once done, you can inject the Websocket scope in a #MessageMapping controller method and get those attributes (see reference doc).

Resources