Understanding how Mule ESB fits in with JDBC and REST - java

I have this simple REST service running in our JBoss app server which works great. For now, the service has one #GET method (getAllPeople), which makes a JDBC connection to a mysql database, to retrieve some data (SELECT name, address FROM Person).
My team lead wants me to experiment with Mule ESB, and use this simple project as a starting point. I'm confused on how to make this work with a Mule flow. I set up my flow with an HTTP inbound point, REST component, and JDBC component, like this:
I configured the JDBC component to use MySQL, and added a query to it (SELECT name, address FROM Person).
The question: The code in my REST service #GET method is still connecting directly to the DB with JDBC. I think I need to change this to instead, invoke the Mule JDBC datasource instead, but I have no idea how to do this. What code do I need to put in my REST service #GET method to utilize the Mule JDBC component, and get a result? I am trying to get a grasp on how the components talk to each other.
EDIT: new thought - Should an ESB be used to link different components of a single system together, such as REST services, SOAP services, and JDBC data sources, or should an ESB be used only to connect completely independent systems together?

To make your trivial example work, just delete the REST component and add an Object to JSON transformer after the JDBC component. Your flow will then return the query results in JSON format.

We can use mule here to call the rest api, rest api will return some data that may not be sufficient. In order to get more data from db based on the result of rest call (transformed by JsontoObject) jdbc connector could be used. In your case if you want to call rest api which eventually calls the DB, you will get json or xml which can be transformed to send the data to outbounds.
Thanks-

I am not sure why you have that 'rest' component in between. that may not be needed. the simplest would be to have the http, and a transformer (to transform the data from http to/from), and a jdbc component. with this approach you are leaving behind the existing ReST service in JBoss App Server.
I think in the posted picture you try to call the pre-existing ReST service in Jboss App Server which may not be needed. You can simply immitate what the existing service does. or you may consider the existing service and the new mule service can act as a proxy service - in such a case the JDBC component is not required.

Related

Microserivce Using Spring Boot to Consume SOAP

I need to create a REST service that first consumes SOAP. What would the best way to go about this?
I would like to use Spring Boot to create a microservice, but I have a few questions for those with experience:
What other architectures or technologies should I look into using
(with spring boot)?
Is there a standard technology stack for this?
Are there pitfalls I should be aware of?
What other architectures or technologies should I look into using (with spring boot)?
My answer is if you just want to simply provide RESTful service
without Spring Cloud, then I think you can refer to following 2
tutorials on Spring official website to achieve this:
Building a RESTful Web Service
Consuming a SOAP web service
Is there a standard technology stack for this?
Currently, I will suggest you to use Spring Boot as your first choice. Because these are rich resources on the web and it does reduces much effort in development.
Are there pitfalls I should be aware of?
If you finally choose Spring Boot, please be familiar to its components, you can start from Guides to realize how it works. Or you may mix up Spring Boot with tradition Spring framework.
There are similar cases in our project and we did it with spring components. As far as i understood, you want to open a REST endpoint which most probably accepts json object and you want to make a soap web service request within that request then you want to return a response containing information from the soap response. To make a soap request, you can use spring web services - WebServiceTemplate. It will marshall your object to xml and make soap request for you. Of course you can use some other web service frameworks like apache cxf which may best fit for some special cases, but i would first try to use a framework which is from spring family while using Spring. You should set timeout values on webservicetemplate object to not wait too long if the external system is not working well or you have some problems with the network. Because it directly affects your systems performance. Also here, i suggest you to implement circuit breaker pattern to make your system more robust. You should always isolate your systems performance from other systems that you integrate and in this scenario you can do this by doing the things explained above.
As per my knowledge , you should use Spring boot application with Maven build.
In order to send a REST call to SOAP web service and get back a JSON Response ,you need to follow all of this steps in order :
Business Logic : Mapping the JSON fields such as headers, query-param , body variables to XML Request mandatory fields (either using pojo classes or object-mapper) Reason : Since the SOAP end point will only accept XML Request.
Service Broker Logic : Import "org.springframework.web.client.RestTemplate" and use
**ResponseEntity<String> responseEntity=RestTemplate.exchange(endPointURL, HttpMethod.GET/POST/PUT/DELETE, HttpEntity/headers, uriVariables)**
**endpointURL** -- SOAP End point URL ,that the REST service has to consume.
**HTTPMethod** -- Method Type such as GET ,PUT ,POST ,DELETE etc.
**HTTPEntity** -- Soap requires for mandatory sender/headers/{a}.Make sure that you set your header name and value as key-Valye pair in HTTP headers.
**uriVariables** -- (Object... urivariables) such as String.class ,Integer.class
You should also put the **connectTimeout** ,**isSSLDisabled**,**responseCached** elements while generating request to restTemplate.
responseEntity.getBody() is the XML response after un-marshalling.It can be extracted by using mapper.
XML_BaseResponse response=mapper.readValue(responseEntity.getBody(), XML_BaseResponse.class);
Business Logic : Extract necessary field from XML_BaseResponse and using setter's or getter's functions,set the mandatory fields in the response.
baseResponse.setName(xml_baseResponse.getPersonsName());
baseResponse.setAddress(xml_baseResponse.getAddress());
baseResponse.setCity(xml_baseResponse.getcityName());

