繁体   English   中英

如何格式化地图 <Entity,integer> 在json负载中

[英]How to format a Map<Entity,integer> in json payload

我正在尝试从客户端向Rest / Api Spring Boot后端发送实体“订单”。 在我的OrderEntity ,包含一个购买该订单产品的地图。

我们正在尝试使用邮递员软件生成一个正确的JSON字符串,该字符串希望随请求附在正文中。

@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;

ProductEntity

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;
    }

我们尝试使用此类正文在发布请求中使用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)

我认为您不见了:在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
     }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM