Spring Cloud Stream - use spring cloud function("Function" specifically) to expose as rest endpoint and publish to a topic - spring

With Spring Cloud Stream - is it possible to use spring cloud function("function" specifically) to expose as rest endpoint and publish to a topic - something like below-
#Bean
public Function<String,String> postLoginEvent() {
return valFrmRest -> valFrmRest;
}
Currently if I try this, the endpoint isn't exposed and I get 404. As a workaround, I used an EmitterProcessor to get input from rest endpoint and separately have a supplier return this processor to publish to a topic. Not sure if what I am asking is technically possible, but it might be a pretty common usecase and it will be nice to have out-of-the-box functionality available. Any thoughts? Thank you in advance for sharing your inputs.

You can have it as a REST endpoint and then use StreamBridge API to publish it programmatically. See the details here.
Here is a sample using StreamBridge.

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

Using Spring Integration Router with Spring Cloud Stream

I have been trying to use the Spring Integration's #Router with Spring Cloud Stream with Kafka binding. My understanding is that when you return a List of channel names from the method annotated with #Router they should be produced to the destined Kafka topics. But I don't see the messages being produced.
Does Spring Integration's #Router work with Spring Cloud Stream. If not, what's the alternative and how do I programmatically route to channels selected at runtime?
What makes you believe that Spring Integration #Router has any effect in Spring Cloud Stream? It's a completely different framework.
Yes there are mechanisms to route FROM and TO in Spring Cloud Stream and they are described here.
I think the specific section of interest to your use case is Routing FROM, but consider reading the full section to understand the differences and mechanisms used.

How to redirect user request to other Restful API that runs on other server in Spring boot?

I am a noob in spring boot. I am writing a Gateway for some services. In a condition, I need to forward user request to other services (some Restful API) after authentication. I have done some search on 'forward' and 'redirect'. I think I need 'forward'. But I still have some questions: 1. when I forward it to other URI(eg. abc.dce.com/service/), does the service get the request body. 2.How can I do it in spring boot? Do you guys have a good example that fit my condition? (I admit that I am kind of lazy for this, but there are really many style of forward that confused me.)
//I find this example, but this is forwarding to service in same package //under same Internet.
#Override
public void addViewControllers(ViewControllerRegistry registry) {
// forward requests to /admin and /user to their index.html
registry.addViewController("/portal").setViewName(
"forward:/app/index.html");
}
Since you mention you're new to spring boot, you might want to take a look at spring project that implements a fully-featured gateway. I've used an earlier version of it (zuul) and the current spring-cloud-gateway allows you to implement a complete gateway easily by creating a spring-boot project and configuring. It has a lot of features you'll likely want to implement as a gateway (like adding/removing headers, modifying payloads,..). If you need features they don't support, you can implement via filters and other interfaces they provide. This was initially opensourced from Netflix so it is fairly comprehensive.
https://spring.io/projects/spring-cloud-gateway
Sample project:
https://github.com/spring-cloud-samples/spring-cloud-gateway-sample

mimic swagger api on spring boot application

I got a Spring boot REST based proxy server application with standard REST End point exposed(GET, POST,PUT and DELETE) to outside world.
Any requests coming from external world then will be routed to actual functionality internally.
Now the requirement is to expose all the API supported by my REST proxy server. As I mentioned I do not have static controller path in my code for individual APIs. So would like to get your input how to create swagger support for all the internal APIs that my proxy server support. Is there are a way I can generate swagger schema manually to mimics the controller path? Or is there any other way to solve this problem?
Thanks in advance.

how to make spring mvc functions available for rest calls

I have a spring mvc application which runs correctly,now another colleague wants to call the same functions from another application but he needs REST URL of my functions.
how is it possible to provide the same functionality through spring REST?
is it just with new annotations .please provide some resource to show me how to do it.
when server has a service, only legal clients which had any contracts with server can access it. And clients can use service by the way such as: use RestTemplate to get/post request to URL of service, and clients can get data as JSON, or XML type if you have an equivalent object as this image:
Also, a service can be support as a interface, ex: google search is a service supported by google, but it's not rest service.
If you know each other URL address you can consume each other REST API from java code by using RestTemplate object.
I would advise you to go over the Spring starter guide which deals with that issue, here is the link (Consuming a RESTful Web Service):
https://spring.io/guides/gs/consuming-rest/

Resources