如何动态地将 xml 元素添加到 java JAXRS 表示中?
假设我有一个 java 类,它表示“Dog”,其属性“Color”和“Type”用 @XmlElement
和 @XmlRootElement
标记标记。如何以编程方式将“Age”xml 元素添加到这个基于 JAXRS 的类,而无需使用 @XmlAttribute(name="Age")
修改我的 Dog 类?
假设我有代表:
DogRepClass adog = new DogRepClass();
Say I have a java class that is a representation of a "Dog" with attrbutes "Color" and "Type" marked up with @XmlElement
and @XmlRootElement
tags. How do I add a "Age" xml element to this JAXRS based class programmatically without modifying my Dog class with @XmlAttribute(name="Age")
?
Assume I have the representation:
DogRepClass adog = new DogRepClass();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以仅针对此特定类型定义自己的 MessageBodyWriter (http://jsr311.java.net/nonav/javadoc/javax/ws/rs/ext/MessageBodyWriter.html)。它可以将您的实例编组到 DOM 中,对其进行修改并随后进行序列化。既不漂亮也不优雅,但它会起作用。
You can define your own MessageBodyWriter (http://jsr311.java.net/nonav/javadoc/javax/ws/rs/ext/MessageBodyWriter.html) only for this particular type. It can marshall your instance into DOM, modify it and serialize afterwards. Not nice nor elegant, but it will work.
您必须创建自己的
MessageBodyWriter
,但您可以扩展它来处理一些自定义注释,您的消息正文编写器可以通过.entity(Object , Annotation[])
方法。这样你只需添加一些特定于你的需求的东西You'd have to create your own
MessageBodyWriter
but rather than make your own from scratch, you can extend it to process some custom annotations that your message body writer can recognize via the.entity(Object, Annotation[])
method. That way you just add on something specific for your needs