0

我在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 响应:

这是创建的 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的新手..请帮助我尽快解决这个问题。提前谢谢

4

3 回答 3

1

您的回复是一个字符串。访问 JSON 响应元素的最简单方法是将响应转换为 Json-Object。然后,您可以通过名称轻松访问这些字段。看看: 如何在 Java 中解析 JSON

于 2017-03-30T09:23:17.940 回答
0

这段代码对我有用..

 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")); }
于 2017-03-31T05:57:47.267 回答
0

您还可以查看以下链接以将 json 转换为对象。

将 JSON 响应解析为对象

于 2017-03-30T09:36:32.930 回答