Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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代码中获得SpringRESTAPI响应?_Java_Spring_Api - Fatal编程技术网

如何在java代码中获得SpringRESTAPI响应?

如何在java代码中获得SpringRESTAPI响应?,java,spring,api,Java,Spring,Api,我完成了SpringRESTAPI。这是返回json数据,现在我想在我的系统中调用该api(通过ip远程)。如何在我的java代码中获得该响应 或Rest模板 {[ { "deviceId": 1, "userId": "100", "userName": "Jee", "date": "2016-09-19 00:00:00.000" }, . . . n ]} 如何使用rest模板在java中阅读。您需要的是在java中使用rest

我完成了SpringRESTAPI。这是返回json数据,现在我想在我的系统中调用该api(通过ip远程)。如何在我的java代码中获得该响应 或Rest模板

{[
  {
    "deviceId": 1,
    "userId": "100",
    "userName": "Jee",
    "date": "2016-09-19 00:00:00.000"
  },
  .
  .
  .
  n

]}

如何使用rest模板在java中阅读。

您需要的是在java中使用rest api。应该可以开始了。

这几乎是一个基本的需求,在如何使用RESTAPI方面,它遍布整个web

这些都是很好的起点

在此引用相关部分:

  • 使用POST创建资源
  • 为了在API中创建新资源,我们可以充分利用postForLocation()、postForObject()或postForEntity()API

    第一个返回新创建的资源的URI,而第二个返回资源本身

    4.1。postForObject API

    ClientHttpRequestFactory requestFactory = getClientHttpRequestFactory();
    RestTemplate restTemplate = new RestTemplate(requestFactory);
    
    HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
    Foo foo = restTemplate.postForObject(fooResourceUrl, request, Foo.class);
    assertThat(foo, notNullValue());
    assertThat(foo.getName(), is("bar"));
    
    使用此rest api时,响应对象将

    public class ResponseObject { 
    private List<BiomatrixResult> results;
    
    //getter setters
    }
    
    public class BiomatrixResult { 
    private int deviceId; 
    private String userId;
    private String userName;
    private Date date; 
    //getters setters
    
    }
    

    我已经发布了api{[{“deviceId”:1,“userId”:“100”,“userName”:“Jee”,“date”:“2016-09-19 00:00:00.000”},{“deviceId”:1,“userId”:“100”,“userName”:“Jee”,“date”:“2016-09-19 00:00:00.000”}我有那种类型的json如何在javaDATA中使用restTempate读取,比如公共类BiomatrixResult{@JsonProperty(“deviceId”)@XmlElement(required=true)private int deviceId;@JsonProperty(“userId”)@XmlElement(required=true)private String userId;@JsonProperty(“userName”)@xmlement(required=true)私有字符串用户名;@JsonProperty(“日期”)@xmlement(必需=true)private String date;您可以创建一个表示响应数据的pojo类。您确实需要所有这些注释。一个简单的pojo类就可以了。属性名称将映射为json key values.org.springframework.web.client.HttpClientErrorException:415不支持的媒体类型,原因是:null。列表中的每个项代表什么一张什么的清单?
    {
    "results": [
        {
            "deviceId": 1,
            "userId": "100",
            "userName": "Jee",
            "date": "2016-09-19 00:00:00.000"
        },
        {
            "deviceId": 1,
            "userId": "100",
            "userName": "Jee",
            "date": "2016-09-19 00:00:00.000"
        }
     ]
    }
    
    public class ResponseObject { 
    private List<BiomatrixResult> results;
    
    //getter setters
    }
    
    public class BiomatrixResult { 
    private int deviceId; 
    private String userId;
    private String userName;
    private Date date; 
    //getters setters
    
    }