java - 杰克逊无法反序列化带有连字符的字段
问题描述
在下面的代码中,结果 orderObj 没有合同 ID、订单号详细信息。它仅对带有连字符的字段进行反序列化。据我所知@jsonproperty 应该映射。
请帮助获取我的结果 orderObj 中的所有字段。
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",
}
解决方案
正如评论中所讨论的,看起来您ObjectMapper
的配置为使用JaxbAnnotationIntrospector
,它将寻找例如@XmlElement
而不是@JsonProperty
. 该id
字段仍然有效,因为它的字段名称在 JSON 和 Java 之间匹配。
您应该能够通过简单地删除此行来解决此问题:
mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(mapper.getTypeFactory()));
推荐阅读
- gradle - 无法使用 gradle 从 nexus 下载 ojdbc 特定 jar
- r - 如何使用 ggplot 自定义 plotCounts (DESeq2) 输出?
- delphi - tDBLookupComboBox 显示 (MEMO)
- imagemagick-convert - 将大 tiff 拆分为具有正确边框的多个页面
- atlassian-sourcetree - SourceTree 中的视觉分支
- python - Keras 掩蔽层作为 lstm 层的输入
- python - 使用 venv 运行 docker 容器时找不到 gunicorn
- ios - DJI SDK iOS 4.7 错误 403 禁止使用 Cocoapods
- python - 无论行数如何,都从最后 3 行中提取数据
- c# - 如何避免进入c#中的缓存块并返回循环迭代?