Javascript 如何格式化地图<;实体,整数>;在json负载中
我正试图从客户端向Rest/Api Spring Boot发送一个实体“订单” 后端。 在myJavascript 如何格式化地图<;实体,整数>;在json负载中,javascript,java,json,spring,jackson,Javascript,Java,Json,Spring,Jackson,我正试图从客户端向Rest/Api Spring Boot发送一个实体“订单” 后端。 在myOrderEntity中,包含购买该订单产品的映射 我们正在尝试使用postman软件生成一个正确的JSON字符串,该字符串希望通过post请求附加到正文中 @ElementCollection @CollectionTable(name = "product.order", joinColumns = @JoinColumn(name = "order.id")) @MapKeyJoinColumn(
OrderEntity
中,包含购买该订单产品的映射
我们正在尝试使用postman软件生成一个正确的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)
订单实体
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,“错误”:“错误请求”,
“消息”:“JSON解析错误:输入字符串:\”ProductEntity{id=4,nome='oneplus 6t',Descriptione='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)
@PostMapping(“/postorder”)//PROVA aggiunge un-ordine
公共响应性postOrder(@RequestBody OrderEntity order){
orderRepository.save(ordine);
返回新的响应属性(Collections.singletonMap(“id”,ordine.getId()),HttpStatus.CREATED)
我认为您在ProductEntity中缺少了:
,这就是为什么它不能在映射中解析为obj。请尝试告诉我它是否解决了问题,我还没有测试它
{
"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
}
}
确保您的ProductEntity使用@JsonDeserialize进行注释,并使用此JSON,然后查看它是否有效
{
"id": 10,
"data": "2019-07-11 00:00:00",
"totale": null,
"fattura": null,
"product": {
"{id=4, nome='oneplus 6t', descrizione='smartphone', categoria='elettronica', prezzo=500.0, foto='', quantitaMagazzino=4, spedizione_veloce=0}": 2
},
"cliente": {
"id": 3
}
}
你可以试试gson
String-jsonMap=new-GsonBuilder().create().toJson(map);
@jsondeselliate可以做到这一点。更多信息:您好,感谢您尝试帮助我,我尝试了:但我也遇到了同样的问题,也许我必须更改为字符串方法?我不知道如何用body-json格式编写此产品