Using Camel to get data from a web service

There is a lot of information for using Camel with JMS, but I can’t find much on using Camel to request data from a web service. I am wondering if this is even possible to do this directly, or do I need to use some kind of JMS/queue?
Ideally, I want my service (using java DSL) to send out a request to another web service, retrieve the data, and then store this in a file locally. Is it possible to do this in Camel using a simple custom RouteBuilder and a camelContext? I’ve tried setting up my routes using
`from(“http:..”).to(“file:...");`
but this doesn’t seem to work, as it doesn’t seem to get the data from the web service. From what I've read, I was under the impression that the endpoint http: will build the request automatically and route this to a file.
I am now wondering what I could try next, and if this is even possible.
It is definitely possible. The best example to start with is the CXF-Proxy Example. This shows how to invoke a remote web-service from Camel. Feel free to ask specific questions if you run into issues and we can help.
Also you could use Spring Web Services Component
http://camel.apache.org/spring-web-services.html
In this case, your route would look like:
from("<some event to trigger the route>")
.to("spring-ws:<endpoint of the WS you want to use>")
.to("file:<write the WS result>")
If you don't mind trying things out at the SOAP level you could try out the approaches found here http://camel.apache.org/soap.html link. Its worth a look even if you end up using something a little more high level.

Can an ESB imitate a database stored proc and pass the request on to a web service instead?

I've got an older system that can call a stored proc, but can't call a web service. The business logic from this stored proc is being migrated into a web service. Are there any ESB adapters that can simulate a database and accept input in the form of stored proc input variables, then turn around and call a web service? After the web service returns a result, the answer would need to be mapped into a result set and/or output variables and sent back to the client. I would prefer to use Spring Integration or Mule or some other Java-based ESB, but would be willing to consider other options.
If the legacy application uses JDBC, use the p6spy driver to intercept the calls and redirect them to the web service.

Java REST using DataSource

I'm new to REST on Java and I would like to create "simple" REST services that queries a database and returns some JSON info.
What I would like is to use JDBC access to DB, no JPA.
When working with servlets I configure a server datasource and in my servlets in the "init()" method I store a reference to the datasource and on every request create a connection.
What must I do for REST services?
Thanks in advance.
If you are planning to connect via JDBC based on the REST API requests and already familiar with REST, you should consider looking into Spring JDBC support: Current JDBC Spring Support. This would take care of all your concerns regarding the right place and time of instantiating/looking up a data source or connection.

Implementation of web service

I just want to implement a service in java that will:
take some arguments, then search the database
return the JSON object of the fetched data
I need help to identify the ways through which I can implement this thing.
e.g. Suppose I am getting the name of the book as argument I want to render.
On service part, I have to fetch book data and convert it to JSON and write/return to response.
I was looking at the Apache Axis2 but I am not sure that I am going in the right direction.
So, pls help.
Need guidelines not implementation.
Thanks
I would suggest using JAX-RS based services which would be ideal for your scenario as you want json data. These are pretty easy to get started with. Jersey is a widely used frameworks. Also see RESTEasy.
If you are returning the data in JSON then you probably don't need to implement a full web service, which uses XML for both the request and the response.
A normal dynamic web application (written as a Java Servlet) will be able to read request parameters in the HTTP payload and return a JSON-encoded HTTP response.
However you need to consider your clients; if they are only able to access web services then you need to forget about a JSON response and simply objectify the response. However if the clients can access web resources without issue then go with the servlet approach.
If you need to go with web services then look at the Metro 2 framework.
One way to do this is to keep it standards-based.
If you are using the JEE5/6 framework, your best bet would be to go with JAX-WS - comes built-in with the JSE too (if I remember correctly)
You really just have to annotate a POJO with #WebService to achieve this.
Regarding creating a JSON response, a good bet is to stick with the implementation from http://code.google.com/p/google-gson/ ; simple and straightforward
Axis2 can handle/support the webservice related part, iaw, transforming java objects into JSON and vice-versa and providing an easy-to-use API for the communication part.
Hibernate or JPA could be useful for database related tasks, although it might be easier to just use JDBC to send some simple SQL commands to the database (especially if the database already existst).

Categories