如何使用 javax.ws.rs.client.Invocation.Builder 忽略无法识别的属性?

我已经构建了一个使用 SSL 的 Web 客户端。

客户端初始化如下:

SSLContext sc = SSLContext.getInstance("ssl");
        sc.init(null,noopTrustManager,null);
        
        this.client = ClientBuilder.newBuilder().
                sslContext(sc).
                build();

然后像这样初始化 web 目标:

this.webTarget = this.client.target(urlAddress);

invocation Builder 是这样初始化的:

Builder request = this.webTarget.request();

但是当 - 在适当设置标题之后 - 我尝试这样做:

request.get(InitiateTransferResponse.class);

我收到以下错误消息:

javax.ws.rs.client.ResponseProcessingException: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "timestamp" (class com.interaxa.ivr.visual.moa.service.model.InitiateTransferResponse),不是标记为可忽略

有没有办法告诉构建器忽略所有无法识别的属性? (我也打算将这个相同的构建器用于其他 Web 服务,如果我可以将其设置为永久忽略未知属性,而不管响应类如何,那就太好了。

提前致谢。

wujianlin1984 回答:如何使用 javax.ws.rs.client.Invocation.Builder 忽略无法识别的属性?

我找到了解决方案!

关键是换行: request.get(InitiateTransferResponse.class);

通过这个代码:

        InitiateTransferResponse result;
        try {
            result = getMapper().readValue(response,InitiateTransferResponse.class);
        } 
        catch (IOException e) {
            ViewFactory.logError(e);
            result = null;
        }

其中 getMapper() 如下:

public ObjectMapper getMapper() {
        if (mapper == null){
            mapper = new ObjectMapper();
            mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,false);
            mapper.configure(DeserializationConfig.Feature.FAIL_ON_NULL_FOR_PRIMITIVES,false);
            mapper.setSerializationInclusion(Inclusion.NON_NULL);
        }
        return mapper;
}

ObjectMapper 是 org.codehaus.jackson.map.ObjectMapper

这样,ObjectMapper 可以控制它允许通过的内容,而不是将决定权留给 Builder。

本文链接:https://www.f2er.com/1118759.html

大家都在问