我正在使用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课程时遗失了某些内容,有人可以告诉我吗?
答案 0 :(得分:9)
尝试使用已正确配置的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进行测试
答案 1 :(得分:3)
我有同样的问题,并寻找解决方案......找到你的命题,它对我有用:)
我的测试非常基础/见下文:
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 :(得分:0)
Jersey版本2.30:
例外: 引起原因:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“ failedLoginCount” 在[资料来源:org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@77fbf8c9;行:1,列:100](通过参考链:com.xx.XXX [“ failedLoginCount”]) 在com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:62)
它适用于以下代码:
<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;
public class ServiceClient {
private Client client;
public ServiceClient() {
ClientConfig config = new ClientConfig();
JacksonJsonProvider
jacksonJsonProvider = new JacksonJaxbJsonProvider().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
config.register(jacksonJsonProvider);
client = ClientBuilder.newClient(config);
}
}