我試圖讀取與RestTemplate(春季啓動)休息服務的響應:春天開機:沒有字符串參數的構造函數/工廠方法從字符串值反序列化
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 : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of co.easymatch.portals.rightmove.entities.RMError: no String-argument constructor/factory method to deserialize from String value ('MND_00026') at [Source: [email protected]; line: 1, column: 532] (through reference chain: co.portals.entities.RMSendPropertyResponse["errors"]->java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of co.portals.entities.RMError: no String-argument constructor/factory method to deserialize from String value ('MND_00026') at [Source: [email protected]; line: 1, column: 532] (through reference chain: 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;
}
}
我不明白爲什麼沒有一個構造函數/工廠方法從字符串值反序列化。
謝謝
謝謝!我正在使用Spring Boot 1.4.2。我不管理傑克遜版本。 Spring Boot獲得了傑克遜2.8.4。如果我用郵遞員致電相同的服務,這就是我得到的原始迴應。我會盡力直接通過這個原始答案,就像你一樣,看看我是否仍然得到相同的錯誤。 – dbeja
好的,所以我嘗試手動創建響應,就像你做的一樣,它的工作。難道是因爲某種原因,我對郵差的反應與我在項目中得到的反應不同嗎?你知道調試來自RestTemplate的原始響應的最佳方式嗎? – dbeja
好吧,發現問題...我添加了一個攔截器,當出現錯誤時,服務默認在xml中回覆,而不是在json中回覆。我強迫在頭只接受json,現在工作正常! – dbeja