猿问

如何使用相同的元素和其他元素映射 XML Wrapper

我有一个 XML 模式,带有一个元素name:


<xsd:element name="name">

  <xsd:complexType>

    <xsd:sequence>

      <xsd:element name="given" maxOccurs="unbounded" type="xsd:string"/>

      <xsd:element name="family" type="xsd:string"/>

    </xsd:sequence>

  </xsd:complexType>

</xsd:element>

我需要将它映射到工作 java 类。


我有一个带有 JAXB XML 控制器的 Spring Boot 应用程序,它需要:


<name>

  <given>First</given>

  <given>Second</given>

  <family>Lastname</family>

</name>

如果我使用自动模式源生成(使用 jaxb2-maven-plugin),我会得到一个类:


            @XmlAccessorType(XmlAccessType.FIELD)

            @XmlType(name = "", propOrder = {

                "given",

                "family"

            })

            public static class Name {


                @XmlElement(required = true)

                protected List<String> given = new ArrayList<>();

                @XmlElement(required = true)

                protected String family;


                public List<String> getGiven() {

                    if (given == null) {

                        given = new ArrayList<>();

                    }

                    return this.given;

                }

                public void setGiven(List<String> given) {

                    this.given = given;

                }


                public String getFamily() {

                    return family;

                }


                public void setFamily(String value) {

                    this.family = value;

                }


            }

但问题是,当我运行 spring boot 应用程序并使用上述 XML 调用它时,出现错误:


*....Name["given"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token*

我无法控制发送数据的格式,因此无法更改接收到的 XML。我尝试了许多不同的解决方案,但我完全坚持这一点。你能帮我吗?


不负相思意
浏览 193回答 1
1回答

慕姐8265434

试试 @JacksonXmlElementWrapper(useWrapping = false)例子@Testpublic void test2() throws JsonParseException, JsonMappingException, IOException {&nbsp; &nbsp; ObjectMapper mapper = new XmlMapper();&nbsp; &nbsp; Name name = mapper.readValue("<name>\n" + "&nbsp; <given>First</given>\n" + "&nbsp; <given>Second</given>\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; <family>Lastname</family>\n" + "</name>", Name.class);&nbsp; &nbsp; System.out.println(toString(name));}public static class Name {&nbsp; &nbsp; @JacksonXmlElementWrapper(useWrapping = false)&nbsp; &nbsp; public List<String> given = new ArrayList<>();&nbsp; &nbsp; @XmlElement(required = true)&nbsp; &nbsp; public String family;}public String toString(Object obj) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; StringWriter w = new StringWriter();&nbsp; &nbsp; &nbsp; &nbsp; new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true).writeValue(w, obj);&nbsp; &nbsp; &nbsp; &nbsp; return w.toString();&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException(e);&nbsp; &nbsp; }}印刷{&nbsp; "given" : [ "First", "Second" ],&nbsp; "family" : "Lastname"}
随时随地看视频慕课网APP

相关分类

Java
我要回答