我们有两个带有客户端 - 服务器架构的Spring Boot应用程序。后端配置了Spring Data REST + JPA。前端应该消耗后端公开的资源并提供公共REST API。
是否可以让Spring数据通过声明例如mapper bean自动从DTO映射域对象?
// 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> {}
答案 0 :(得分:3)
我们可以在Spring Data REST中使用Projection功能(从2.2.x开始提供)。如下所示:
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> {
}
调用REST时,将“projection”参数设置为“orderDTO”即
http://host/app/order?projection=orderDTO
请参阅:
注意: