我在eclipse中创建了一个rest api作为一个maven项目。用于休息 api 的 MobileAnalyticsModel 类是
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响应.. as
[{"created":"2017-03-30T14:36:58.56","location":"http://api.server.com","name":"Mobile Analytics","prize":"$1.00 per 1,000,000 Amazon Mobile Analytics events per month thereafter","requirement":"PutEvents"}]
但我希望将单个参数作为我的输出,例如名称、位置或要求。我也在同一个 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 响应中获取单个名称参数?我是这个rest api的新手..请帮助我尽快解决这个问题。提前谢谢