无法识别的属性异常对我没有意义

时间:2018-03-01 18:52:56

标签: java json jackson

我有一个包含简单数据类型(String)和复杂数据类型的JSON映射。我已经创建了一个结构来处理这个问题。这是:

public static class PayloadData {
    String authkey;
    PizzaPlaceTo pizzaPlace;
    Map<String, List<String>> updates;
}

现在,我像这样读取数据:

    PayloadData payloadData = objectMapper.readValue(payload, PayloadData.class);

它给了我:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "authkey" (class pizzainthecloud.pizzaplace.controller.PizzaPlaceController$PayloadData), not marked as ignorable (0 known properties: ])
 at [Source: {"authkey":"Don't Panic!","pizzaPlace":{"contactName":null,"customerId":null}; line: 1, column: 13] (through reference chain: pizzainthecloud.pizzaplace.controller.PizzaPlaceController$PayloadData["authkey"])

因此,如果我正确地阅读此内容,它会告诉我接收类中没有authkey来接收JSON数据中的authkey,对吧?只有。

请帮助。

1 个答案:

答案 0 :(得分:0)

因此,看起来Jackson只适用于bean而不适用于struct style类。我把班级改为:

public static class PayloadData {
    private String authkey;
    private PizzaPlaceTo pizzaPlace;
    private Map<String, List<String>> updates;

    public String getAuthkey() {
        return authkey;
    }

    public void setAuthkey(String authkey) {
        this.authkey = authkey;
    }

    public PizzaPlaceTo getPizzaPlace() {
        return pizzaPlace;
    }

    public void setPizzaPlace(PizzaPlaceTo pizzaPlace) {
        this.pizzaPlace = pizzaPlace;
    }

    public Map<String, List<String>> getUpdates() {
        return updates;
    }

    public void setUpdates(Map<String, List<String>> updates) {
        this.updates = updates;
    }
}

这解决了这个问题。