java - 无法读取 json post 方法 (jackson)

标签 java json spring-boot jackson

我有对象 Bill,其中我 lisen @JsonRootName(value = "bill")。 我有结果 Bill{siteId='null', billId='null'} 我在哪里犯了错误? 我的 json

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

我的类(class)比尔

@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 对象的方法

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

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

        return null;
    }

最佳答案

问题可能是 jackson 的“功能切换”。您需要启用UNWRAP_ROOT_VALUE在您的ObjectMapper中。因此,如果您使用 Spring,这应该进入您的 @Bean 配置中的某个位置:

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

关于java - 无法读取 json post 方法 (jackson),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57231434/

相关文章:

java - Beans 在 NetBeans 中绑定(bind) JTable

json - Swift - 具有基本身份验证的 Http-Request

c# - 在 asp.net 页面中动态添加 javascript 的一些最佳实践是什么?

java - Spring 启动+ quartz : "Requested bean is currently in creation: Is there an unresolvable circular reference?"

angularjs - Spring 服务器中 OAuth 身份验证的 CORS 失败

Java - 从 (a,b) 延伸到 (c,d) 的直线是否与点 (x,y) 相交?

java - 从 Eclipse 部署到 Glassfish,不包含类

java - com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException : Column cannot be null/one-to-one mapping

php - 如何使用 PHP 从 json 响应中保存图像

java - 如何在请求体中仅发送 id 而不是对象?