I'm currently working on sending an entity named "order" from a client to the Rest/Api Spring Boot Back-End. Within my OrderEntity
, there is a Map containing the products of that order.
We are using Postman software to create a correct JSON string that we want to include in the body of a POST request.
@ElementCollection
@CollectionTable(name = "product.order", joinColumns = @JoinColumn(name = "order.id"))
@MapKeyJoinColumn(name = "product.id")
@Column(name = "quantity")
//@JsonSerialize(keyUsing = ProdottoMapSerializer.class)
@JsonDeserialize(keyUsing = ProdottoMapDeserializer.class)
OrderEntity
public OrderEntity(Map<ProductEntity, Integer> product, ClientEntity cliente,Integer id, String data, Float totale, String fattura) {
this.product = product;
this.client = client;
this.id = id;
this.data = data;
this.total = total;
this.invoice = invoice;
}
@ManyToOne
private ClientEntity client;
ProductEntity
public ProductEntity(Integer id, String name, String description, String category, Float price, String photo,
Integer stockQuantity, Integer fastShipping) {
this.id = id;
this.name = name;
this.description = description;
this.category = category;
this.price = price;
this.photo = photo;
this.stockQuantity = stockQuantity;
this.fastShipping = fastShipping;
}
When making a POST request with JSON, the body should be formatted like this:
{
"id": 10,
"date": "2019-07-11 00:00:00",
"total": null,
"invoice": null,
"product": {
"ProductEntity{id=4, name='oneplus 6t', description='smartphone', category='electronics', price=500.0, photo='', stockQuantity=4, fastShipping=0}": 2
},
"client": {
"id": 3
}
}
The "product" field is causing an error due to incorrect formatting. This is the specific problem:
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: For input string: \"ProductEntity{id=4, name='oneplus 6t', description='smartphone', category='electronics', price=500.0, photo='', stockQuantity=4, fastShipping=0}\"; nested exception is com.fasterxml.jackson.databind.JsonMappingException
Below is the post request being made:
@PostMapping("/postorder") //PROVA aggiunge un ordine
public ResponseEntity<?> postOrder(@RequestBody OrderEntity order){
orderRepository.save(order);
return new ResponseEntity<>(Collections.singletonMap("id", order.getId()),HttpStatus.CREATED)