调用休息 api 客户端
Invoking a rest api client
我在eclipse中创建了一个restapi作为maven项目。
其余 api 的 MobileAnalyticsModel class 是
package org.subhayya.amazonws.mobileanalytics;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class MobileAnalyticsModel {
private String name;
private Date created;
private String location;
private String prize;
private String requirement;
public MobileAnalyticsModel() {
}
public MobileAnalyticsModel(String name, String location, String prize, String requirement) {
this.name = name;
this.location = location;
this.prize = prize;
this.requirement = requirement;
this.created = new Date();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getPrize() {
return prize;
}
public void setPrize(String prize) {
this.prize = prize;
}
public String getRequirement() {
return requirement;
}
public void setRequirement(String requirement) {
this.requirement = requirement;
}
}
这是创建的 api 的 json 响应:
和
这是我调用 rest api:
的示例测试代码
package org.subhayya.example;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
public class SampleTestREstClient {
public static void main(String[] args) {
Client client = ClientBuilder.newClient( );
String reponse = client.target("http://localhost:8080/AgentBasedCloudServiceCompositionFramework/webapi/mobileanalytics/mobileanalyticsjson")
.request(MediaType.APPLICATION_JSON)
.get(String.class);
System.out.println(reponse);
}}
然后我得到了完整的 json 回复.. 作为
[{"created":"2017-03-30T14:36:58.56","location":"http://api.server.com","name":"Mobile Analytics","prize":".00 per 1,000,000 Amazon Mobile Analytics events per month thereafter","requirement":"PutEvents"}]
但我想将单个参数作为我的输出,例如名称、位置或 requirement.I 我也在同一个 Maven 项目中创建客户端调用代码。所以我写了如下代码
Client client = ClientBuilder.newClient( );
MobileAnalyticsModel reponse =
client.target("http://localhost:8080/AgentBasedCloudServiceCompositionFramework/webapi/mobileanalytics/mobileanalyticsjson")
.request(MediaType.APPLICATION_JSON)
.get(MobileAnalyticsModel.class);
System.out.println(reponse.getName());
但我遇到异常,所以我将其更改为 System.out.println(reponse);
) 至少得到 JSON 响应,然后也出现错误。
如何从 JSON 响应中获取单个名称参数?我是这个休息的新手api..请尽快possible.thanks提前帮我解决这个问题
您的回复是一个字符串。访问 JSON-response 元素的最简单方法是将 resonse 转换为 Json-Object。然后您可以通过名称轻松访问这些字段。
看一下:
How to parse JSON in Java
您还可以检查下面的 link 以将 json 转换为对象。
Parse a JSON response as an object
这段代码对我有用..
String url = "http://localhost:8080/AgentBasedCloudServiceCompositionFramework/webapi/mobileanalytics/";
String city = "mobileanalyticsjson";
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.register(JsonProcessingFeature.class).target(url);
JsonArray jsonArray = webTarget.path(city)
.request(MediaType.APPLICATION_JSON_TYPE).get(JsonArray.class);
for (JsonObject jsonObject : jsonArray.getValuesAs(JsonObject.class)) {
System.out.println(jsonObject.getString("name"));
System.out.println(jsonObject.getString("location")); }
我在eclipse中创建了一个restapi作为maven项目。 其余 api 的 MobileAnalyticsModel class 是
package org.subhayya.amazonws.mobileanalytics;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class MobileAnalyticsModel {
private String name;
private Date created;
private String location;
private String prize;
private String requirement;
public MobileAnalyticsModel() {
}
public MobileAnalyticsModel(String name, String location, String prize, String requirement) {
this.name = name;
this.location = location;
this.prize = prize;
this.requirement = requirement;
this.created = new Date();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getPrize() {
return prize;
}
public void setPrize(String prize) {
this.prize = prize;
}
public String getRequirement() {
return requirement;
}
public void setRequirement(String requirement) {
this.requirement = requirement;
}
}
这是创建的 api 的 json 响应:
和 这是我调用 rest api:
的示例测试代码package org.subhayya.example;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
public class SampleTestREstClient {
public static void main(String[] args) {
Client client = ClientBuilder.newClient( );
String reponse = client.target("http://localhost:8080/AgentBasedCloudServiceCompositionFramework/webapi/mobileanalytics/mobileanalyticsjson")
.request(MediaType.APPLICATION_JSON)
.get(String.class);
System.out.println(reponse);
}}
然后我得到了完整的 json 回复.. 作为
[{"created":"2017-03-30T14:36:58.56","location":"http://api.server.com","name":"Mobile Analytics","prize":".00 per 1,000,000 Amazon Mobile Analytics events per month thereafter","requirement":"PutEvents"}]
但我想将单个参数作为我的输出,例如名称、位置或 requirement.I 我也在同一个 Maven 项目中创建客户端调用代码。所以我写了如下代码
Client client = ClientBuilder.newClient( );
MobileAnalyticsModel reponse =
client.target("http://localhost:8080/AgentBasedCloudServiceCompositionFramework/webapi/mobileanalytics/mobileanalyticsjson")
.request(MediaType.APPLICATION_JSON)
.get(MobileAnalyticsModel.class);
System.out.println(reponse.getName());
但我遇到异常,所以我将其更改为 System.out.println(reponse);
) 至少得到 JSON 响应,然后也出现错误。
如何从 JSON 响应中获取单个名称参数?我是这个休息的新手api..请尽快possible.thanks提前帮我解决这个问题
您的回复是一个字符串。访问 JSON-response 元素的最简单方法是将 resonse 转换为 Json-Object。然后您可以通过名称轻松访问这些字段。 看一下: How to parse JSON in Java
您还可以检查下面的 link 以将 json 转换为对象。
Parse a JSON response as an object
这段代码对我有用..
String url = "http://localhost:8080/AgentBasedCloudServiceCompositionFramework/webapi/mobileanalytics/";
String city = "mobileanalyticsjson";
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.register(JsonProcessingFeature.class).target(url);
JsonArray jsonArray = webTarget.path(city)
.request(MediaType.APPLICATION_JSON_TYPE).get(JsonArray.class);
for (JsonObject jsonObject : jsonArray.getValuesAs(JsonObject.class)) {
System.out.println(jsonObject.getString("name"));
System.out.println(jsonObject.getString("location")); }