Spring JAXB@XmlValue无法获取文本,无法生成空XML元素,也无法读取属性
我有一个SpringREST控制器,它以Xml形式接受请求。这是即将推出的示例请求格式Spring JAXB@XmlValue无法获取文本,无法生成空XML元素,也无法读取属性,spring,jaxb,Spring,Jaxb,我有一个SpringREST控制器,它以Xml形式接受请求。这是即将推出的示例请求格式 <Message> <Header id="101" desc="Header content description"> <title text="The quick brown fox" /> </Header> <Content /> <Footer name="test">Footer cont
<Message>
<Header id="101" desc="Header content description">
<title text="The quick brown fox" />
</Header>
<Content />
<Footer name="test">Footer content sample.</Footer>
</Message>
对于Message类,我有以下几个类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Message")
public class Message {
@XmlElement(name = "Header", required = true)
private Header header;
@XmlElement(name = "Content", required = true)
private Content content;
@XmlElement(name = "Footer", required = true)
private Footer footer;
// Getters and setters here...
@Override
public String toString() {
// In here, I outputted the values of the header and footer.
}
}
标题类:
@XmlRootElement(name = "Header")
@XmlAccessorType(XmlAccessType.FIELD)
public class Header {
@XmlAttribute(name = "id", required = true)
private String id;
@XmlAttribute(name = "desc", required = true)
private String description;
// Getters and setters here...
}
内容类别:
@XmlRootElement(name = "Content")
@XmlAccessorType(XmlAccessType.FIELD)
public class Content {
}
对于页脚类:
@XmlRootElement(name = "Footer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Footer {
@XmlValue
private String value;
@XmlAttribute(name = "name")
private String name;
//Getter and setters here...
}
因此,我从输出中看到了三个问题:
有什么想法吗?这已经解决了。我的坏消息是我从gradle文件导入了以下内容 编译('com.fasterxml.jackson.dataformat:jackson-dataformat-xml')
因此,通过将其从build.gradle中删除,一切都会按预期进行 一,。在
标题
类中,您需要使用@xmldattribute
,而不是@xmlement
。很抱歉,我刚才输入错误,但我已经在代码中使用了@xmldattribute。我已经更新了帖子,谢谢。
@XmlRootElement(name = "Footer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Footer {
@XmlValue
private String value;
@XmlAttribute(name = "name")
private String name;
//Getter and setters here...
}