Why Spring Cloud Contract with Reactive Webflux mandate EXPLICIT testmode - spring-webflux

We have an option of using RestAssuredMockMVC or RestAssured while writing cloud contracts for Spring MVC. By RestAssuredMockMVC we can mock the controller and no need of starting the context. And via RestAssured we can use the direct call the controller via physical API using the testmode =EXPLICIT.
But, while using Reactive Webflux, we have been limited to RestAssured only and we have to make context up and running.
Why can't we have an option of mocking the controller here as well?

We will, there's an open issue for that to start using WebTestClient - https://github.com/spring-cloud/spring-cloud-contract/issues/422 . If you're interested in the feature go ahead add +1 and / or file a PR :)

Related

Can spring webclient be used with RESTful API which is not reactive based?

Is there any issues with using WebClient for fetching responses from multiple REST APIs which are not reactive based.
Yes, you definitely can, and in fact, the use of WebClient is encouraged by Spring officially. Also, the RestTemplate which is generally the preferred way of communication for REST-based services has already been deprecated.
Also, there are no issues when using WebClient in a non-reactive context
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/web.html#webmvc-client
However, you'll explicitly have to call subscribe or block when using WebClient in a non-reactive context.

How to use request and session scopes in spring-webflux (as of latest releases)

I am developing a rest web service using reactive programming through spring-webflux(Spring Boot 2.1 and Spring Framework 5.1). I need to create components having request level scope. #Scope annotation is suggested for spring MVC applications. But I found that the same doesn't work with webflux application.
Is there an equivalent feature available in webflux, as of latest release?
If not, what shall be the workaround here to create a new instance of a component on every incoming request?
I am trying to avoid use of new operator.
Thank you for the suggestions.
Unfortunately you cannot use request scopes in spring-webflux like in spring MVC applications. The main reason being, they use ThreadLocals which cannot be used by spring-webflux as work can be done on any thread at any time.
Spring webflux uses project-reactor at its core. So you can use Reactor Context which allows you to share data in your reactive pipeline.

FF4J: REST endpoint as a feature store

I am currently looking at implementing feature toggles using ff4j for our application. We want to have a remote central config app which will hold all the features in it and the applications will talk to this central config app via REST to get the features. We will not be able to leverage Spring Cloud Config or Archaius for this purpose.
I went through the documentation and it seems there is a support for HttpClient (https://github.com/ff4j/ff4j/wiki/Store-Technologies#httpclient). But I couldn't find any sample for the same. Can someone please let me know if I can leverage this method to build my feature store from a REST endpoint. Also, I would appreciate if someone could point me to a sample of this.
This is a common pattern.
A component holds the Administration UI (console) and the REST API. You can call it the "Admin Component". For security reasons It may be the only component to have access to persistance unit (any of the 15 DB implementation available)
For the "admin component" HERE is sample using standAlone spring-bppt application using JDBC DB, and HERE you find a simple web application.
The REST API can be secured using credentials user/password and/or API Key. More information HERE
All microservices access the REST API as clients and request feature store. You will need the dependency ff4j-webapi-jersey2x or ff4j-webapi-jersey1x that hold the client http> Then you can define the store using :
FeatureStoreHttp storeHTT = new FeatureStoreHttp("http://localhost:9998/ff4j");
Warning : Please consider using cache to limit overhead introduce by accessing the REST API at each feature usage. More info on cache HERE

WebFlux web and webflux starter

I created a Spring boot 2.0.0.M7 project with the webflux starter because I want to use all the asynchronous and the non-blocking capabilities.
I added the server.servlet.context-path but it does not work if I don't add the web starter as well.
If I add both starters can I have issues with the non-blocking functionality?
I executed some stress tests with Gatling and I have received the same scores removing the web starter or adding it.
Any help with this?
If you add both spring-boot-starter-web and spring-boot-starter-webflux to your application, Spring Boot will configure it as a Spring MVC app.
This is intentional as many Spring MVC will get the webflux dependency to leverage the new WebClient in their MVC apps. Also, as of Spring Framework 5, Spring MVC knows how to handle a few cases with Flux at the controller level.
You can always force your choice like this:
SpringApplication app = new SpringApplication(MyApplication.class);
app.setWebApplicationType(WebApplicationType.REACTIVE);
app.run(...);
In your case, this is not about forcing a choice but rather using something that's not supported in WebFlux.
The server.servlet.context-path configuration property is Servlet-specific, so it won't work with WebFlux. Currently Spring Boot does not support war deployment nor multiple web contexts for WebFlux applications. So there's no point in offering such a property.
The runtime model difference between "Servlet-based" and Reactive runtime with Spring can be quite subtle, and I encourage you to watch a talk that describes those choices. The short answer is: if you're using Spring MVC with async types (DeferredResult, Flux or SseEmitter) things will be async but reading and writing will still be blocking.
Properly benchmarking that is quite hard, but the results you're seeing are somehow expected. Running locally server+client, no latency involved, looking at raw throughput - all of those constraints should not be in favour of the reactive model which has a concurrency cost. If anything, this benchmark shows that the reactive stack is quite optimized already, even for non-ideal use cases!

How to use Spring Rest Service with REST Component in Mule

Hi I am working with Mule Studio and I want to use Spring Rest Service with REST component in the Mule. So how can I access Spring REST features with REST Component I don't want to use Jersey way of creating REST service with Mule.
I just want to declare one REST controller with spring annotation that will automatically invoke.
Mules Rest Component is a Jersey implementation of JAX-RS which loads the resource classes so that they can be accesses as Rest URLs.
Spring Rest Controller is the Spring way of creating a Rest service which runs ona web-container.
If you want to run the SpringRestController based rest service on Mule you can package and deploy it directly on Mule standalone. Mule can run a web applicaiton as it contains an embedded Jetty container.
Unfortunately you cannot include a Spring RestController into the Mules's REST component(which is a Jersey implementation). Controller's purpose is not to serve as a component.
By the way in Mule also you just need to specify the annotations and provide the resource class to the REST component. Mule takes care of the rest.
Hope this helps.