Sukhen Bhattacharjee
Sukhen Bhattacharjee

Reputation: 21

Not getting external Json API response using RestTemplate

Getting External API As below image- [1]: https://i.sstatic.net/a28Y1.png

{
"status" : 200,
"data" : [ 
{
"_id": "StringValue1",
"itmId": "StringValue1",
"itmName": "StringValue1",
"imgFileName":"StringValue1",
"imgFile":"byte[] data"
},
{
"_id": "StringValue2",
"itmId": "StringValue2",
"itemName": "StringValue2",
"imgFileName":"StringValue2",
"imageFile":"byte[] data2"
}
],
"message":""
}

Used as data array object.

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Response {     
public String status;
Data [] data;
public String message
}

Another Object is below

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Data{              
public String _id;
public String itmid;
public String itmName;
public String imgFileName;
byte [] imgFile;
}

Used /getForObject/exchange/getForEntity but not getting any response. Pls check at bottom method I used.

Now used as List of Data Object also not getting any response-

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Response {     
public String status;
List<Data> data;
public String message
}


@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Data{              
public String _id;
public String itmid;
public String itmName;
public String imgFileName;
byte [] imgFile;
}

Here how called these by resttemplate-

  1. Response obj=restTemplate.getForObject(uri, Response.class,"paramvalue");
  2. ResponseEntity obj=restTemplate.exchange(uri, HttpMethod.GET,null,Response.class,"paramvalue");
  3. ResponseEntity obj=restTemplate.getForEntity(uri, Response.class,"paramvalue");

Tried all the possible ways, but not getting any response from external rest API.

Please note response json conatin data[] where we have byte data for image. Need support and help pls..

Can any one support on this? Thanks in advance.

Upvotes: 0

Views: 421

Answers (1)

Felix K Jose
Felix K Jose

Reputation: 892

You can achieve what you are looking by following:

ResponseEntity<List<Data>> responseEntity = 
  restTemplate.exchange(
    BASE_URL,
    HttpMethod.GET,
    null,
    new ParameterizedTypeReference<List<Data>>() {}
  );
List<Data> data = responseEntity.getBody();

Please let me know if it helps

Upvotes: 0

Related Questions