Java client for streaming Data with Spring Boot using StreamingResponseBody - spring

A spring rest endpoint to stream data seems to be programmed rather straigthforward using a StreamingResponseBody (example). Could anybody point me to a resource on how to write a client in java (another spring app)?

You must consume the data as any other web resource, for example with spring :
String value = new RestTemplate().getForObject("https://someuri.com", String.class);

Related

Spring Serverless HTTP Poller

I want to implement a serverless function that polls an HTTP endpoint and publishes the response to a messaging queue.
My initial thought is to build a spring boot application using spring integration gateway and adapters for HTTP polling and publishing to queue (and deploy as lambda). Is there a better option in the spring stack?
I looked at spring cloud function, spring cloud stream, spring cloud task. Any suggestions?
In Spring Cloud Stream this type of microservice is called source. So, you need to have a Supplier bean based on Spring Integration Java DSL to build a simple flow to let Spring Cloud Stream to poll it periodically and produce a result into bound destination.
Something like this:
#Bean
public IntegrationFlow pollingHttpFlow() {
return IntegrationFlows
.from(Supplier.class, gateway -> gateway.beanName("httpSupplier"))
.handle(Http.outboundGateway("http://somehost"))
.get();
}
See a blog post about this kind of interoperability: https://spring.io/blog/2019/10/25/spring-cloud-stream-and-spring-integration

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".

Using Gateway to consume Spring Boot application from Spring Integration application

I am just starting with Spring Integration with Spring Boot 2.
I am working on a application which uses Spring Integration's HTTP outbound gateway to consume a Spring boot Service.
I am consuming the spring boot service from Spring Integration application using Gateway.
When I call the Gateway method which in turn will use the outbound gateway to call the spring boot service, the request does not seem to be completed. It is just going on when I make the HTTP GET request via the browser.
The request is also not received by the Spring Boot service.
I am not able to identify what is wrong in my Integration application when using gateway to consume a Spring Boot 2 service.
I have shared my Spring Boot 2 Application and also the Integration application which I am using to consume it in the below github folder. it contains 2 folders, one for the spring Integration application and the other for the spring boot application.
https://github.com/gsamartian/spring-int-demos
I have exposed a REST interface for the Integration application using RestController.
I access the boot application from integration application via the url, http://localhost:8763/callExternalServiceViaGateway
I am able to access the spring boot application directly from its port.
If anyone can help me identify the cause it would be great.
Thanks,
Your problem that the gateway method is without any args:
#Gateway(requestChannel = "get.request.channel", replyChannel = "reply.channel")
String getCustomMessage();
In this case the gateway works as receiving. No any requests is send because nothing to wrap to the payload. See more info on the matter in the Reference Manual.
Right now I see several bugs with the payloadExpression and no arg, so I suggest you to add some String payload arg to the getCustomMessage() gateway method and perform it with an empty string.
Will take a look into the bugs and will fix them soon.
Thank you for a good sample how to catch and reproduce!
https://jira.spring.io/browse/INT-4448
https://jira.spring.io/browse/INT-4449

Spring integration and Microservice communication

I am pretty new to Spring Integration. I have an application written in spring integration which does a rest call to my microservice(spring boot application)
The request is a pure String and the request message(header+payload) has content type set as TEXT_PLAIN and my microservice's controller consumes as mediatype.PLAIN_TEXT_VALUE
When initiate a request to the microservice from the spring integration application it is throwing an error as "unsupported type"
Why does it throw error and what other alternative do i have?
I do not want or can't have JSON as request or response. It is just an unformatted string as input and a formatted string as output

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;

Resources