簡體   English   中英

無法讀取json post方法(傑克遜)

[英]Can't read json post method (jackson)

我有對象Bill,我在其中@@ JsonRootName(value =“ bill”)。 我有結果Bill {siteId ='null',billId ='null'}在哪里我犯錯了? 我的json

{
    "bill":
    {
        "siteId":"gkfhuj-00",
        "billId":"11b0309c-42b8-4d20-bd58-3e854f039287"
    }
}

我班的比爾

@JsonRootName(value = "bill")
public class Bill {
    private final String siteId;
    private final String billId;

    public Bill(String siteId, String billId) {
        this.siteId = siteId;
        this.billId = billId;
    }


    public String getSiteId() {
        return siteId;
    }

    public String getBillId() {
        return billId;
    }

    @Override
    public String toString() {
        return "Bill{" +
                "siteId='" + siteId + '\'' +
                ", billId='" + billId + '\'' +
                '}';
    }
}

我許可Json Object的方法

    @PostMapping("/json")
    @ResponseBody
    public ResponseEntity getJson(@RequestBody Bill bill) {

        System.out.println(bill.toString());

        return null;
    }

問題可能出在傑克遜的“功能切換”上。 您需要在ObjectMapper啟用UNWRAP_ROOT_VALUE 因此,如果使用Spring,這應該放在@Bean配置中:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM