简体   繁体   中英

How to get Spring rest API response in java code?

I done Spring rest api .that is returning json data now i want to call that api in my system(remote by ip) .how to get that response in my java code Or Rest Template.

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

]}

how to read in java using rest Template .

What you want is consuming a rest api in Java. This should get you started.

Its a pretty much a basic need and its all over the web on how to consume rest apis.

This and this are good starting points.

Quoting the relevant parts here:

  1. Use POST to Create a Resource

In order to create a new Resource in the API – we can make good use of the postForLocation(), postForObject() or postForEntity() APIs.

The first returns the URI of the newly created Resource while the second returns the Resource itself.

4.1. The 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"));

4.2. The postForLocation API

Similarly, let's have a look at the operation that – instead of returning the full Resource, just returns the Location of that newly created Resource:

HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
URI location = restTemplate.postForLocation(fooResourceUrl, request);
assertThat(location, notNullValue());

4.3. The exchange API Finally, let's have a look at how to do a POST with the more generic exchange API:

RestTemplate restTemplate = new RestTemplate();
HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
ResponseEntity<Foo> response = restTemplate.
exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class);
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
Foo foo = response.getBody();
assertThat(foo, notNullValue());
assertThat(foo.getName(), is("bar"));

You'd do good with the post exchange API.

Update

Your json response is not valid. You need to change the rest api to return something like this

{
"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"
    }
 ]
}

When consuming this rest api, the response object will be

public class ResponseObject { 
private List<BiomatrixResult> results;

//getter setters
}

where your BiomatrixResult object will be

public class BiomatrixResult { 
private int deviceId; 
private String userId;
private String userName;
private Date date; 
//getters setters

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM