使用 MOXy 进行编组
我在使用 MOXy 解组时没有遇到任何问题。这是我已经解组的 XML。
<eng><shape type="square"><square-specific>dasdasdas</square-specific></shape></eng>
但是在编组时,我得到这个:
<eng><shape><type/><square-specific>dasdasdas</square-specific></shape></eng>
这是我的模型文件:
@XmlRootElement(name="eng")
public class Eng {
private Shape shape;
public void setShape(Shape shape) {
this.shape = shape;
}
@XmlElement
public Shape getShape() {
return shape;
}
}
@XmlDiscriminatorNode("type")
public abstract class Shape {
}
@XmlDiscriminatorValue("square")
public class Square extends Shape {
private String squareSpecificAttribute;
@XmlElement(name="square-specific")
public String getSquareSpecificAttribute() {
return squareSpecificAttribute;
}
public void setSquareSpecificAttribute(String s) {
this.squareSpecificAttribute = s;
}
}
这是我的控制器中的方法:
@GET
@Produces(MediaType.APPLICATION_XML)
public Eng get(){
Eng e = new Eng();
Square s = new Square();
s.setSquareSpecificAttribute("dasdasdas");
e.setShape(s);
return e;
}
我想我错过了一些东西,知道它会是什么吗?
谢谢。
I'm experiencing no problems while unmarshalling with MOXy. This is the XML, I've unmarshalled.
<eng><shape type="square"><square-specific>dasdasdas</square-specific></shape></eng>
But when marshalling, I get this:
<eng><shape><type/><square-specific>dasdasdas</square-specific></shape></eng>
Here's my model files:
@XmlRootElement(name="eng")
public class Eng {
private Shape shape;
public void setShape(Shape shape) {
this.shape = shape;
}
@XmlElement
public Shape getShape() {
return shape;
}
}
@XmlDiscriminatorNode("type")
public abstract class Shape {
}
@XmlDiscriminatorValue("square")
public class Square extends Shape {
private String squareSpecificAttribute;
@XmlElement(name="square-specific")
public String getSquareSpecificAttribute() {
return squareSpecificAttribute;
}
public void setSquareSpecificAttribute(String s) {
this.squareSpecificAttribute = s;
}
}
And this is the method in my controller:
@GET
@Produces(MediaType.APPLICATION_XML)
public Eng get(){
Eng e = new Eng();
Square s = new Square();
s.setSquareSpecificAttribute("dasdasdas");
e.setShape(s);
return e;
}
I guess I'm missing something, any idea what could it be?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@XmlDecriminator 节点采用 XPath。要指示 type 是一个属性,您可以执行以下操作:
有关示例,请参阅:
@XmlDescriminator node takes an XPath. To indicate that type is an attribute you can do the following:
For an example see: