Spring REST service with third party api as a datasource - spring

I need to expose REST service which as a datasource gets data returned from third party api calls, instead of regular DB.
I'd like to have all the benefits of the Spring Data Rest, but I don't know what I really need as my dependency, since I'm not using JPA.
Basicaly, how my model (Entity?) should be implemented to have the functionalities (like, paging, sorting...).
What Spring framework dependencies should I choose.
Or possibly I'm overlooking something here and for such requirement is another path.
Thanks.

Related

Integrating Vert.x with spring data JPA

I am currently using common approach of calling Spring data JPA repository from Spring service class. But as those calls are blocking, performance is not good for my use-case as number of requests I have to handle is very high.
So I plan to use Vert.x. My understanding is that I would need to call DAO repository from verticle, which in turn I need to integrate in service layer.
Would that be correct way of doing that?

Writing a Mostly Client-Side App Without Controllers (but still within the Spring framework)

We are writing a mostly single-page, client-side app, but server-side/DB endpoints are still required of course, so the natural choice is SpringMVC (since we are a Java / Spring shop).
But this got me thinking why we need the cluttered, very old design for this app:
- Controller layer
- Service layer
- DAO layer
This app is mostly just the client side making AJAX calls with JSON for DB retrieval/persistence. Do I really need to go thru the Controller layer to receive requests, then invoke a Service method, which in turn invokes a DAO method?
At the same time, I don't want to write a REST Service because it could result in overhead and we may not support all of the REST requirements... but is it the right choice here? If I understand correctly, I would still need a RESTController on the presentation layer?
My goal is to just directly hit a Service method or, maybe even more directly, a DAO method. Is that how modern apps are written?
You cannot hit a DAO unless you expose it through an API of some sort that can be invoked remotely by the UI application; as a consequence, you need to write a service.
A convenient way of exposing a service is to either:
Use Spring MVC and use the controllers as stateless endpoints that provide a JSON/Protobuffer/XML sort of payload that is then parsed by your API (with JSON being the simplest option of them all, perhaps) or
Use Spring Boot, which uses Spring MVC under the hood.
Hope this helps and good luck with your project.

Spring Integration Webservice vs. RestTemplate

I'm trying to learn SI (Spring Integration) but i'm a bit confused on the real usage of this.
As first example i would like to interact with a WebService but i dont understand what could be the difference from
Invoke a WebService Using SI
Invoke a Webservice using RestTemplate
So, what is the benefit of using SI (in Webservice context, or in other context)?
Looking the web, i havent find an article that explain:
Usually you will do in this way....
With SI you can do better - in this another way - and the benefit are....
To be more explicit on what i have to realize, this is an example:
1) I have to write an application (Standalone application) that have to collect some data in the system periodically and then invoke a Web Service that will store it.
2) The Web Service receive the call from the "clients" and store in the database.
My webservice will use REST.
The reason because i've think to use SI is that the Standalone Application should interact with different system
Webservice in first instance
A Web Mail, if the webservice is not achievable
File system if Web mail is not achievable too
If you only need to pull some data in a simple way and push it onwards to a REST service this does not "justify" the use of Spring Integration. A simple Spring (Boot) application combined with a scheduler will be sufficient.
But if you want to use a more complex data source for which an endpoint is available, you need transformations, complex and flexible routing is a high priority or even Enterprise Integration Patterns (EIP) then Spring Integration is for you. Have a look at the Overview and decide if it mentions something you consider as valuable to you.
Perhaps you will create additional value by mixing in Spring Batch if you need to process a lot of data.
But as I understand your current demand starting with just a RESTTemplate should do for the moment. Starting small will not prevent you from switching to Spring Integration later on.
Have a look at the various tutorials and guides provided by the Spring Boot project. There is even an example for Spring Integration.

Spring Enterprise Application Best Practices

After read the Gordon's article about Best Practices to build Enterprise Application using Spring Framework, I would like to share some ideas about the Service layer.
My architecture represents exactly what Gordon described in this image http://gordondickens.com/wordpress/wp-content/uploads/2012/07/Spring-App-Layers.png
The application is complex, has a heavy business rule and demands to use different resources like Database, SOAP, REST and file handle sometimes in the same use case.
For this scenery that I have described above, I have a Service class that needs to perform SOAP and REST requests and handle some database data. So, I have autowired in my Service class a SOAP and a REST component and the Repository to handle the database stuff.
I'm concerned about if this is the best approach do handle the integration between my Services and the resources like SOAP, REST, Database and etc.
Thanks
So, I have autowired in my Service class a SOAP and a REST component
and the Repository to handle the database stuff.
Sounds problematic even though it will work.
Think about the dependency between layers. Service layer will depend on Repository layer (Business logic layer will depend on data layer). Service integration layer (or service communication layer) for incoming requests will depend on the Service layer. But the data layer does not depend on the service layer. Nor the service layer depend on the inbound service invocation layer.
So, remove the SOAP and REST components from the Service class. To the SOAP and REST components, wire the Service instance (i.e. avoid SOAP and REST components wired into the Service, do it in the reverse direction).
This way, when you want to support another integration protocol (say JMS), you do all such work not by modifying your service.
Your data access seems to be fine. I hope your Service accesses the repository via DAOs.
So, I have autowired in my Service class a SOAP and a REST component
and the Repository to handle the database stuff.
Sounds fine. You are using dependency injection, this means they can be easily tested or altered.

How to connect my Spring + Hibernate based application backend with pure HTML and AJAX based client?

I'd like to call methods of my DAOs by AJAX. I'm quite new in that so I would like to ask what is the best way to do that. Is it possible to publish my beans as web services and call them with e.g. jQuery? I think it is not possible :) I've also read about Direct Web Remoting but I don't know which way to go...
As I see, there are lot of experienced guys here so I think you can show me direction.. thanks in advance
Rather than exposing your DAO beans directly, you should create some Spring MVC controller beans, and call those from the client-side (using AJAX). Ideally, the controllers should not call the DAOs directly, but should instead call service beans (and the service beans should call the DAOs). One advantage of this approach is that you can define your service methods to be transactional, i.e. whenever a service method begins a transaction is started, and whenever a service method returns (without an exception) the transaction is committed. If the boundaries of your transactions are your DAO methods then it is not possible to wrap several database calls in a single transaction.
Of course there's no reason why you need to use Spring MVC - any web framework would suffice.
You have to expose your DAO's or beans by means of http. Typically you create a layer above the DAO layer to expose your services through HTTP, which are available to any AJAX framework such as jQuery. What jQuery and other frameworks ends up doing is using a special asynchronous request called XMLHttpRequest and then parse the server response (can be anything, pure HTML, JSON, XML, etc) and process it.
Here's a link I found that shows Spring & DWR with AJAX: Bram Smeets Blog.

Resources