2011-10-09 90 views
2

我有興趣不編組/解組我的A對象的主體字段。我在不同的地方添加了0​​,但它似乎仍然編組它。對於javaBean屬性,@XmlTransient

任何想法?

這裏是A類:

@XmlRootElement(name = "A") 
    public class AImpl implements A, Serializable { 

    private String attrName; 
    private String attrValue; 

    @XmlTransient 
    private Object principal; 

    public class Adapter extends XmlAdapter<AImpl,A> { 
     public A unmarshal(AImpl v) { return v; } 
     public AImpl marshal(A v) { return (AImpl)v; } 
    } 

    public String getAttrName() { 
    return attrName; 
    } 
    public void setAttrName(String s) { 
    this.attrName = s; 
    } 

    public String getAttrValue() { 
    return attrValue; 
    } 

    public void setAttrValue(String s) { 
    this.attrValue = s; 
    } 

    @XmlTransient 
    public Object getPrincipal() { 
    return principal; 
    } 

    @XmlTransient 
    public void setPrincipal(Object o) { 
    this.principal = o; 
    } 
} 

這是我如何元帥它:

JAXBContext context = JAXBContext.newInstance(AImpl.class); 
Marshaller m = context.createMarshaller(); 
m.setProperty(javax.xml.bind.Marshaller.JAXB_FRAGMENT,true); 
m.marshal(al sw);) 
+1

關於Bozho的回答指定@XmlAccessType(XmlAccessType.FIELD),這個鏈接可以幫助你理解爲什麼你應該指定@XmlAccessType http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html –

回答

1

嘗試在你的類

+2

@XmlAccessorType(XmlAccessType.FIELD) –