I'm creating an e-commerce shopping platform.
I've the following 3 entity classes:
ProductInventory
Orders
OrderedProduct
@Entity @Getter @Setter @NoArgsConstructor @AllArgsConstructor public class ProductInventory {
@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;
}
@Entity @Getter @Setter @NoArgsConstructor @AllArgsConstructor public class Orders {
@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<>();
}
@Entity @Setter @Getter @NoArgsConstructor @AllArgsConstructor @JsonIdentityInfo(property = "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;
}
I've the following controller to accept POST requests.
@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);
}
}
When I send the following POST request :
{
"userEmail": "new order",
"orderDate": "09/03/2022",
"deliveryDate": "09/04/2022",
"bill": 80,
"orderedProductList": [
{
"orderedProductId": 1,
"quantityOrdered": 3
}
]
}
I get the following as the output in the console:
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
I tried doing POST request when the product with given id is present as well as absent in the table. Both the time it returned the same result in the console.
I also tried doing the POST by removing the "quantityOrdered": 3
from the JSON, but again I was getting the same result. The data is getting saved in the MySqlDB but the List is empty each time.
I'm unable to find out where I'm making the mistake.
Can anyone help?
Thanks in advance.
CodePudding user response:
You have @JsonIgnore on that field so it will not be deserialized.
Refer this.