我遇到了反序列化JSON字符串的问题。我想我尝试了一切,但我卡住了。到目前为止这是我的代码。行家:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.2</version>
</dependency>
实体:
@JsonIgnoreProperties(ignoreUnknown = true)
public class ImportTask {
@JsonProperty("ID")
private long id;
@JsonProperty("Name")
private String name;
@JsonProperty("Description")
private String description;
public ImportTask() {
}
// getters and setters
}
客户端:
List<ImportTask> importTaskList = new ArrayList<ImportTask>();
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
JacksonJaxbJsonProvider jacksonJaxbJsonProvider = new JacksonJaxbJsonProvider();
jacksonJaxbJsonProvider.setMapper(mapper);
Client client = ClientBuilder.newClient(new ClientConfig(jacksonJaxbJsonProvider));
Response response= client
.target("http://localhost:51873/api/getImportTaskList")
.request(MediaType.APPLICATION_JSON)
.get();
importTaskList = response.readEntity(new GenericType<List<ImportTask>>(){});
这是我从webservice获得的JSON字符串:
"[{\"ID\":16,\"Name\":\"name\",\"Description\":\"description\"}]"
我收到此错误
javax.ws.rs.ProcessingException: Error reading entity from input stream.
.......
引起:com.fasterxml.jackson.databind.JsonMappingException:可以 不要从VALUE_STRING中反序列化java.util.ArrayList的实例 令牌
与Id相关的错误是否作为int而不是字符串传递? 任何帮助都会得到很多赞赏。
答案 0 :(得分:0)
Sotirios是对的。
问题在于webservice返回字符串而不是json。
答案 1 :(得分:0)
您返回的字符串不是jsonString。
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.readValue("returning",String.class);
// then,your operation...