以下 XML 的 JAXB 注释
我花了一段时间试图让它发挥作用,但运气不佳。
我有以下 XML:
<message>
<buckets>
<bucket>
<channels>
<channel>Test A</channel>
<channel>Test B</channel>
</channels>
<messageText>This is sample text</messageText>
</bucket>
<bucket>
....
</bucket>
</buckets>
<userId>10</userId>
</message>
我试图使用 JAXB 注释将其映射到 POJO,但遇到了麻烦。
我的课程如下:
@XmlRootElement(name="message")
public class MessageRS {
public static class Bucket {
private List<String>channels;
private String text;
private Bucket() {}
public List<String> getChannels() { .... }
public void setChannels(List<String> channels) { .... }
public String getText() { .... }
public void setText(String text) { .... }
}
private List<Bucket> buckets;
private Long userId;
private MessageRS() { }
public List<Bucket getBuckets() { .... }
public void setBuckets(List<Bucket> buckets) { .... }
public long geUserId() { .... }
public void setUserId(long UserId { .... }
}
不幸的是,这似乎无法正常工作。我将如何注释它以正确地将 XML 映射到该对象(所需的映射应该是显而易见的)?
这里没有什么是一成不变的——此时我可以随意更改 XML 和 Java 类结构。
I've spent a while trying to get this to work however I've had little luck.
I have the following XML:
<message>
<buckets>
<bucket>
<channels>
<channel>Test A</channel>
<channel>Test B</channel>
</channels>
<messageText>This is sample text</messageText>
</bucket>
<bucket>
....
</bucket>
</buckets>
<userId>10</userId>
</message>
I'm trying to get this mapped to a POJO using JAXB annotations but am having trouble.
My class is as follows:
@XmlRootElement(name="message")
public class MessageRS {
public static class Bucket {
private List<String>channels;
private String text;
private Bucket() {}
public List<String> getChannels() { .... }
public void setChannels(List<String> channels) { .... }
public String getText() { .... }
public void setText(String text) { .... }
}
private List<Bucket> buckets;
private Long userId;
private MessageRS() { }
public List<Bucket getBuckets() { .... }
public void setBuckets(List<Bucket> buckets) { .... }
public long geUserId() { .... }
public void setUserId(long UserId { .... }
}
Unfortunately, this doesn't seem to be working correctly. How would I annotate this to correctly map the XML to this object (the desired mapping should be obvious)?
Nothing is set in stone here - I'm free to change both the XML and Java class structures at this point.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
您在
getUserId()
中也有一个拼写错误,这会导致该属性无法正常工作。它是 geUserId()。而且,像这样混合 Long 和 long 是很危险的。如果您的 userId 为 null 并且您调用
getUserId()
,它将抛出 NullPointerException。Try this:
You also had a typo in
getUserId()
, which would make that property not work correctly. It wasgeUserId()
.Also, it's dangerous to mix Long and long like that. If your userId is null and you call
getUserId()
, it'll throw a NullPointerException.通常,jaxb 在列表周围使用包装对象。因此,您将拥有一个 Buckets 类,其中包含
List; getBuckets()
,并且您将拥有一个带有List的 Channels 类。 getChannels()。我相信您可以使用额外的注释来避免额外的包装类,但它们并不经常使用(至少不被常见的自动生成工具使用)。
就我个人而言,我发现编写 xml 模式并使用 xjc 生成 java 类要简单得多。
typically, jaxb uses wrapper objects around lists. so, you would have a Buckets class which has
List<Bucket> getBuckets()
, and you would have a Channels class withList<Channel> getChannels()
. i believe there are extra annotations you can use to avoid the extra wrapper classes, but they are not frequently used (at least not by the common auto-generation tools).personally, i find it much simpler to write the xml schema and use xjc to generate the java classes.