java Spring Boot:没有从字符串值反序列化的字符串参数构造函数/工厂方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45165943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:33:13  来源:igfitidea点击:

Spring Boot: no String-argument constructor/factory method to deserialize from String value

javajsonspring-bootHymansonresttemplate

提问by dbeja

I'm trying to read a response from a rest service with RestTemplate (Spring Boot):

我正在尝试使用 RestTemplate (Spring Boot) 读取来自休息服务的响应:

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<RMSendPropertyResponse> response = restTemplate.exchange("https://url", HttpMethod.POST, entity, RMSendPropertyResponse.class);
RMSendPropertyResponse rmResponse = response.getBody();

But when errors array is present on the response:

但是当响应中存在错误数组时:

{
    "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"
}

I always get this error:

我总是收到这个错误:

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: java.io.PushbackInputStream@77f5bb5f; line: 1, column: 532] (through reference chain: co.portals.entities.RMSendPropertyResponse["errors"]->java.util.ArrayList[0]); nested exception is com.fasterxml.Hymanson.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: java.io.PushbackInputStream@77f5bb5f; line: 1, column: 532] (through reference chain: co.portals.entities.RMSendPropertyResponse["errors"]->java.util.ArrayList[0])

2017-07-18 12:29:08.220 WARN 9489 --- [nio-8080-exec-9] .wsmsDefaultHandlerExceptionResolver:无法读取 HTTP 消息:org.springframework.http.converter.HttpMessageNotReadableException:无法读取文档:无法构造 co.easymatch.portals.rightmove.entities.RMError 的实例:在 [来源:java.io.PushbackInputStream@77f5bb5f; 没有从字符串值 ('MND_00026') 反序列化的字符串参数构造函数/工厂方法;行:1,列:532](通过参考链:co.portals.entities.RMSendPropertyResponse["errors"]->java.util.ArrayList[0]);嵌套异常是 com.fasterxml.Hymanson.databind.JsonMappingException:无法构造 co.portals.entities.RMError 的实例:没有从字符串值反序列化的字符串参数构造函数/工厂方法('MND_00026' ) 在 [来源:java.io.PushbackInputStream@77f5bb5f; 行:1,列:532](通过参考链:co.portals.entities.RMSendPropertyResponse["errors"]->java.util.ArrayList[0])

And my classes are...

而我的课...

RMSendPropertyResponse class:

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 class:

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;
    }
}

I can't understand why there isn't a constructor/factory method to deserialize from String value.

我不明白为什么没有从 String 值反序列化的构造函数/工厂方法。

Thanks

谢谢

采纳答案by Akli REGUIG

Your code works for me. What is the version of spring-bootyour are using? do you manage yourself Hymanson's version ? if so what version is it your are using. Are you sure that the response you get from the external url you are calling is what you pasted in the question ? Here is the working code i copied :

你的代码对我有用。你用的是什么版本spring-boot?你管理自己Hymanson的版本吗?如果是这样,您使用的是什么版本。您确定从您正在调用的外部 url 获得的响应是​​您粘贴在问题中的内容吗?这是我复制的工作代码:

@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;
    }
}