Spring boot Rest service app issues with routing logic to mongo db - spring

I have to create api using spring boot application where the routing logic using apache camel and api should save to Mongo db please help if anyone has created such application.

You need to use rest component of apache camel
here is a sample app using apache camel and springboot
rest("/users").description("User REST service")
.consumes("application/json")
.produces("application/json")
.get().description("Find all users").outType(User[].class)
.responseMessage().code(200).message("All users successfully returned").endResponseMessage()
.route()
.to("bean:userService?method=findUsers")
.endRest()
in above camel example, i am creating a rest api for users, which will return list of user.

Related

How to build a Pub Sub application with RabbitMQ, Spring Boot and React

I'm trying to build an application with a couple of services. One service publishes details to a rabbitMQ queue, another spring boot application has to consume them and send the correct details to the correct user. The front end is developed in reactJS. How can I implement the spring boot consuming and sending to the subscribed user part? Any reference is appreciated. Can I use the web socket in spring boot to this?

Spring Boot Rest Controller vs Camel rest dsl

I'm developing a new application which is Spring Boot with camel. I am exposing REST endpoints as part of this application.
I'm little confused to choose between these two options:
Spring Rest Controller --> Spring service with producer template to call camel routes --> Camel routes for EIP
Camel Rest DSL --> Camel routes for EIP
Can you please help me to choose better option?
This is your call which should you want to implement, But as you are integrating camel in spring boot so you can take advantage of REST DSL camel components and bind that flow with other components of Apache Camel, it will reduce your additional work in spring boot app to send and receive data in your other camels routes.
Here is a sample CRUD rest operations using REST DSL of apache camel component.
rest("/users").description("User REST service")
.consumes("application/json")
.produces("application/json")
.get().description("Find all users").outType(User[].class)
.responseMessage().code(200).message("All users successfully returned").endResponseMessage()
.route()
.to("bean:userService?method=findUsers")
.endRest()
.get("/{id}").description("Find user by ID")
.outType(User.class)
.param().name("id").type(RestParamType.path).description("The ID of the user").dataType("integer").endParam()
.responseMessage().code(200).message("User successfully returned").endResponseMessage()
.route()
.to("bean:userService?method=findUser(${header.id})")
.endRest()
.post().description("Create a user").type(User.class)
.param().name("body").type(RestParamType.body).description("The user to create").endParam()
.responseMessage().code(204).message("User successfully created").endResponseMessage()
.to("bean:userService?method=create")
}
You can check the fully above sample app of spring boot and camel rest dsl from here

Difference between spring-boot-admin server and client

There are no much resources on this topic so thought to post it. Also I myself am trying to understand the different. Previously we just had only spring-boot application. So can anybody explain the difference.
Spring boot admin is nice monitoring dashboard for spring boot application. To feed data to this dashboard spring boot admin provide two approaches:
1) Using client library: client libary will send data to spring boot admin dashboard.
2) Use service discovery (eureka)

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

Camel Rest api consumer using SpringOAuthResttemplate

I have to invoke a couple of rest web service from my spring boot application. I am planning to use the Camel to configure the flow and other EIP use cases. Some of the endpoints are using oAuth2 authentication. I am planning to use the Spring oAuthResttempalte. All the examples on the internet are either using restlet, CXF or camel-http.
Camel Rest Consmer
I am not able to find a single example with just spring resttemplate. Did anyone implement Camel Rest consumer using Spring Resttemplate?
Some of the Examples on the internet use a jetty server to consume a rest endpoint. Why do you need a jetty server for simple rest consumer?
Did anyone implement Camel Rest consumer using Spring Resttemplate?
I'm not aware of that and it's unlikely to found something in that direction because Camel already have bult-in components to consume rest endpoints.
Some of the Examples on the internet use a jetty server to consume a rest endpoint. Why do you need a jetty server for simple rest consumer?
I believe that jetty was used as a consumer not a producer endpoint. So you won't need the "server". Or maybe you saw an example using jetty acting as a server to serve an OAuth endpoint?
If you excuse my approach, I'd suggest to remain with Camel HTTP/Rest capabilities to consume REST APIs using OAuth. I've found this example on Gist:
from("direct:authService").tracing()
.setHeader(Exchange.HTTP_PATH)
.simple("<auth service context>/oauth2/token")
.setHeader("CamelHttpMethod")
.simple("POST")
.setHeader("Content-Type")
.simple("application/x-www-form-urlencoded")
.setHeader("Accept")
.simple("application/json")
.setBody()
.constant("grant_type=client_credentials&client_id=<client id>&client_secret=<client sec>")
.to("https4://<remote auth service url>")
.convertBodyTo(String.class)
.log("response from API: " + body())
.choice()
.when().simple("${header.CamelHttpResponseCode} == 200")
.unmarshal().json(JsonLibrary.Jackson, AccessResponseToken.class)
.setHeader("jwt").simple("${body.access_token}")
.to("direct:<some direct route>")
.otherwise()
.log("Not Authenticated!!!");
If you want to stick into OAuthRestTemplate you may implement a Processor or a bean to wrap those calls and return to your route the authorization token.

Resources