Spring Rest Controller vs Camel rest servlet in micro services --- What's the difference , is camel have any other adv? - spring

In my previous application, i have exposed rest api using spring rest controllers.
Now i'm going to work in spring boot microservice where they have used , camel rest services.
Is there any adv of camel rest compared with spring rest?

Apache Camel offers a REST styled DSL which can be used with Java or
XML. The intention is to allow end users to define REST services using
a REST style with verbs such as get, post, delete etc.
Source https://camel.apache.org/manual/latest/rest-dsl.html
The idea of Camel REST endpoints is to start e Camel Route with a REST Call.
If you don't need Camel Routes it doesn't make sense to use Camel REST.
On the other side Spring MVC provides the ability to define REST APIs that will use other Spring Components to fullfil use case.

Related

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

Does Apache Camel replace or complement creating micro-services with Spring Boot?

I have been working for a while with Spring micro-services and have no come across Apache Camel as a tool for building micro-services. I'm unclear -- is Apache Camel a replacement for creating micro-sevices with Spring Boot or does it add functionality / short-cuts to developing such services with Spring Boot? It's already fairly simple to create microservices with Spring Boot so it's hard to imagine what Apache Camel would add but that is the essence of my question.
Apache Camel has nothing to do with microservices.
It's an implementation of the Enterprise Integration Patterns: https://www.enterpriseintegrationpatterns.com/
Apache Camel provides an implementation for most of the patterns from the book from Gregor Hohpe and Bobby Woolf. Plus a variety of inbound and outbound endpoints to integrate with systems like the file system, FTP, HTTP, Messaging, Facebook etc.
Find more information on the website: https://camel.apache.org/
There is a Spring Boot Starter project to run Camel in a Spring Boot application:
https://camel.apache.org/spring-boot.html
what Apache Camel would add, that is the essence of my question
In service of declaring REST based microservices, Camel's REST DSL provides a fluent API for declaring microservices. Take for example:
rest("/books").produces("application/json")
.get().outType(Book[].class)
.to("bean:bookService?method=getBooks(${header.bookCategory})")
Should tell you at a glance that requests to the path /books will get you a List of Book, as long as you send a request parameter named bookCategory. This is mapped to a POJO bean called bookService.
Spring Boot is a framework which simplifies application packing and startup while Spring is the actual framework which has libraries for performing various tasks.
Technically, we can use Camel for building micro-services as well and many aspects of camel depend on Spring. If you foresee many integration related functionality like sending email or communicating with other system, you can use also use Hexagonal architecture.

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

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.

Spring XD as a Rest Endpoint

Can we integrate Spring XD and Spring MVC, so that we can ingest/process data sent to the REST end point?
We have huge amounts of data collected in a Spring MVC app and are wondering if we could use Spring XD for this.
The http source is a stripped-down http server (based on netty); it is not a full Spring MVC server. For that, you would need a custom source with an embedded tomcat/jetty.
See Artem's comment on this answer for a suggestion about how to embed tomcat in a module.

Resources