【问题标题】:Invoking a rest api client调用 rest api 客户端
【发布时间】:2017-03-30 09:19:51
【问题描述】:

我在eclipse中创建了一个rest api作为一个maven项目。 rest 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的新手..请帮助我尽快解决这个问题。提前谢谢

【问题讨论】:

  • 请在您的问题中包含 MobileAnalyticsModel 类代码
  • 你得到了哪个例外?你试过 RESTEasy 了吗?

标签: java json eclipse maven restclientbuilder


【解决方案1】:

您的响应是一个字符串。访问 JSON 响应元素的最简单方法是将响应转换为 Json-Object。然后,您可以通过名称轻松访问这些字段。 看一下: How to parse JSON in Java

【讨论】:

    【解决方案2】:

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

    Parse a JSON response as an object

    【讨论】:

      【解决方案3】:

      这段代码对我有用..

       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")); }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-20
        • 2018-01-24
        • 1970-01-01
        • 2017-04-20
        • 2015-11-29
        • 2017-08-11
        相关资源
        最近更新 更多