我正在使用javax.xml.bind.annotation.XmlRootElement注释对象将其序列化为xml字符串。

        JAXBContext jc = JAXBContext.newInstance(obj.getClass());
        // Marshal the object to a StringWriter
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.example.com/schema.xsd");
        StringWriter stringWriter = new StringWriter();
        marshaller.marshal(obj, stringWriter);
        result = stringWriter.toString();


如何排除对象的某些字段以便不发送它们?为了将其从最终字符串中排除,必须注释该类字段的注释。

最佳答案

使用@XmlTransient批注:


  防止将JavaBean属性/类型映射到XML表示形式。


@XmlTransient
public String toBeSkippedField;

10-08 19:10