【发布时间】:2019-12-02 12:43:29
【问题描述】:
我正在尝试将实体“订单”从客户端发送到 Rest/Api Spring Boot
后端。
在我的OrderEntity 中,包含一个购买该订单产品的地图。
我们正在尝试使用邮递员软件生成一个正确的 JSON 字符串,该字符串希望通过 post 请求附加到正文中。
@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.totale = totale;
this.fattura = fattura;
}
@ManyToOne
private ClientEntity cliente;
产品实体
public ProductEntity(Integer id, String nome, String descrizione, String categoria, Float prezzo, String foto,
Integer quantitaMagazzino, Integer spedizione_veloce) {
this.id = id;
this.nome = nome;
this.descrizione = descrizione;
this.categoria = categoria;
this.prezzo = prezzo;
this.foto = foto;
this.quantitaMagazzino = quantitaMagazzino;
this.spedizione_veloce = spedizione_veloce;
}
我们尝试在 post 请求中使用 json,使用这种类型的正文:
{
"id": 10,
"data": "2019-07-11 00:00:00",
"totale": null,
"fattura": null,
"product": {
"ProductEntity{id=4, nome='oneplus 6t', descrizione='smartphone', categoria='elettronica', prezzo=500.0, foto='', quantitaMagazzino=4, spedizione_veloce=0}": 2
},
"cliente": {
"id": 3
}
}
“产品”字段有错误,我们写的格式错误, 就是这样的问题:
“状态”:400,
“错误”:“错误请求”,
"message": "JSON 解析错误:对于输入字符串:\"ProductEntity{id=4, nome='oneplus 6t', descrizione='smartphone', categoria='elettronica', prezzo=500.0, foto='', quantitaMagazzino =4, spedizione_veloce=0}\"; 嵌套异常为 com.fasterxml.jackson.databind.JsonMappingException
这是我的发帖请求:
@PostMapping("/postorder") //PROVA aggiunge un ordine
public ResponseEntity<?> postOrder(@RequestBody OrderEntity order){
orderRepository.save(ordine);
return new ResponseEntity<>(Collections.singletonMap("id", ordine.getId()),HttpStatus.CREATED)
【问题讨论】:
-
你可以试试gson
String jsonMap = new GsonBuilder().create().toJson(map); -
@JsonDeserialize 可以解决问题。更多信息:baeldung.com/jackson-exception
标签: javascript java json spring jackson