再会,
我目前正在尝试使用 Jackson(与 Jersey)使用生成 JSON(用 .NET 编写)的 REST 服务进行集成。JSON 由可能的错误消息和对象数组组成。下面是由 Jersey 的日志过滤器返回的 JSON 示例:
{
"error":null,
"object":"[{\"Id\":16,\"Class\":\"ReportType\",\"ClassID\":\"4\",\"ListItemParent_ID\":4,\"Item\":\"Pothole\",\"Description\":\"Pothole\",\"Sequence\":1,\"LastEditDate\":null,\"LastEditor\":null,\"ItemStatus\":\"Active\",\"ItemColor\":\"#00AF64\"}]"
}
我有两个类来表示类型(外部 ListResponse):
public class ListResponse {
public String error;
public ArrayList<ListItem> object;
public ListResponse() {
}
}
和(内部 ListItem):
public class ListItem {
@JsonProperty("Id")
public int id;
@JsonProperty("Class")
public String classType;
@JsonProperty("ClassID")
public String classId;
@JsonProperty("ListItemParent_ID")
public int parentId;
@JsonProperty("Item")
public String item;
@JsonProperty("Description")
public String description;
@JsonAnySetter
public void handleUnknown(String key, Object value) {}
public ListItem() {
}
}
调用并返回 JSON 的类如下所示:
public class CitizenPlusService {
private Client client = null;
private WebResource service = null;
public CitizenPlusService() {
initializeService("http://localhost:59105/PlusService/");
}
private void initializeService(String baseURI) {
// Use the default client configuration.
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getClasses().add(JacksonJsonProvider.class);
client = Client.create(clientConfig);
// Add a logging filter to track communication between server and client.
client.addFilter(new LoggingFilter());
// Add the base URI
service = client.resource(UriBuilder.fromUri(baseURI).build());
}
public ListResponse getListItems(String id) throws Exception
{
ListResponse response = service.path("GetListItems").path(id).accept(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE).get(ListResponse.class);
return response;
}
}
这里重要的调用是 getListItems 方法。在测试工具中运行代码,会产生以下结果:
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token
at [Source: java.io.StringReader@49497eb8; line: 1, column: 14] (through reference chain: citizenplus.types.ListResponse["object"])
请协助。
问候,卡尔 - 彼得迈耶