JSON 解析错误:无法构造"org.test.dtos.TestChild"的实例(尽管至少存在一



我收到错误

2020-01-28 18:19:21,311 DEBUG [http-nio-8069-exec-8] ServletInvocableHandlerMethod : Could not resolve parameter [0] in public org.springframework.http.ResponseEntity<org.test.dtos.TestResponse> org.test.controllers.TestController.testRoute(org.test.dtos.TestRequest): JSON parse error: Cannot construct instance of `org.test.dtos.TestChild` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('0'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `org.test.dtos.TestChild` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('0')
at [Source: (PushbackInputStream); line: 5, column: 18] (through reference chain: org.test.dtos.TestRequest["children"]->org.test.dtos.TestChildWrapper["child"]->java.util.HashSet[0])
2020-01-28 18:19:21,314 WARN  [http-nio-8069-exec-8] DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `org.test.dtos.TestChild` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('0'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `org.test.dtos.TestChild` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('0')
at [Source: (PushbackInputStream); line: 5, column: 18] (through reference chain: org.test.dtos.TestRequest["children"]->org.test.dtos.TestChildWrapper["child"]->java.util.HashSet[0])]

发送时:

<?xml version="1.0" encoding="UTF-8"?>
<TestRequest>
<children>
<child>
<childStuffA>0</childStuffA>
<childStuffB>0</childStuffB>
<childStuffC>string</childStuffC>
</child>
</children>
<doubleProperty>0</doubleProperty>
<id>0</id>
<longProperty>0</longProperty>
<stringProperty>string</stringProperty>
</TestRequest>

到路线/test/api/apply

我在 GitHub 上写了一个针对特定问题的示例代码。

我正在使用Spring boot 2.2.4-RELEASE和Spring Cloud Hoxton.SR1,你在那里得到pom。 https://github.com/cmeon/spring-issue-test/tree/master

好的,我在 github 中检查了您的代码并发现了问题,因此当您尝试反序列化Collections时,您必须用@JacksonXmlElementWrapper提及该属性。

请像下面这样添加它

测试儿童包装器.class

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@Data
@NoArgsConstructor
public class TestChildWrapper implements Serializable {
private static final long serialVersionUID = 1L;
@JacksonXmlProperty(localName = "child")
@JacksonXmlElementWrapper(useWrapping = false)
private Set<TestChild> child = new HashSet<>();
}

它有效..

最新更新