【发布时间】:2021-11-17 18:48:52
【问题描述】:
我有一个如下所示的 jsonString:
String jsonString = "{\"ID_BUY\":1234567}";
我有一个 POJO:
@Data
@NoArgsConstructor
@SuperBuilder
@JsonInclude(Include,NON_EMPTY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Order() {
@JsonProperty("ID_BUY")
private String identityBuy;
}
我正在使用 ObjectMapper:
ObjectMapper mapper = new ObjectMapper()
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(MapperFeature.USE_ANNOTATION, true);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()));
当我表演时
Order output = mapper.readValue(jsonString, Order.class);
打印出来
System.out.println(output.toString());
我注意到 identityBuy 的值为 null。
有没有办法让映射器记下@JsonProperty 注释?
谁能指出我正确的方向。
【问题讨论】:
-
我复制了您的代码并且它有效,它需要删除该行:
mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()));。可以试试吗?
标签: java spring jackson objectmapper fasterxml