代码之家  ›  专栏  ›  技术社区  ›  Kiran

Jackson无法用连字符反序列化字段

  •  1
  • Kiran  · 技术社区  · 7 年前

    在下面的代码中,resultant orderobj没有合同ID、订单号详细信息。它只反序列化带连字符的字段。根据我的知识,@jsonproperty应该映射。

    请帮助获取我的结果订单obj中的所有字段。

    import com.fasterxml.jackson.annotation.JsonProperty;
    
    public class Order implements Serializable {
    
        private static final long serialVersionUID = 6791737023394030929L;
    
        @JsonProperty("id")
        private String id;
    
        @JsonProperty("contract-id")
        private String contractId;
    
        @JsonProperty("order-number")
        private String orderNumber;
    }
    
    
    final ObjectMapper mapper = new ObjectMapper();
    mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(mapper.getTypeFactory()));
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
    
    Response response = orderServiceTarget.queryParam("contract-id",contractId).request().accept(MediaType.APPLICATION_JSON).headers(headersMap).get();
    final String serverResponseStr = response.readEntity(String.class);
    Order  orderObj = objectMapper.readValue(serverResponseStr, Order.class); 
    

    结果对象是:它缺少其他字段,

    {
        "id": "7FABA1724B8F15306447852233",
    }
    

    以下是服务器响应:

    {
        "id": "7FABA1724B8F15306447852233",
        "contract-id": "42BAD9AAA5231BD",
        "order-number": "ADD",
        "market-segment": "COM",
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Paul Bellora    7 年前

    ObjectMapper JaxbAnnotationIntrospector @XmlElement @JsonProperty id

    mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(mapper.getTypeFactory()));