java 没有字符串参数构造函数/工厂方法,当它是整数时从字符串值反序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45425849/
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:41:29  来源:igfitidea点击:

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

javaHymansonxmlmapper

提问by Dean Smith

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

我正在尝试将以下 xml 反序列化为对象:

<?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(@HymansonXmlProperty(localName = "dean") final Dean dean) {
    this.dean = dean;
}

public Dean getDean() {
    return dean;
}
}

public class Dean {

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

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

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

public class Bar {

@HymansonXmlText
private Integer value;

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


public Integer getValue() {
    return value;
}
}

My mapper dependencies are the following:

我的映射器依赖项如下:

gradle
ext {
HymansonVersion = "2.8.9"

}

}

compile(group: "com.fasterxml.Hymanson.dataformat", name: "Hymanson-dataformat-
xml", version: "$HymansonVersion")
compile(group: "com.fasterxml.Hymanson.datatype", name: "Hymanson-datatype-
jsr310", version: "$HymansonVersion")enter code here

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

这是 xmlMapper 抛出异常的失败测试

@Test
public void shouldParseAndCreateObject() throws Exception {
    final HymansonXmlModule HymansonXmlModule = new HymansonXmlModule();
     XmlMapper xmlMapper = (XmlMapper) new XmlMapper(HymansonXmlModule)
            .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.Hymanson.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])

在 [来源:out/test/resources/test.xml; 行:4,列:16](通过引用链: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.

从读取异常看起来映射器将值 28 视为字符串而不是整数,但是如果我将 Bar 类更改为以下并将元素 bar 的属性添加到原始 xml 中,则相同的测试通过。

public class Bar {

private String test;

@HymansonXmlText
private Integer value;

public Bar(@HymansonXmlProperty(localName = "test", isAttribute = true) String 
test, @HymansonXmlProperty(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 HymansonXmlModule HymansonXmlModule = new HymansonXmlModule();
     XmlMapper xmlMapper = (XmlMapper) new XmlMapper(HymansonXmlModule)
            .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")

我会说映射器应该从构造函数参数类型推断类型,并尝试使用字符串值实例化该对象,并在引擎盖下执行类似 Integer.valueOf("28") 的操作

回答by Taras Melnyk

Add to POJO empty constructor and also for all fields. Also try the following:

添加到 POJO 空构造函数以及所有字段。还可以尝试以下操作:

HymansonXmlModule module = new HymansonXmlModule();
module.setDefaultUseWrapper(false);
XmlMapper xmlMapper = new XmlMapper(module);