使用 Jackson 从 CloseableHttpResponse 读取的 json 对象获取空值,但也获取 200
Getting null values on json object read from CloseableHttpResponse using Jackson, but also getting 200
在 Postman 中发出 returns 一个 json 响应主体的请求,但是当我 运行 我的代码时只是 returns 一个 200。我很确定我用来与 Jackson 反序列化的 classes 结构正确,并且请求的 json 主体似乎是有效的。我错过了什么?
这是提出请求的class:
import ...
@Service
public class CPService {
@Value("${cp.url}")
private String cpUrl;
@Autowired
private ObjectMapper objectMapper;
public ResponseData send(String caseId, String pId, String documentId) throws IOException {
String version = "1.0";
Request request = Request.builder()
.requestData(RequestData.builder()
.version(version)
.transactionId(caseId)
.credPublish(CredPublish.builder()
.refId(pId)
.reportDocumentId(documentId)
.build())
.build())
.build();
HttpPost httpPost = new HttpPost(cpUrl);
EntityBuilder builder = createBuilder();
builder.setContentType(ContentType.APPLICATION_JSON);
builder.setText(objectMapper.writeValueAsString(request));
httpPost.setEntity(builder.build());
try (CloseableHttpClient httpClient = createHttpClient()) {
CloseableHttpResponse response = httpClient.execute(httpPost);
ResponseData responseData = objectMapper.readValue(response.getEntity().getContent(), ResponseData.class);
return responseData;
}
}
EntityBuilder createBuilder() {
return EntityBuilder.create();
}
CloseableHttpClient createHttpClient() {
return HttpClients.createDefault();
}
}
以下是使用的 classes:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Data
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseData {
private Integer resultCount;
private String version;
private CredPublishResponse credPublish;
private String transactionId;
private List<ErrorMessage> errors;
}
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
@Data
@Getter
@Setter
public class CredPublishResponse {
private String responseMessage;
}
以下是 Postman 的回复示例:
{
"responseData": {
"resultCount": 1,
"version": "",
"credPublish": {
"responseMessage": "success"
},
"transactionId": "CASE-1234567"
}
}
发生这种情况是因为您发布的 json 响应不是 ResponseData
对象表示,但它在 “responseData”[=] 中包含 ResponseData
对象表示22=] 属性;通常,转换尝试会以错误表示并以适当的异常结束,但 class 上的 @JsonIgnoreProperties(ignoreUnknown = true)
注释会阻止错误报告,然后获得的对象仅包含空字段。
为避免这种行为,您可以在 "responseData" 属性 位置读取您的 json 作为 JsonNode
对象,并且将其转换为如下所示的 ResponseData
对象:
JsonNode root = mapper.readTree(response.getEntity().getContent());
ResponseData responseData = mapper.treeToValue(root.at("/responseData"), ResponseData.class);
在 Postman 中发出 returns 一个 json 响应主体的请求,但是当我 运行 我的代码时只是 returns 一个 200。我很确定我用来与 Jackson 反序列化的 classes 结构正确,并且请求的 json 主体似乎是有效的。我错过了什么?
这是提出请求的class:
import ...
@Service
public class CPService {
@Value("${cp.url}")
private String cpUrl;
@Autowired
private ObjectMapper objectMapper;
public ResponseData send(String caseId, String pId, String documentId) throws IOException {
String version = "1.0";
Request request = Request.builder()
.requestData(RequestData.builder()
.version(version)
.transactionId(caseId)
.credPublish(CredPublish.builder()
.refId(pId)
.reportDocumentId(documentId)
.build())
.build())
.build();
HttpPost httpPost = new HttpPost(cpUrl);
EntityBuilder builder = createBuilder();
builder.setContentType(ContentType.APPLICATION_JSON);
builder.setText(objectMapper.writeValueAsString(request));
httpPost.setEntity(builder.build());
try (CloseableHttpClient httpClient = createHttpClient()) {
CloseableHttpResponse response = httpClient.execute(httpPost);
ResponseData responseData = objectMapper.readValue(response.getEntity().getContent(), ResponseData.class);
return responseData;
}
}
EntityBuilder createBuilder() {
return EntityBuilder.create();
}
CloseableHttpClient createHttpClient() {
return HttpClients.createDefault();
}
}
以下是使用的 classes:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Data
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseData {
private Integer resultCount;
private String version;
private CredPublishResponse credPublish;
private String transactionId;
private List<ErrorMessage> errors;
}
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
@Data
@Getter
@Setter
public class CredPublishResponse {
private String responseMessage;
}
以下是 Postman 的回复示例:
{
"responseData": {
"resultCount": 1,
"version": "",
"credPublish": {
"responseMessage": "success"
},
"transactionId": "CASE-1234567"
}
}
发生这种情况是因为您发布的 json 响应不是 ResponseData
对象表示,但它在 “responseData”[=] 中包含 ResponseData
对象表示22=] 属性;通常,转换尝试会以错误表示并以适当的异常结束,但 class 上的 @JsonIgnoreProperties(ignoreUnknown = true)
注释会阻止错误报告,然后获得的对象仅包含空字段。
为避免这种行为,您可以在 "responseData" 属性 位置读取您的 json 作为 JsonNode
对象,并且将其转换为如下所示的 ResponseData
对象:
JsonNode root = mapper.readTree(response.getEntity().getContent());
ResponseData responseData = mapper.treeToValue(root.at("/responseData"), ResponseData.class);