簡體   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