简体   繁体   English

如何使用此响应主体解析对POJO的响应

[英]How to parse response to POJO with this response body

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 . 我正在使用rest模板+ 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. 我想将信息映射到响应pojo中,只是不确定如何进行进一步操作,或者不确定我是否在正确的方向上。 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 试图映射到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 我如何设置的Pojo示例

@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() 您可以使用postForEntity()方法返回ResponseEntity类型的对象,而不是postForObject()

you need to do small change that will map the response body to the POJO 您需要进行一些小的更改,以将响应主体映射到POJO

ResponseEntity<TopStatus> response = rt.postForEntity(uri, entity, TopStatus.class);
TopStatus topStatusResponse = response.getBody();
return topStatusResponse;

This should return only the expected POJO. 这应该仅返回预期的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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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