POST 请求 SpringBoot HIbernate 的 Controller 函数中获取的空列表
Empty List obtained in the Controller function for a POST request SpringBoot HIbernate
我正在创建一个电子商务购物平台。
我有以下 3 个实体 classes:
产品库存
订单
已订购产品
@实体
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class 产品库存 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long productId;
private String name;
private Double price;
private Long availableQty;
private String description;
@Enumerated(EnumType.STRING)
private Category category;
}
@实体
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class 订单 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long orderId;
private String userEmail;
@Temporal(TemporalType.DATE)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="dd/MM/yyyy")
private Date orderDate;
@Temporal(TemporalType.DATE)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="dd/MM/yyyy")
private Date deliveryDate;
private double bill;
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL , mappedBy = "orders")
private List<OrderedProduct> orderedProductList = new ArrayList<>();
}
@实体
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@JsonIdentityInfo(属性 = "id",generator = ObjectIdGenerators.PropertyGenerator.class)
public class OrderedProduct {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Long orderedProductId;
private Long quantityOrdered;
@ManyToOne(optional = false , fetch = FetchType.LAZY)
@JoinColumn(name = "ORDEREDPRODUCT_ID")
private Orders orders;
@ManyToOne(optional = false , fetch = FetchType.LAZY)
@JoinColumn(name = "product_product_id")
private ProductInventory product;
}
我有以下控制器来接受 POST 请求。
@RestController
@RequestMapping("/orders")
public class OrderController {
@Autowired
private OrderService orderService;
@PostMapping(value = "/placeOrder")
public ResponseEntity<String> createUserRequest(@RequestBody Orders newOrder) throws IOException {
System.out.println("NewOrder ID = "+ newOrder.getOrderId());
System.out.println("NewOrder UserEmail = "+ newOrder.getUserEmail());
System.out.println("NewOrder ORDERDATE = "+ newOrder.getOrderDate());
System.out.println("NewOrder DELIVERYDATE = "+ newOrder.getDeliveryDate());
System.out.println("NewOrder BILL = "+ newOrder.getBill());
System.out.println("NewOrder List = "+ newOrder.getOrderedProductList());
return orderService.createOrder(newOrder);
}
}
当我发送以下 POST 请求时:
{
"userEmail": "new order",
"orderDate": "09/03/2022",
"deliveryDate": "09/04/2022",
"bill": 80,
"orderedProductList": [
{
"orderedProductId": 1,
"quantityOrdered": 3
}
]
}
我在控制台中得到以下输出:
NewOrder ID = null
NewOrder UserEmail = new order
NewOrder ORDERDATE = Wed Mar 09 05:30:00 IST 2022
NewOrder DELIVERYDATE = Sat Apr 09 05:30:00 IST 2022
NewOrder BILL = 80.0
NewOrder List = [] --------------------I'm more concerned about this list being empty inside the controller
Inside OrderService
Bill : 80.0
The number of ordered products are as follows : 0
当具有给定 ID 的产品在 table 中存在和不存在时,我尝试执行 POST 请求。
两次它在控制台中返回相同的结果。
我也尝试通过从 JSON 中删除 "quantityOrdered": 3
来执行 POST,但我再次得到相同的结果。 数据正在保存在 MySqlDB 中,但每次列表都是空的。
我找不到我哪里出错了。
有人能帮忙吗?
提前致谢。
你在那个字段上有@JsonIgnore,所以它不会被反序列化。
参考this.
我正在创建一个电子商务购物平台。
我有以下 3 个实体 classes:
产品库存
订单
已订购产品
@实体 @Getter @Setter @NoArgsConstructor @AllArgsConstructor public class 产品库存 {
@Id @GeneratedValue(strategy = GenerationType.AUTO) private Long productId; private String name; private Double price; private Long availableQty; private String description; @Enumerated(EnumType.STRING) private Category category;
}
@实体 @Getter @Setter @NoArgsConstructor @AllArgsConstructor public class 订单 {
@Id @GeneratedValue(strategy = GenerationType.AUTO) private Long orderId; private String userEmail; @Temporal(TemporalType.DATE) @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="dd/MM/yyyy") private Date orderDate; @Temporal(TemporalType.DATE) @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="dd/MM/yyyy") private Date deliveryDate; private double bill; @JsonIgnore @OneToMany(cascade = CascadeType.ALL , mappedBy = "orders") private List<OrderedProduct> orderedProductList = new ArrayList<>();
}
@实体 @Setter @Getter @NoArgsConstructor @AllArgsConstructor @JsonIdentityInfo(属性 = "id",generator = ObjectIdGenerators.PropertyGenerator.class) public class OrderedProduct {
@Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private Long orderedProductId; private Long quantityOrdered; @ManyToOne(optional = false , fetch = FetchType.LAZY) @JoinColumn(name = "ORDEREDPRODUCT_ID") private Orders orders; @ManyToOne(optional = false , fetch = FetchType.LAZY) @JoinColumn(name = "product_product_id") private ProductInventory product;
}
我有以下控制器来接受 POST 请求。
@RestController
@RequestMapping("/orders")
public class OrderController {
@Autowired
private OrderService orderService;
@PostMapping(value = "/placeOrder")
public ResponseEntity<String> createUserRequest(@RequestBody Orders newOrder) throws IOException {
System.out.println("NewOrder ID = "+ newOrder.getOrderId());
System.out.println("NewOrder UserEmail = "+ newOrder.getUserEmail());
System.out.println("NewOrder ORDERDATE = "+ newOrder.getOrderDate());
System.out.println("NewOrder DELIVERYDATE = "+ newOrder.getDeliveryDate());
System.out.println("NewOrder BILL = "+ newOrder.getBill());
System.out.println("NewOrder List = "+ newOrder.getOrderedProductList());
return orderService.createOrder(newOrder);
}
}
当我发送以下 POST 请求时:
{
"userEmail": "new order",
"orderDate": "09/03/2022",
"deliveryDate": "09/04/2022",
"bill": 80,
"orderedProductList": [
{
"orderedProductId": 1,
"quantityOrdered": 3
}
]
}
我在控制台中得到以下输出:
NewOrder ID = null
NewOrder UserEmail = new order
NewOrder ORDERDATE = Wed Mar 09 05:30:00 IST 2022
NewOrder DELIVERYDATE = Sat Apr 09 05:30:00 IST 2022
NewOrder BILL = 80.0
NewOrder List = [] --------------------I'm more concerned about this list being empty inside the controller
Inside OrderService
Bill : 80.0
The number of ordered products are as follows : 0
当具有给定 ID 的产品在 table 中存在和不存在时,我尝试执行 POST 请求。 两次它在控制台中返回相同的结果。
我也尝试通过从 JSON 中删除 "quantityOrdered": 3
来执行 POST,但我再次得到相同的结果。 数据正在保存在 MySqlDB 中,但每次列表都是空的。
我找不到我哪里出错了。
有人能帮忙吗?
提前致谢。
你在那个字段上有@JsonIgnore,所以它不会被反序列化。
参考this.