Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 调用RESTAPI客户端_Java_Json_Eclipse_Maven_Restclientbuilder - Fatal编程技术网

Java 调用RESTAPI客户端

Java 调用RESTAPI客户端,java,json,eclipse,maven,restclientbuilder,Java,Json,Eclipse,Maven,Restclientbuilder,我在eclipse中创建了一个RESTAPI作为maven项目。 rest api的MobileAnalyticsModel类为 package org.subhayya.amazonws.mobileanalytics; import java.util.Date; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class MobileAnalyticsModel { privat

我在eclipse中创建了一个RESTAPI作为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响应。。作为

 [{"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"}]
但我希望将单个参数作为输出,例如名称、位置或需求。所以我写了我的代码如下

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(response)
)至少获取JSON响应,然后还获取错误


如何从JSON响应中获取单一名称参数?我不熟悉此rest api..请帮助我尽快修复此问题。提前感谢您的回复是字符串。访问JSON响应元素的最简单方法是将响应转换为JSON对象。然后,您可以通过字段的名称轻松访问这些字段。 看看:

您还可以查看下面的链接,将json转换为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")); }

请在您的问题中包含MobileAnalyticsModel类代码,您会遇到哪些异常?你试过放松吗?