question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No String-argument constructor/factory method to deserialize from String value when it is a Integer

See original GitHub issue

I am trying to deserialise the following xml into the objects:

<?xml version="1.0" encoding="iso-8859-1" ?>
<foo>
    <dean>
        <bar>28</bar>
        <bar>31</bar>
    </dean>
</foo>

My classes are the following

public class Foo {

    private final Dean dean;

    public Foo(@JacksonXmlProperty(localName = "dean") final Dean dean) {
        this.dean = dean;
    }

    public Dean getDean() {
        return dean;
    }
}`
public class Dean {

    @JacksonXmlElementWrapper(useWrapping = false)
    private final List<Bar> bar;

    public Dean(@JacksonXmlProperty(localName = "bar") final List<Bar> bar) {
        this.bar = bar;
    }

    public List<Bar> getBar() {
        return bar;
    }
}
public class Bar {

  @JacksonXmlText
    private Integer value;

    public Bar(@JacksonXmlProperty(localName = " ") Integer value) {
        this.value = value;
    }


    public Integer getValue() {
        return value;
    }
}

My mapper dependencies are the following:

ext {
    jacksonVersion = "2.8.9"
}
compile(group: "com.fasterxml.jackson.dataformat", name: "jackson-dataformat-xml", version: "$jacksonVersion")
compile(group: "com.fasterxml.jackson.datatype", name: "jackson-datatype-jsr310", version: "$jacksonVersion")

Here is the failing test with exception being thrown by the xmlMapper

@Test
    public void shouldParseAndCreateObject() throws Exception {
        final JacksonXmlModule jacksonXmlModule = new JacksonXmlModule();
         XmlMapper xmlMapper = (XmlMapper) new XmlMapper(jacksonXmlModule)
                .disable(FAIL_ON_UNKNOWN_PROPERTIES, FAIL_ON_IGNORED_PROPERTIES);
        Foo foo = xmlMapper.readValue("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n" +
                "<foo>\n" +
                "    <dean>\n" +
                "        <bar>28</bar>\n" +
                "        <bar>31</bar>\n" +
                "    </dean>\n" +
                "</foo>", Foo.class);
        assertThat(foo.getDean().getBar().get(0).getValue(), is(28));
    }
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of Bar: no String-argument constructor/factory method to deserialize from String value ('28')
 at [Source: out/test/resources/test.xml; line: 4, column: 16] (through reference chain: service.Foo["dean"]->service.Dean["bar"]->java.util.ArrayList[0])

From reading the exception it looks like the mapper is treating the value 28 as a string rather than a Integer but if i change the Bar class to the following and add an attribute to element bar to the raw xml the same test passes.

public class Bar {

    private String test;

    @JacksonXmlText
    private Integer value;

    public Bar(@JacksonXmlProperty(localName = "test", isAttribute = true) String test, @JacksonXmlProperty(localName = " ") Integer value) {
        this.test = test;
        this.value = value;
    }

    public String getTest() {
        return test;
    }

    public Integer getValue() {
        return value;
    }
<?xml version="1.0" encoding="iso-8859-1" ?>
<foo>
    <dean>
        <bar test="haha1">28</bar>
        <bar test="haha2">31</bar>
    </dean>
</foo>
@Test
    public void shouldParseAndCreateObject() throws Exception {
        final JacksonXmlModule jacksonXmlModule = new JacksonXmlModule();
         XmlMapper xmlMapper = (XmlMapper) new XmlMapper(jacksonXmlModule)
                .disable(FAIL_ON_UNKNOWN_PROPERTIES, FAIL_ON_IGNORED_PROPERTIES);
        Foo foo = xmlMapper.readValue("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n" +
                "<foo>\n" +
                "    <dean>\n" +
                "        <bar test=\"haha1\">28</bar>\n" +
                "        <bar test=\"haha2\">31</bar>\n" +
                "    </dean>\n" +
                "</foo>" +
                "</foo>", Foo.class);
        assertThat(foo.getDean().getBar().get(0).getValue(), is(28));
    }

I would say that the Mapper should infer the type from the constructor parameter type and try to instantiate that object with the string value and under the hood do something like Integer.valueOf(“28”)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
RicardoRBcommented, Aug 11, 2017

@cowtowncoder I tried with the recommendations what still not working, any other way to make it works?

1reaction
cowtowncodercommented, May 13, 2020

Note to self: problem is the fact that XML has no notion of values other than Strings (and nesting via elements, attributes), so Jackson just sees a String token and does not like that – while there is coercion from Strings to numbers this only works for simple values but not delegation model.

But it would be reasonable to support such usage for non-typed formats like XML (and CSV as well), if I can figure out a clean way to achieve that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No String-argument constructor/factory method to deserialize ...
Instead of sending values for my Address bean, I'm getting just an empty string. Unfortunately, I'm receiving my data from a third party...
Read more >
No string-argument constructor/factory method to deserialize from ...
In this article, we will solve the problem `No string-argument constructor/factory method to deserialize from the string value` and we will also discuss...
Read more >
com.fasterxml.jackson.databind.DeserializationContext
Method to called to create value instance from JSON Object using * an ... "no String-argument constructor/factory method to deserialize from String value...
Read more >
Java – No String-argument constructor/factory method to ...
Java – No String-argument constructor/factory method to deserialize from String value (”). javajsonspring. I'm running into a json parsing issue when using ...
Read more >
Using Optional with Jackson - Baeldung
Optional : no String-argument constructor/factory method to deserialize from String value ('The Parish Boy's Progress')
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found