I have a Spring REST interface and the controller is making the conversion from DTO to Business Entity objects. The Service layer operates on Business Entity objects. However, If my Business Layer wants to call external REST interfaces, where should that Business Entity to DTO conversion happen? At Business Layer? The DAO object using RestTemplate to make the call? Some other layer? The call sequence would current be like this:
business layer -> DAO -> RestTemplate -> RESTful service
Thanks! :)
Well, normally, in spring framework the conversion of a Model to DTO and backwards, is done by implementing org.springframework.core.convert.converter.Converter interface. Thus, getting a reusable converter which can be used either in Facade or DAO or Controller, depending on your needs. Business Layer should not be concerned about conversion. From my perspective, using converter in DAO is perfectly fine. :)
Related
I have a backend api and a frontend client (Angular App) consuming this api, I have to redefine many DTOs form Database Entities (almost 100). I wonder if there is an alternative to transform my entities with much more easier way than using Transformers.
You can use Jackson API to convert the Entity object into DTO or any POJO class objects. Please check below URL for the code example.
https://www.thetechnojournals.com/2019/10/entity-object-conversion-to-dto-object.html
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.
I using spring-data-rest in conjunction to spring-data-jpa which exposes all my spring-data-jpa interfaces as REST resources in HAL JSON format.
I would like to expose my #Service methods in the same fashion. Is this possible? If not, what is the best way to implement an endpoint that may need to use more complex logic the JSON response?
I think you should take a look at this post:
Spring HATEOAS versus Spring Data Rest
Basically the answer says that if you want to expose a REST service which implies some logic more complex than CRUD, there's no other way but implementing the REST layer yourself.
I am about to start creating a new application and wanted to get some feedback on the approach I plan on using. We will be using spring and spring data jpa.
Can controllers call domain services and repositories or should the controller only make calls to application and infrastructure services?
If its "ok" to call domain services and repositories from a controller and a domain service operation needs a transaction, can/should I just put the #Transactional annotation on the domain service method? Or should I have an application service that wraps the call (not sure I like this approach because you would end up with a bunch of pass through methods)?
If its not "ok" to call domain services and repositories from a controller do I need to create application services to wrap all possible domain service calls(like I said in 2 that could be a lot of pass through methods)?
The best solution I can come up with is something like the following:
Repositories will have the #Transactional annotation with propagation REQUIRED when writing to the database and propagation set to readOnly=true and propagation set to SUPPORTS when reading from the database.
Application and Domain Services will add the #Transactional annotation as needed
If the controller ever needs to make make a direct call to a repository a domain service or an application service it can. No pass throughs.
I am not clear for your question. What is the Domain Services doing? I knew Application Services and Domain Repositories very well.
In spring , there are two layers service and data access layer.
Service layer can used #Service (In your design it will be application Services) but not used #Transactional Tag.
Data access layer used #Repository Tag and also #Transactional Tag, Because This layer is directly connected and make operation with the Database. So, I like to know what 's function of the Domain Service. I am not clear for that.
Thanks buddy.
I personally would only access your domain and application services from your controllers. That way you only have to put #Transactional annotations at one "level". You get transactionality out of the box at your repository layer if you're extending the regular Spring Data repository interfaces. I would leave that layer as simple as possible. Put your readOnly and other configuration at the service layer.
Creating "pass through" methods allows you more flexibility down the road too if you ever decide to change your DAO implementation.
I'm developing a simple web service using java ee stack.
The business objects are the POJOs annotated with JPA and Bean Validation rules.
The business methods are exposed through the ws.
The Ws is annotated with:
#SchemaValidation()
#WebService()
#Stateless
When I'm calling the WS through a simple java-main app with invalid params, I get the exceptions on the server side, in the business objects layer, my WS layer seems does not make the validation.
My goal is to protect as much as possible my business layer from the invalid data in the way to have the verification logic on the WS side.
How can I implement this?