亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 JSON 反序列化為類實例

將 JSON 反序列化為類實例

四季花海 2023-04-26 15:42:48
我有一個通過 RabbitMQ 作為消息發送的類,在發件人服務上,它定義如下:public final class User implements Serializable {    private String nome;    private String cognome;    public User(@JsonProperty("nome") String nome,                @JsonProperty("cognome") String cognome) {        this.nome = nome;        this.cognome = cognome;    }    public String getNome() {        return nome;    }    public String getCognome() {        return cognome;    }    public User(){}}在接收方服務上:@Documentpublic class Persona {    @Id    @JsonProperty    public ObjectId id;    private String nome;    private String cognome;    public String getId() {        return id.toHexString();    }    public void setId(ObjectId id) {        this.id = id;    }    public String getNome() {        return nome;    }    public void setNome(String nome) {        this.nome = nome;    }    public String getCognome() {        return cognome;    }    public void setCognome(String cognome) {        this.cognome = cognome;    }    public Persona(ObjectId id, String nome, String cognome) {        this.id = id;        this.nome = nome;        this.cognome = cognome;    }    public Persona(){}}在接收器控制器中,我有以下方法,它應該獲取該消息,將其轉換為一個對象,并將其保存在數據庫中,它看起來像:@RabbitListener(queues = {"default_parser_q"})    public void receiveMessage(final Message message){        ObjectMapper mapper = new ObjectMapper()                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)                .configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);        mapper.readValue(message.getBody(), Persona.class);        System.out.println(message.toString() + "the message has been received");    }問題是我在該readValue()方法上遇到異常,特別是:未處理的異常:java.io.IOException、com.fasterxml.jackson.core.JsonParseException、com.fasterxml.jackson.databind.JsonMappingException在這種情況下發送的消息 (JSON) 是:{  'nome': "John",  'cognome': "Doe"}我在這里做錯了什么?
查看完整描述

4 回答

?
慕娘9325324

TA貢獻1783條經驗 獲得超4個贊

它看起來像我為 Kafka 看到的行為,因為你也在使用 Spring 我認為它是相同的。

您的發送端將對象轉換為 Json,然后將其作為字符串發送 - 因此錯誤消息 () 中的轉義引號\"nome\": \"John\",\n\t\t\t\"cognome\": \"Doe\"。

您需要在發送方聲明 JsonSerializer 然后將其傳遞給您User,或者 - 如果您手動創建 Json String - 聲明它是一個字節數組,這樣 Spring 就不會嘗試轉義引號和空格。


查看完整回答
反對 回復 2023-04-26
?
慕田峪9158850

TA貢獻1794條經驗 獲得超7個贊

看起來您正在對 JSON 進行“雙重”編碼。


如果您使用 發送 JSON 字符串RabbitTemplate,則不應使用 JSON 消息轉換器,因為它會重新編碼已經編碼的 JSON。


要么使用template.send(msg)(設置messageProperties.contentType()為application/json),要么如果使用,則在模板中convertAndSend()使用 a并SimpleMessageConverter


 template.convertAndSend(exchange, rk, myJsonStrng, msg -> {

        msg.getMessageProperties().setContentType("application/json");

        return msg;

 });


查看完整回答
反對 回復 2023-04-26
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

終于找到了導致您發布的堆棧跟蹤的匹配輸入

問題是您對 jackson 映射器的輸入是這樣的:

\"{\\n\\t\\t\\t\\\"nome\\\": \\\"John\\\",\\n\\t\\t\\t\\\"cognome\\\": \\\"Doe\\\"\\n\\t\\t}\"

杰克遜認為這是一個單一的價值,但未能將其映射到創造者身上。

正確的輸入看起來像這樣

{\n\t\t\t\"nome\": \"John\",\n\t\t\t\"cognome\": \"Doe\"\n\t\t}

正如@daniu發布的那樣,這可能是由于Spring其他地方的一些干擾。


查看完整回答
反對 回復 2023-04-26
?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

您可能可以編寫復雜的自定義 JSON 配置,但最簡單的解決方案是將您的消息反序列化回類,User然后向您的Persona類添加一個構造函數,該構造函數將idclassUser作為參數



查看完整回答
反對 回復 2023-04-26
  • 4 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號