【问题标题】:How to parse response to POJO with this response body如何使用此响应正文解析对 POJO 的响应
【发布时间】:2018-08-07 21:03:29
【问题描述】:

春天和一切的新手。试图命中端点并检索响应字段中的信息。但是,我不知道如何访问更具体的信息,而不仅仅是带回整个响应,因为它没有嵌套在键/值中?

我正在使用休息模板 + MappingJacksonHttpMessageConverter。 我想将信息映射到对 pojo 的响应中,只是不确定如何走得更远,或者我是否在这里的方向正确。下面的示例代码。

邮递员回复:

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

试图映射到 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 示例

@JsonIgnoreProperties(ignoreUnknown = true)
public class TopStatus{

    @JsonProperty("TopStatus")
    private String topStatus;

    rest of fields, setters, getters etc...

}

【问题讨论】:

  • @JsonProperty("TopStatus") 不起作用。消息的特定部分不是 JSON。您可以期望的最好的是将response 提取为字符串,然后自己进一步解析它
  • 我明白了。我认为这是问题所在,但我不确定我是否在某个地方遗漏了有关如何提取它的文档,而不是拉出整个字符串并自己做..

标签: java json postman resttemplate pojo


【解决方案1】:

您可以使用 postForEntity() 方法返回 ResponseEntity 类型的对象,而不是 postForObject()

您需要做一些小改动,将响应正文映射到 POJO

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

这应该只返回预期的 POJO。

【讨论】:

  • 似乎仍然会返回一个 pojo,所有 null 字段仍然带有它
  • 你能发表回复吗?
【解决方案2】:

尝试使用下面的代码。

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;
}

修改后的代码。

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;
}

【讨论】:

  • 为什么 OP 应该尝试这个代码?没有解释这将如何帮助或目的是什么
  • 我确实试过了,但它仍然是一个 null pojo。 pojo 有一个名为 status 的字段,它填充了“200”,所以这告诉我它不会在正文中响应等。我的问题是我认为不可能提取响应信息以映射到 pojo无论如何,响应的结构如何。至少就我目前的知识而言。
  • 我通常使用 RestClient 和 NStringEntity。如果您想更改 RestClient,我将向您发送我正在使用的确切代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-15
  • 2021-12-19
  • 1970-01-01
  • 2018-10-25
  • 1970-01-01
相关资源
最近更新 更多