0

I'm getting an error

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])]

when sending:

<?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>

to the route /test/api/apply

I wrote a sample code on GitHub with the particular issue.

I am using Spring boot 2.2.4-RELEASE and Spring Cloud Hoxton.SR1, you get the pom over there. https://github.com/cmeon/spring-issue-test/tree/master

1 Answer 1

1

Okay I checked your code in github and found the issue, so when you try to deserialise a Collections you have to mention that attribute with @JacksonXmlElementWrapper.

Do add it like below

TestChildWrapper.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<>();
}

It works..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.