我有一个类,我正在使用 Jersey 客户端反序列化。这个类有一个看起来像这样的方法:
public boolean isEmpty() {
return (code == null &&
label == null &&
codeSystem == null &&
codeSystemLabel == null &&
description == null &&
concept == null &&
alternateCode == null
);
没有二传手。按原样,这将引发此异常:
com.sun.jersey.api.client.ClientHandlerException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "empty" (Class com.app.models.CodedElementModel), not marked as ignorable
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@3b927d51; line: 1, column: 270] (through reference chain: com.app.models.LabOrderModel["code"]->com.app.models.CodedElementModel["empty"])
我读过这篇文章,事实证明我可以通过在CodedElementModel
类上添加这个注释来解决这个问题:@JsonIgnoreProperties(ignoreUnknown = true)
。
问题是我有很多方法抛出这个异常。有没有办法将 Jersey 客户端配置为好像@JsonIgnoreProperties(ignoreUnknown = true)
在每个类上都设置了一样,所以我不必手动注释它们?我不想通过手动添加此注释来更改 ~30 个文件。如果有人在没有 setter 的情况下添加了 getter,这将防止将来出现此类错误。
以下是我创建 Jersey 客户端的方式:
DefaultClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(clientConfig);
我觉得clientConfig
可能有一个设置可以做到这一点,但我不知道如何找到它。