Getting an Unrecognized field “Status” (Class ektron.cms.jdbc.extractors.AssetDataResponse), not marked as ignorable error with Jersey Client API
我从RESTful服务返回以下JSON
1 2 3 4 5 6 | { "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反序列化为此数据类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 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); } } |
以下是我的客户代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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 |
但是,如果我将上述内容更改为:
1 2 3 4 5 6 | AssetDataResponse resp = webResource .path("3f7078c4") .path("ceb3dc64-33ed-4e96-80a2-846120ecd9ee") .accept(MediaType.APPLICATION_JSON) .get(AssetDataResponse.class); |
我收到以下错误:
Unrecognized field"Status" (Class ektron.cms.jdbc.extractors.AssetDataResponse), not marked as ignorable
我需要在ClientConfig上进行任何配置才能使反序列化正常工作吗?任何有关这方面的帮助将非常感谢。我是.NET开发人员,对Java很新,对Jersey框架不太熟悉。我已经从一个类似的问题检查了答案,我的案例与那里列出的案例不同。
客户端罐子
- 注释-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
- 杰克逊 - 数据绑定 - 2.1.3.jar
- 杰克逊JAXRS-1.9.2.jar
- 杰克逊映射器-ASL-1.9.11.jar
- 杰克逊-XC-1.9.2.jar
- jcommander-1.30.jar
- 新泽西州Apache的客户1.17.jar
- 球衣原子的阿布德拉-1.17.jar
- 新泽西州的客户 - 1.17.jar
- 新泽西州的核心1.17.jar
- 新泽西州吉斯 - 1.17.jar
- 新泽西州JSON-1.17.jar
- 新泽西州的多,1.17.jar
- 新泽西服务器1.17.jar
- 新泽西州的servlet-1.17.jar
- 抛放-1.1.jar
- JSR311的API-1.1.1.jar
- 验证-API 1.0.0.GA.jar
我解决了这个问题。 问题是我正在从"Perception"指出的两个版本的Jackson库中导入类。 我现在可以使用Jersey客户端的最新稳定版Jackson库。
感谢Perception的建议。
这是我的客户端代码现在的样子。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 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; } |
我不得不包括以下行失败,我再次遇到原始错误
1 | clientConfig.getClasses().add(JacksonJsonProvider.class); |
从JacksonJsonProvider导入
1 2 | //jackson-jaxrs-json-provider-2.1.3.jar import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; |
我的罐子现在看起来像。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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 |