gpt4 book ai didi

java - 从序列化中排除某些对象字段

转载 作者:行者123 更新时间:2023-12-01 21:45:52 25 4
gpt4 key购买 nike

我正在使用 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注释:

Prevents the mapping of a JavaBean property/type to XML representation.

@XmlTransient
public String toBeSkippedField;

关于java - 从序列化中排除某些对象字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36008263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com