Spring Boot:没有从String值反序列化的String-argument构造函数/工厂方法

时间:2017-07-18 11:48:39

标签: java json spring-boot jackson resttemplate

我尝试使用RestTemplate(Spring Boot)从休息服务中读取响应:

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:无字符串参数   从String值反序列化的构造函数/工厂方法   (&#39; MND_00026&#39;)在[来源:java.io.PushbackInputStream@77f5bb5f;线:   1,专栏:532](通过参考链:   co.portals.entities.RMSendPropertyResponse [&#34;错误&#34;] - &GT; java.util.ArrayList中[0]);   嵌套异常是   com.fasterxml.jackson.databind.JsonMappingException:无法构造   co.portals.entities.RMError的实例:没有String-argument   从String值反序列化的构造函数/工厂方法   (&#39; MND_00026&#39;)在[来源:java.io.PushbackInputStream@77f5bb5f;线:   1,专栏:532](通过参考链:   co.portals.entities.RMSendPropertyResponse [&#34;错误&#34;] - &GT; 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;
    }
}

我无法理解为什么没有构造函数/工厂方法从String值反序列化。

由于

1 个答案:

答案 0 :(得分:1)

您的代码适合我。 您使用的spring-boot版本是什么?你管理自己jackson的版本?如果是这样,你正在使用什么版本。 您确定从您拨打的外部网址获得的回复是您在问题中粘贴的内容吗? 这是我复制的工作代码:

@RestController
public class MyController {  

 @GetMapping(value = "/read", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity read() {
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<RMSendPropertyResponse> response = restTemplate.exchange("http://localhost:8080/", HttpMethod.GET, null, RMSendPropertyResponse.class);
    RMSendPropertyResponse rmResponse = response.getBody();
    return new ResponseEntity<>(rmResponse, HttpStatus.CREATED);

}

@GetMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
public String get() {
    return "{\n" +
            "    \"message\": \"Something failed.\",\n" +
            "    \"success\": false,\n" +
            "    \"errors\": [\n" +
            "        {\n" +
            "            \"error_code\": \"MND_00026\",\n" +
            "            \"error_value\": \"\",\n" +
            "            \"error_description\": \"field not present\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"error_code\": \"VAL_00039\",\n" +
            "            \"error_value\": \"0\",\n" +
            "            \"error_description\": \"Wrong field\"\n" +
            "        }\n" +
            "    ],\n" +
            "    \"warnings\": null,\n" +
            "    \"request_timestamp\": \"18-07-2017 11:34:46\",\n" +
            "    \"response_timestamp\": \"18-07-2017 11:34:46\"\n" +
            "}";
  }

}

@JsonIgnoreProperties(ignoreUnknown = true)
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;
    }
}

@JsonIgnoreProperties(ignoreUnknown = true)
class RMSendPropertyResponse {

    private List<RMError> errors;

    public RMSendPropertyResponse() {
    }

    public List<RMError> getErrors() {
        return errors;
    }

    public void setErrors(List<RMError> errors) {
        this.errors = errors;
    }
}