gpt4 book ai didi

json - Jersey 2.8客户端在反序列化期间不会忽略未知属性

转载 作者:行者123 更新时间:2023-12-04 13:27:00 27 4
gpt4 key购买 nike

我正在使用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对此进行了测试

关于json - Jersey 2.8客户端在反序列化期间不会忽略未知属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26393310/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com