【发布时间】:2015-04-09 23:10:07
【问题描述】:
我正在开发一个 httpclient 应用程序,它使用来自 JSON 格式的 RESTful Web 服务的list<OBJECT>。
我想以 JSON 格式使用此结果。
这是我的 httpclient 应用程序类:
public class ClientAbortMethod {
public final static void main(String[] args) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpGet httpget = new HttpGet("http://localhost:8080/structure/alldto");
System.out.println("Executing request " + httpget.getURI());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
String data= EntityUtils.toString(response.getEntity());
System.out.println(data);
// Do not feel like reading the response body
// Call abort on the request object
httpget.abort();
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
}
这是执行这段代码的结果
[{"ids":"#AB","champs":[ {"idChamp":1,"order":1,"type":"FROM"},{"idChamp":2,"order":2,"type":"TO"},{"idChamp":3,"order":3,"type":"TEXT"}]},{"ids":"#AC","champs":[{"idChamp":4,"order":1,"type":"FROM"},{"idChamp":5,"order":2,"type":"TO_MAIL"},{"idChamp":6,"order":3,"type":"TEXT"}]},{"ids":"tt","champs":[]}]
这是宁静的网络服务
@RequestMapping("/alldto")
public List<StructureNotificationDto> GetALLStructureNotification() {
return StructureNotif.StructureDTOS();
}
我怎样才能获得 JSON 格式或其他易于操作的格式的 Web 服务的结果?
【问题讨论】:
-
我不明白。
data包含响应内容为String,响应内容为 JSON。你还想要什么? JSON 只是文本。 -
数据以String格式表示Web服务的结果,但我想使用JSON或List
-
因为 web 服务返回一个列表
标签: java arrays json rest httpclient