How to make Spring Data REST endpoints asynchronous? - spring

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

Related

How to call Async API in Spring Boot using RestTemplate?

I am in a need of calling a api asynchronously in my spring boot application? Is there any methods that we can used in Spring RestTemplate?
RestTemplate may not be a good idea. As per documentation, AsyncRestClient exposes similar methods as RestTemplate, but returns ListenableFuture wrappers as opposed to concrete results. Also, WebClient would be a much better choice over AsyncRestClient which is deprecated. As the linked page shows it is
Deprecated. as of Spring 5.0, in favor of WebClient

Spring boot reactive WebClient calling legacy endpoint

In a Spring Boot (2.2.2.RELEASE) application, I have reactive endpoints (returning Mono or Flux), each of them is using reactive WebClient for calling another service. This "other" service is legacy (non-reactive) one.
Here is my question:
Is there a benefit of using Webflux (reactive WebClient) if my reactive endpoint is calling this non-reactive endpoint which does blocking stuff?
Is my reactive endpoint still reactive?
If we're talking about HTTP endpoints, we can call them with blocking or non-blocking (asynchronous) clients, but not fully reactive.
If your "new" application is reactive, you have to use non-blocking client (WebClient in your case), otherwise you will block NIO-threads and loose all the advantages of the reactive approach. The fact that the “other” application is blocking doesn't matter, you can still get a less resource-intensive "new" application.
They are
1. Not fully.
2. Your request is not full reactive until you change legacy APIs
Explanation:
End-to-End Reactive pattern only help into to the performance side
Currently you’re using reactive client this helps to connect to server in two way communication.
First set of APIs are reactive so web server layer is now reactive but data layer (Legacy APIs ) not reactive

Spring Reactor Web Client use case. replacing RestTemplate with WebClient

I am working on a microservice project where my individual spring boot microservices would be calling themselves and mainly to 3rd party API's to fetch and save data.
Since I am with legacy Spring boot application I can't think of replacing it with Reactor based Microservices.
But I am thinking of replacing My RestTemplate (using for communication with Other MS's and 3rd Party App) with new Spring Reactor Webclient to get some advantages of Async calls.
Is My use case a right candidate for Using Spring reactor WebClient?
Yes, composition of microservice and REST calls is a good use case for WebClient.
Plus Spring Boot 2 will let you combine the Spring MVC starter with the WebFlux starter and interpret that as "you want to run on the servlet stack, but might want to use WebClient sporadically".

Integrating spring Websession with spring reactive web flux

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.

Spring annotation for REST layer

I'm developing a RESTful service using ApacheCXF. I'm using Spring to inject the bean at each layer. I've three layer - REST layer, Service layer (Business logic layer) and DAO layer. I understand that we can annotate Service layer with #Service and DAO layer with #Repository but how do we annotate Rest class ? Do you suggest to annotate it with #Controller ? I've seen many examples where Rest class is annotated as #Controller if you're developing REST using Spring MVC. IMO, Spring MVC comes into play if you have deal with presentation layer as well (I may be wrong, don't have much idea about it) but this is just a web service which is hosted on one server to consume some data by some other application. I've not used Spring MVC in my past, but when would you suggest to develop REST services using Spring MVC? What's the benefit ?
If you're already using Spring, then Spring MVC is the way to write a RESTful service.
Prior to Spring 3, Spring MVC was very much focussed on traditional model-view-controller web apps that typically returned HTML to a web browser. Spring 3 added support for building RESTful services using Spring #Controllers typically configured to return JSON or XML payloads.
Rather than rehashing what's already been written, this blog post is a good introduction to the REST support that was added in Spring 3 and outlines a number of the benefits.

Resources