用MOXy编组

时间:2011-03-07 09:22:45

标签: jaxb eclipselink 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;
}

我想我错过了什么,不知道它会是什么?

感谢。

1 个答案:

答案 0 :(得分:1)

@XmlDescriminator节点采用XPath。要指明该类型是属性,您可以执行以下操作:

@XmlDescriminatorNode("@type")

有关示例,请参阅:

相关问题