Jersey 2.8客户端在反序列化期间没有忽略未知属性
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jersey 2.8客户端在反序列化期间没有忽略未知属性相关的知识,希望对你有一定的参考价值。
我正在使用Jersey Client 2.8
,并尝试注册自己的Jackson配置器,该设置器将设置自定义ObjectMapper
属性。
public class ConnectionFactory {
private final Client client;
public ConnectionFactory() {
ClientConfig clientConfig = new ClientConfig();
clientConfig.property(ClientProperties.FOLLOW_REDIRECTS, true);
clientConfig.property(ClientProperties.CONNECT_TIMEOUT, connTimeoutSec * 1000);
clientConfig.property(ClientProperties.READ_TIMEOUT, readTimeoutSec * 1000);
this.client = ClientBuilder.newBuilder().register(JacksonConfigurator.class).register(JacksonFeature.class).withConfig(clientConfig).build();
// Some more code here ...
}
}
根据Jackson Registration文档中的示例8.15,应将以下JacksonConfigurator类注册到客户端。
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Provides custom configuration for jackson.
*/
@Provider
public class JacksonConfigurator implements ContextResolver<ObjectMapper> {
private final ObjectMapper mapper;
public JacksonConfigurator() {
mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
}
如果我反序列化来自客户端的响应,则客户端应忽略响应中无法识别的字段。但我收到以下错误-
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "xyz" (class LookupData), not marked as ignorable (3 known properties: "abc", "pqr", "def"])
at [Source: org.glassfish.jersey.message.internal.EntityInputStream@55958273; line: 1, column: 11] (through reference chain: LookupData["xyz"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51)
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:731)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:915)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1292)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1270)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:247)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
at com.fasterxml.jackson.databind.ObjectReader._bind(ObjectReader.java:1232)
at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:676)
at com.fasterxml.jackson.jaxrs.base.ProviderBase.readFrom(ProviderBase.java:800)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:257)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:229)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:149)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1124)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:851)
... 39 more
有人可以让我知道在注册JacksonConfigurator类时是否缺少什么吗?
答案
尝试使用已正确配置的JacksonJsonProvider
初始化Jersey客户端:
final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
final Client client = ClientBuilder.newClient(new ClientConfig(jacksonJsonProvider));
[这已通过Jackson 2.5.1和Jersey 2.17进行了测试
另一答案
我遇到了同样的问题,正在寻找解决方案……找到您的主张,它对我有用:)
我的测试非常基础/请参见下面的内容:
Client client = ClientBuilder.newClient()
.register(JacksonFeature.class).register(JacksonConfigurator.class);
WebTarget wt = client.target(REST_SERVICE_URL).path(
"amount");
wt = wt.queryParam("from", "USD");
ConverterResponse cr=wt.request().get(ConverterResponse.class);
如果我没有注册您的JacksonConfigurator,我将得到与您相同的例外:
UnrecognizedPropertyException: Unrecognized field
因为converterResponse定义了REST响应对象方法的子集。
非常感谢!
另一答案
它对我有用。球衣版本2.30
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
import com.fasterxml.jackson.databind.DeserializationFeature;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
ClientConfig config = new ClientConfig();
JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
config.register(jacksonJsonProvider);
client = ClientBuilder.newClient(config);
以上是关于Jersey 2.8客户端在反序列化期间没有忽略未知属性的主要内容,如果未能解决你的问题,请参考以下文章
用于在反序列化期间忽略未知属性的 SpringMVC 全局设置
Asp Core 3.1 API JsonIgnore (not) 在反序列化中被忽略