我正在尝试使用RestTemplate(Spring Boot)读取来自rest服务的响应:
但是当响应中存在错误数组时:
我总是遇到这个错误:
2017-07-18 12:29:08.220 WARN 9489 --- [nio-8080-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : 无法读取HTTP消息: org.springframework.http.converter.HttpMessageNotReadableException: 无法读取文档:不能构造co.easymatch.portals.rightmove.entities.RMError的实例:没有字符串参数构造函数/工厂方法来从字符串值('MND_00026')反序列化. [来源:java.io.PushbackInputStream@77f5bb5f; 行:1,列:532](通过引用链 co.portals.entities.RMSendPropertyResponse["errors"]->java.util.ArrayList[0]); 嵌套异常是com.fasterxml.jackson.databind.JsonMappingException: 无法构造co.portals.entities.RMError的实例:没有字符串参数构造函数/工厂方法可用于从字符串值('MND_00026')反序列化. [来源:java.io.PushbackInputStream@77f5bb5f; 行:1,列:532](通过引用链 co.portals.entities.RMSendPropertyResponse["errors"]->java.util.ArrayList[0])
我的类是…
RMSendPropertyResponse 类:
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<RMSendPropertyResponse> response = restTemplate.exchange("https://url", HttpMethod.POST, entity, RMSendPropertyResponse.class);
RMSendPropertyResponse rmResponse = response.getBody();
但是当响应中存在错误数组时:
{
"message": "Something failed.",
"success": false,
"errors": [
{
"error_code": "MND_00026",
"error_value": "",
"error_description": "field not present"
},
{
"error_code": "VAL_00039",
"error_value": "0",
"error_description": "Wrong field"
}
],
"warnings": null,
"request_timestamp": "18-07-2017 11:34:46",
"response_timestamp": "18-07-2017 11:34:46"
}
我总是遇到这个错误:
2017-07-18 12:29:08.220 WARN 9489 --- [nio-8080-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : 无法读取HTTP消息: org.springframework.http.converter.HttpMessageNotReadableException: 无法读取文档:不能构造co.easymatch.portals.rightmove.entities.RMError的实例:没有字符串参数构造函数/工厂方法来从字符串值('MND_00026')反序列化. [来源:java.io.PushbackInputStream@77f5bb5f; 行:1,列:532](通过引用链 co.portals.entities.RMSendPropertyResponse["errors"]->java.util.ArrayList[0]); 嵌套异常是com.fasterxml.jackson.databind.JsonMappingException: 无法构造co.portals.entities.RMError的实例:没有字符串参数构造函数/工厂方法可用于从字符串值('MND_00026')反序列化. [来源:java.io.PushbackInputStream@77f5bb5f; 行:1,列:532](通过引用链 co.portals.entities.RMSendPropertyResponse["errors"]->java.util.ArrayList[0])
我的类是…
RMSendPropertyResponse 类:
@JsonIgnoreProperties(ignoreUnknown = true)
public class RMSendPropertyResponse extends RMResponse {
private RMPropertyResponse property;
private List<RMWarning> warnings;
private List<RMError> errors;
public RMSendPropertyResponse() {
}
public RMPropertyResponse getProperty() {
return property;
}
public void setProperty(RMPropertyResponse property) {
this.property = property;
}
public List<RMWarning> getWarnings() {
return warnings;
}
public void setWarnings(List<RMWarning> warnings) {
this.warnings = warnings;
}
public List<RMError> getErrors() {
return errors;
}
public void setErrors(List<RMError> errors) {
this.errors = errors;
}
}
RMError类:
@JsonIgnoreProperties(ignoreUnknown = true)
public class RMError {
private String error_code;
private String error_description;
private String error_value;
public RMError() {
}
public String getError_code() {
return error_code;
}
public void setError_code(String error_code) {
this.error_code = error_code;
}
public String getError_description() {
return error_description;
}
public void setError_description(String error_description) {
this.error_description = error_description;
}
public String getError_value() {
return error_value;
}
public void setError_value(String error_value) {
this.error_value = error_value;
}
}
我不明白为什么没有一个构造函数/工厂方法可以从字符串值反序列化。
谢谢。
ClientHttpRequestInterceptor
的实现来记录所有请求和响应。很高兴你解决了你的问题。 - Akli REGUIG