We have two Spring Boot applications with a client-server architecture. The backend is configured with Spring Data REST + JPA. The front end should consume the resources exposed by the backend and serve a public REST api.
Is it possible to have Spring data map the domain objects automatically from DTOs by declaring, e.g., a mapper bean?
// JPA persistable
@Entity
public class Order { .. }
// Immutable DTO
public class OrderDto { .. }
// Is this somehow possible..
@RepositoryRestResource
public interface OrderDtoRepository extends CrudRepository<OrderDto, Long> {}
// .. instead of this?
@RepositoryRestResource
public interface OrderRepository extends CrudRepository<Order, Long> {}
Spring Data REST can be used to expose HATEOAS RESTful resources around Spring Data repositories. Without writing a lot of code, we can expose RESTful API around Spring Data Repositories.
@RepositoryRestResource is used to set options on the public Repository interface - it will automatically create endpoints as appropriate based on the type of Repository that is being extended (i.e. CrudRepository/PagingAndSortingRepository/etc).
The association will not be stored in the database. That’s because Spring Data Rest simply puts a list of Address es into the Customer object and tells Hibernate to store it. Hibernate, however, only stores the associations in a bidirectional relationship if all Address es also know the Customer they belong to (also see this post on Stackoverflow).
When working with Spring Data Rest, this implies that a Spring Data repository must exist for both entities. The easiest variant is also the cleanest and most maintainable. Address has a Customer field annotated with @ManyToOne.
By default, Spring requires setter methods to bind HTTP parameters to fields. Fortunately, it’s possible to reconfigure the binder and use direct field access (via reflection). In order to configure the data binder globally for your whole application, you can create a controller advice component.
Each section of this tutorial is managed as a separate subproject in a single github repo: rest — Spring MVC + Spring HATEOAS app with HAL representations of each resource evolution — REST app where a field is evolved but old data is retained for backward compatibility
We can make use of Projection feature (available from 2.2.x onwards) in Spring Data REST. Something like below:
import org.springframework.data.rest.core.config.Projection;
@Projection(name = "orderDTO", types = Order.class)
public interface OrderDTO {
//get attributes required for DTO
String getOrderName();
}
@RepositoryRestResource(excerptProjection = OrderDTO.class)
public interface OrderRepository extends CrudRepository<Order, Long> {
}
When calling REST set "projection" parameter to "orderDTO" i.e
http://host/app/order?projection=orderDTO
Please refer:
Note:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With