首页 > 解决方案 > 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
     },...
]

标签: restspring-boot

解决方案


推荐阅读