New to spring and everything. Trying to hit an endpoint and retrieve the information in the response field. However I don't know how to access information more specific than just bringing back the whole response since it isn't nested in key/values?
I'm using rest template + MappingJacksonHttpMessageConverter
. I would like to map the information within response to a pojo, just unsure how to go any further or if I'm even in the right direction here. Example code below.
Postman response:
{
"success": true,
"message": "Success",
"body": {
"response": "TopStatus=Completed,TopRanking=4 - reqs met, TopDate=2014-04-23,TopTime=11:00 AM,TopEndTime=1:30 AM"
},
"status": 200
}
Method that is trying to map to Pojo
public TopStatus getTopAppStatus(int topId, Status status) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HashMap<String, String> statusObj = new HashMap<String, String>();
statusObj.put("topStatus", "Fail");
statusObj.put("topId", "93");
RestTemplate rt = getRestTemplate();
URI uri = new URI("urihere");
HttpEntity<HashMap> entity = new HttpEntity<HashMap>(statusObj, headers);
TopStatus response = rt.postForObject(uri, entity, TopStatus.class);
return response;
}
Pojo example on how I have it set up
@JsonIgnoreProperties(ignoreUnknown = true)
public class TopStatus{
@JsonProperty("TopStatus")
private String topStatus;
rest of fields, setters, getters etc...
}
You can use postForEntity() method returns ResponseEntity type object instead of postForObject()
you need to do small change that will map the response body to the POJO
ResponseEntity<TopStatus> response = rt.postForEntity(uri, entity, TopStatus.class);
TopStatus topStatusResponse = response.getBody();
return topStatusResponse;
This should return only the expected POJO.
Try to use below code.
public TopStatus getTopAppStatus(int topId, Status status) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
TopStatus model = new TopStatus();
model.setTopStatus("Fail");
model.setTopId("93");
RestTemplate rt = getRestTemplate();
URI uri = new URI("urihere");
HttpEntity<> entity = new HttpEntity<HashMap>(model, headers);
TopStatus response = rt.postForObject(uri, entity, TopStatus.class);
return response;
}
Modified Code.
public TopStatus getTopAppStatus(int topId, Status status) {
RestTemplate restTemplate = new RestTemplate();
// Add the Jackson message converter
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
// create request body
//Here your TopStatus object converted to json. you can use Jackson to do this.
String input = "{\"topStatus\":\"Fail\",\"topId\":\"93\"}";
// set headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(input, headers);
URI uri = new URI("urihere");
ResponseEntity<TopStatus> response = restTemplate
.exchange(uri, HttpMethod.POST, entity, TopStatus.class);
return response;
}
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.