0

我从 RESTful 服务返回了以下 JSON

{
    "Status": "Success",
    "Success": true,
    "Path": "D:\\Work\\Sites\\EKSites\\OnTrek\\privateassets\\0\\155\\156\\ceb3dc64-33ed-4e96-80a2-846120ecd9ee.pdf",
    "Timestamp": "2013-03-27T18:35:23.8997358-04:00"
}

我正在尝试将 JSON 反序列化为这个数据类:

package ektron.cms.jdbc.extractors;

@JsonPropertyOrder({ "Status", "Success", "Path", "Timestamp" })
public class AssetDataResponse {

    @JsonProperty("Status")
    private String Status;

    @JsonProperty("Success")
    private Boolean Success;

    @JsonProperty("Path")
    private String Path;

    @JsonProperty("Timestamp")
    private String Timestamp;

    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("Status")
    public String getStatus() {
        return Status;
    }

    @JsonProperty("Status")
    public void setStatus(String Status) {
        this.Status = Status;
    }

    @JsonProperty("Success")
    public Boolean getSuccess() {
        return Success;
    }

    @JsonProperty("Success")
    public void setSuccess(Boolean Success) {
        this.Success = Success;
    }

    @JsonProperty("Path")
    public String getPath() {
        return Path;
    }

    @JsonProperty("Path")
    public void setPath(String Path) {
        this.Path = Path;
    }

    @JsonProperty("Timestamp")
    public String getTimestamp() {
        return Timestamp;
    }

    @JsonProperty("Timestamp")
    public void setTimestamp(String Timestamp) {
        this.Timestamp = Timestamp;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperties(String name, Object value) {
        this.additionalProperties.put(name, value);
    }
}

以下是我的客户端代码:

package ektron.common.network;
//...
ClientConfig clientConfig = new DefaultClientConfig();

clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

clientConfig.getClasses().add(JacksonJsonProvider.class);
client = Client.create(clientConfig);

WebResource webResource = 
    client.resource(
            String.format("http://%s:%d/%s","localhost",7605,"asset"));

String return =             
webResource
    .path("3f7078c4")
    .path("ceb3dc64-33ed-4e96-80a2-846120ecd9ee")
    .accept(MediaType.APPLICATION_JSON)
    .get(String.class); //This piece works and I get my JSON response as 
                        //indicated above

但是,如果我将以上内容更改为:

AssetDataResponse resp = 
        webResource
            .path("3f7078c4")
            .path("ceb3dc64-33ed-4e96-80a2-846120ecd9ee")
            .accept(MediaType.APPLICATION_JSON)
            .get(AssetDataResponse.class); 

我收到以下错误:

无法识别的字段“状态”(类 ektron.cms.jdbc.extractors.AssetDataResponse),未标记为可忽略

我需要在 ClientConfig 上进行任何配置以使反序列化正常工作吗?对此的任何帮助将不胜感激。我是 .NET 开发人员,对 Java 很陌生,对 Jersey 框架不太熟悉。我已经检查了一个类似问题的答案,我的案例与那里列出的案例不同。

客户端罐子

  • annotations-1.3.9.jar
  • asm-3.1.jar
  • codemodel-2.4.1.jar
  • 杰克逊注释-2.1.2.jar
  • 杰克逊核心2.1.3.jar
  • 杰克逊核心asl-1.9.11.jar
  • jackson-databind-2.1.3.jar
  • 杰克逊-jaxrs-1.9.2.jar
  • jackson-mapper-asl-1.9.11.jar
  • 杰克逊-xc-1.9.2.jar
  • jcommander-1.30.jar
  • jersey-apache-client-1.17.jar
  • jersey-atom-abdera-1.17.jar
  • jersey-client-1.17.jar
  • jersey-core-1.17.jar
  • jersey-guice-1.17.jar
  • jersey-json-1.17.jar
  • jersey-multipart-1.17.jar
  • jersey-server-1.17.jar
  • jersey-servlet-1.17.jar
  • 抛弃-1.1.jar
  • jsr311-api-1.1.1.jar
  • 验证-api-1.0.0.GA.jar
4

1 回答 1

0

我解决了这个问题。问题是我正在从两个版本的杰克逊库中导入类,正如“感知”所指出的那样。我现在可以将最新稳定版本的 Jackson 库与 Jersey 客户端一起使用。

感谢感知您的建议。

这是我的客户端代码现在的样子。

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;

...
    /**
 * Wrapper class for a RESTful Service
 */
public class RestServiceWrapper
{


    public ResponseData performRequest(
                List<String> reqArgs,
                ResponseType rType,
                Class<?> dataType
                )
    {
        ResponseData data = new ResponseData();

        for(String arg: nullCheck(reqArgs))
        {
            if(StringUtils.isNotEmpty(arg))
            {
                webResource=webResource.path(arg);
            }
        }
        data.data = webResource
                .accept(MediaType.APPLICATION_JSON) //For now only JSON supported
                .get(AssetDataResponse.class);
        data.rType = ResponseType.JSON;




        return data;
    }

    protected ServiceInfo svcInfo;



    public RestServiceWrapper(ServiceInfo svcInfo) {

        ClientConfig clientConfig = new DefaultClientConfig();
        clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
        clientConfig.getClasses().add(JacksonJsonProvider.class);
        client = Client.create(clientConfig);
        this.svcInfo = svcInfo;
        this.webResource = 
                client.resource(
                        String.format("http://%s:%d/%s",svcInfo.Host,svcInfo.Port,svcInfo.EndPoint));
    }

    private static <T> Iterable<T> nullCheck(Iterable<T> iterable) {
        return iterable == null ? Collections.<T>emptyList() : iterable;
    }

    protected Client client;
    protected WebResource webResource;
}

我必须包括以下失败的行,我再次遇到了原始错误

clientConfig.getClasses().add(JacksonJsonProvider.class);

从导入的 JacksonJsonProvider

//jackson-jaxrs-json-provider-2.1.3.jar
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;

我的罐子现在看起来像。

annotations-1.3.9.jar
asm-3.1.jar
codemodel-2.4.1.jar
jackson-annotations-2.1.2.jar
jackson-core-2.1.3.jar
jackson-databind-2.1.3.jar
jackson-jaxrs-json-provider-2.1.3.jar
jcommander-1.30.jar
jersey-apache-client-1.17.jar
jersey-atom-abdera-1.17.jar
jersey-client-1.17.jar
jersey-core-1.17.jar
jersey-guice-1.17.jar
jersey-json-1.17.jar
jersey-multipart-1.17.jar
jersey-server-1.17.jar
jersey-servlet-1.17.jar
jettison-1.1.jar
jsonschema2pojo-core-0.3.5.jar
jsr311-api-1.1.1.jar
validation-api-1.0.0.GA.jar
于 2013-03-29T16:47:51.010 回答