【问题标题】:How to format a Map<Entity,integer> in json payload如何在 json 有效负载中格式化 Map<Entity,integer>
【发布时间】: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


【解决方案1】:

我认为您在 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
    }
}

【讨论】:

  • 嗨,感谢您的帮助,我尝试了:但我遇到了同样的问题,也许我必须更改 toString 方法?我不知道如何用 body json 格式写这个产品
【解决方案2】:

确保您的 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
     }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多