Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当字符串值为整数时,没有字符串参数构造函数/工厂方法从字符串值反序列化_Java_Jackson_Xmlmapper - Fatal编程技术网

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

Java 当字符串值为整数时,没有字符串参数构造函数/工厂方法从字符串值反序列化,java,jackson,xmlmapper,Java,Jackson,Xmlmapper,我正在尝试将以下xml反序列化到对象中: <?xml version="1.0" encoding="iso-8859-1" ?> <foo> <dean> <bar>28</bar> <bar>31</bar> </dean> </foo> } 下面是失败的测试,xmlMapper抛出异常 @Test public void shou

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

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

下面是失败的测试,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')
@测试
public void shouldParseAndCreateObject()引发异常{
最终JacksonXmlModule JacksonXmlModule=新JacksonXmlModule();
XmlMapper XmlMapper=(XmlMapper)新的XmlMapper(jacksonXmlModule)
.禁用(未知属性失败,忽略属性失败);
Foo Foo=xmlMapper.readValue(“\n”+
“\n”+
“\n”+
“28\n”+
“31\n”+
“\n”+
“”,Foo.class);
断言(foo.getDean().getBar().get(0.getValue())为(28));
}
com.fasterxml.jackson.databind.JsonMappingException:无法构造Bar的实例:没有要从字符串值('28'反序列化的字符串参数构造函数/工厂方法
在[Source:out/test/resources/test.xml;第4行,第16列](通过引用链:service.Foo[“dean”]->service.dean[“bar”]->java.util.ArrayList[0])

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

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));
}
公共类栏{
私有字符串测试;
@JacksonXmlText
私有整数值;
公共条(@JacksonXmlProperty(localName=“test”,isAttribute=true)字符串
测试,@JacksonXmlProperty(localName=”“)整数值){
这个。测试=测试;
这个值=值;
}
公共字符串getTest(){
回归试验;
}
公共整数getValue(){
返回值;
}
28
31
@试验
public void shouldParseAndCreateObject()引发异常{
最终JacksonXmlModule JacksonXmlModule=新JacksonXmlModule();
XmlMapper XmlMapper=(XmlMapper)新的XmlMapper(jacksonXmlModule)
.禁用(未知属性失败,忽略属性失败);
Foo Foo=xmlMapper.readValue(“\n”+
“\n”+
“\n”+
“28\n”+
“31\n”+
“\n”+
"" +
“”,Foo.class);
断言(foo.getDean().getBar().get(0.getValue())为(28));
}

我想说的是,映射程序应该从构造函数参数类型推断类型,并尝试使用字符串值实例化该对象,然后在引擎盖下执行类似Integer.valueOf(“28”)

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

gradle
ext {
jacksonVersion = "2.8.9"
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
XmlMapper xmlMapper = new XmlMapper(module);

如果你从
Bar
构造函数中删除参数,它就可以工作了。但是这意味着你需要使类不可变,这不是你想要的。对不起!
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));
}
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
XmlMapper xmlMapper = new XmlMapper(module);