Http响应无法获取响应正文

时间:2020-05-17 15:46:42

标签: java spring rest

当我使用REST发送应用程序/ json请求时,它返回:

ViewContainerRef

问题是我不能只返回尸体,但我得到了整个反应,我如何只返回尸体。当我调用.getBody时,它仍然不会返回正文,而是返回整个响应,以及如何将响应正文映射到我的CustomerResponse类。

我用于发送请求和接收响应的代码:

Trace-ID=[] - Response {
  "url" : null,
  "httpMethod" : null,
  "headers" : {
    "Cache-Control" : [ "no-cache" ],
    "Pragma" : [ "no-cache" ],
    "Content-Type" : [ "application/json; charset=utf-8" ],
  },
  "body" : {
    "AccNum" : "123456789",
    "AccName" : "xxxxxx",
    "ResponseCode" : "000",
  },
  "status" : 200
}
String: "{
  "url" : null,
  "httpMethod" : null,
  "headers" : {
    "Cache-Control" : [ "no-cache" ],
    "Pragma" : [ "no-cache" ],
    "Content-Type" : [ "application/json; charset=utf-8" ],
  },
  "body" : {
    "AccNum" : "123456789",
    "AccName" : "xxxxxx",
    "ResponseCode" : "000",
  },
  "status" : 200
}"

1 个答案:

答案 0 :(得分:0)

问题是:

response.getBody()实际上返回您收到的完整JSON响应,而不是您要访问并返回的'body'JSON对象。

现在,从您的代码看来,响应的类型为“ CustomerResponse”。 因此,我假设“ CustomerResponse”具有实例或静态变量“ body”,可以通过getter方法“ getBody()”访问该实例或静态变量。

所以最后您可以尝试使用response.getBody()。getBody()。

如果这不能解决您的问题,请共享“ CustomerResponse”类的代码。