Jackson JSON反序列化兼容JSON对象和字符串

案例一

no String-argument constructor/factory method to deserialize from String val

:Failed
to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: 
JSON parse error: Cannot construct instance of `com.example.demo.model.Customer` 
(although at least one Creator exists): no String-argument constructor/factory
 method to deserialize from String value 
 ('{"id":0,"name":"Joe","city":"HangZhou","contactNum":123456}');

在spring boot前端访问接口突然出现上述错误,后台接收参数接收到了字符串,导致不能反序列化。但是前端代码看不出任何问题。

解决办法:增加一个构造函数来手动赋值

public SubmitParam(String json) throws IOException {
    SubmitParam param = new ObjectMapper().readValue(json, SubmitParam.class);
    this.id = param.getId();
    this.name = param.getName();
    this.city = param.getCity();
    this.contactNum = param.getContactNum();
}

案例二

no String-argument constructor/factory method to deserialize from String value ('null')
org.springframework.web.client.RestClientException: 
Error while extracting response for type [class cn.appblog.provider.channel.channels.kbank.model.response.QrPayQueryResponse] and content type [application/json;charset=UTF-8];
nested exception is org.springframework.http.converter.HttpMessageNotReadableException: 
JSON parse error: Cannot construct instance of `cn.appblog.provider.channel.channels.kbank.model.Metadata` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('null'); 
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: 
Cannot construct instance of `cn.appblog.provider.channel.channels.kbank.model.Metadata` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('null')
{
    "id":"chrg_prod_609e6d56f22797e82d2b7630ac7962766bf",
    "object":"charge",
    "created":"20210331185005000",
    "livemode":true,
    "amount":3179,
    "currency":"THB",
    "description":"VEMAR I ผู้นำแฟชั่นให้ผู้หญิงยุคใหม่ กระเป๋าชิคที่ไม่ซ้ำใคร - #20180677492802",
    "metadata":"null",
    "source":{
        "id":"qr_prod_4098550e5d3659740f69c974449e85c9bc1",
        "object":"qr",
        "brand":"ThaiQR",
        "card_masking":null,
        "issuer_bank":null
    },
    "status":"success",
    "order_id":"order_prod_6094ef376fe68cd8e90aa00810e7a53fce7",
    "transaction_state":"Settled",
    "reference_order":"182021033100920298",
    "failure_code":null,
    "failure_message":null
}
@Data
@NoArgsConstructor
public class Metadata {
    private String item;
    private String qty;
    private String amount;

    public Metadata(String json) throws IOException {
        this.item = null;
        this.qty = null;
        this.amount = null;
    }
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/04/02/jackson-json-deserialization-compatible-with-json-object-and-string/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Jackson JSON反序列化兼容JSON对象和字符串
案例一 no String-argument constructor/factory method to deserialize from String val :Failed to read HTTP message: org.springframework.http.converte……
<<上一篇
下一篇>>
文章目录
关闭
目 录