开发者

Marshalling with MOXy

开发者 https://www.devze.com 2023-02-15 01:09 出处:网络
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></

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
@Pr开发者_JAVA百科oduces(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.


@XmlDescriminator node takes an XPath. To indicate that type is an attribute you can do the following:

@XmlDescriminatorNode("@type")

For an example see:

  • http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-moxy-extension.html
0

精彩评论

暂无评论...
验证码 换一张
取 消