rest - Spring Boot:构建对象 Rest API
问题描述
我为 3 个表创建了 4 个类。这些表是用来存储产品订单的。
我的第一个实体是订单:
@Entity
@Table(name="orders")
public class OrderEntity implements Serializable {
...
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private UserEntity seller;
private String paymentMode;
private double totalAmount;
@OneToMany(mappedBy = "pk.order")
private List<OrderProductEntity> orderProducts;
@DateTimeFormat
private Date createdAt;
...
}
我的第二个实体是产品:
@Entity
@Table(name="products")
@Getter @Setter
public class ProductEntity implements Serializable {
...
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String productKeyId;
@ManyToOne
@JoinColumn(name = "category_id")
private CategoryEntity category;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private double price;
@Column(nullable = false)
private int availableQty;
@JsonManagedReference
@OneToMany(mappedBy = "pk.product", fetch = FetchType.EAGER)
@Valid
private List<OrderProductEntity> orderProducts;
...
}
我还在 OrderProductEntity 中定义了数据透视表:
@Entity
@Table(name="order_product")
@Getter @Setter
public class OrderProductEntity {
@EmbeddedId
@JsonIgnore
private OrderProductPK pk;
@Column(nullable = false)
private Integer quantity;
// default constructor
public OrderProductEntity(){}
public OrderProductEntity(OrderEntity order, ProductEntity product, Integer quantity) {
pk = new OrderProductPK();
pk.setOrder(order);
pk.setProduct(product);
this.quantity = quantity;
}
}
我定义了这个表的PK:
@Embeddable
@Getter @Setter
public class OrderProductPK implements Serializable {
@JsonBackReference
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "order_id")
private OrderEntity order;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "product_id")
private ProductEntity product;
}
现在我的问题是通过 Web 服务 Rest 将数据返回到前台。我创建了一个模型来代表我想要获得的结果。
public class OrderRest {
private String orderKeyId;
private String paymentMode;
private double totalAmount;
private List<ProductRest> orderProducts;
private UserRest seller;
private Date createdAt;
}
和 :
public class ProductRest {
private String productKeyId;
private String name;
private double price;
private int qty;
private String imgPath;
private CategoryRest category;
}
在我的控制器中,我想返回一个 OrderRest 对象:
public List<OrderRest> getOrders() {
List<OrderRest> returnValue = new ArrayList<>();
List<OrderDto> orderDtos = orderService.getOrders();
// loop the result
for (OrderDto orderDto : orderDtos) {
OrderRest orderRest = new OrderRest();
ModelMapper modelMapper = new ModelMapper();
orderRest = modelMapper.map(orderDto, OrderRest.class);
returnValue.add(orderRest);
}
return returnValue;
}
此方法映射 OrderRest 列表并调用服务层:
public List<OrderDto> getOrders() {
List<OrderDto> returnValue = new ArrayList<>();
Iterable<OrderEntity> orderEntities = orderRepository.findAll();
ModelMapper modelMapper = new ModelMapper();
for(OrderEntity orderEntity: orderEntities) {
OrderDto orderDto = new OrderDto();
UserDto userDto = orderDto.getSeller();
List<OrderProductDto> orderProductDtos = orderDto.getOrderProducts();
orderDto = modelMapper.map(orderEntity, OrderDto.class);
returnValue.add(orderDto);
}
return returnValue;
}
使用 OrderDto :
public class OrderDto implements Serializable {
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private static final long serialVersionUID = 1L;
private Long id;
private String orderKeyId;
private String paymentMode;
private double totalAmount;
private List<OrderProductDto> orderProducts;
private UserDto seller;
private Date createdAt;
}
和 OrderProductDto :
public class OrderProductDto implements Serializable {
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private static final long serialVersionUID = 1L;
private String productKeyId;
private String name;
private Integer price;
private Integer qty;
private String imgPath;
private CategoryDto category;
}
我的问题是关于我从 REST API 返回的内容:我的产品列表没有返回任何内容:
"orderProducts": [
{
"productKeyId": null,
"name": null,
"price": 0.0,
"qty": 0,
"imgPath": null,
"category": null
},...
]
解决方案
推荐阅读
- c - C中数字的位移
- c# - C#浮点/双差依赖于平台
- rest - nagios 中是否有 API 来检查针对特定 Windows 服务器/(或其服务)添加的最后 schedule_downtime?
- python - 如何根据用户的输入创建列表?带 Python
- javascript - 无法使用 Laravel 5.7 在 sweetalert 中取消删除
- android - 如何在androidx中更改工具栏的主页按钮颜色?
- android - 有没有办法在 Android.bp 文件中定义 C 语言宏?
- javascript - 如何计算每个子div的高度和宽度
- javascript - 无法在 div onclick 中使用方法
- karma-jasmine - Karma 打开本地硬盘驱动器路径而不是 localhost