Java 是否可以仅在JAXB/Moxy解组期间忽略字段
我对Java 是否可以仅在JAXB/Moxy解组期间忽略字段,java,jaxb,eclipselink,unmarshalling,moxy,Java,Jaxb,Eclipselink,Unmarshalling,Moxy,我对JAXB/Moxy相当陌生。我正试图将XML文件解压到我的POJO中。我只想在JAXB/Moxy解组期间忽略一个字段,但我想在封送期间保留该字段。我尝试了很多东西,但似乎都不管用,所以我想发布一些建议 我知道我可以使用@xmltransive,但这将使该字段在封送处理中也不可用。经过大量的试验和研究,我了解了@XmlNamedObjectGraphs,但这只适用于封送处理,当我为MOXY unmarshaller添加属性时,它似乎没有任何区别 有什么我遗漏的吗?我找不到任何将@XmlName
JAXB/Moxy
相当陌生。我正试图将XML文件解压到我的POJO中。我只想在JAXB/Moxy解组期间忽略一个字段
,但我想在封送期间保留该字段。我尝试了很多东西,但似乎都不管用,所以我想发布一些建议
我知道我可以使用@xmltransive
,但这将使该字段在封送处理中也不可用。经过大量的试验和研究,我了解了@XmlNamedObjectGraphs
,但这只适用于封送处理
,当我为MOXY unmarshaller
添加属性时,它似乎没有任何区别
有什么我遗漏的吗?我找不到任何将@XmlNamedObjectGraphs
与Unmarshler
一起使用的示例,大多数示例都是与marshaller
一起使用的,因此考虑询问并获得一些建议。任何帮助都将不胜感激
以下是我的客户POJO
:我只想在封送期间使用其他
字段,并在封送期间将其隐藏。有办法吗
@XmlNamedObjectGraphs({
@XmlNamedObjectGraph(
name = "full",
attributeNodes = {
@XmlNamedAttributeNode("name"),
@XmlNamedAttributeNode("age"),
}
)
})
@XmlRootElement(name = "Customer")
@XmlType(name = "Customer", propOrder = {"name", "age", "others"})
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@Setter
@NoArgsConstructor
public class Customer {
private String name;
private String age;
@XmlPath(".")
@XmlJavaTypeAdapter(TestAdapter.class)
@JsonSerialize(using = CustomExtensionsSerializer.class)
private Map<String, Object> others = new HashMap<>();
@JsonAnySetter
public void setOthers(String key, Object value) {
others.put(key, value);
}
}
以下是我的Main
课程:
public class Unmarshalling {
public static void main(String[] args) throws JAXBException, XMLStreamException, FactoryConfigurationError, JsonProcessingException {
try {
final InputStream inputStream = Unmarshalling.class.getClassLoader().getResourceAsStream("customer.xml");
final InputStream xsdStream = Unmarshalling.class.getClassLoader().getResourceAsStream("customer.xsd");
final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
final JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setEventHandler(new DefaultValidationEventHandler());
unmarshaller.setProperty(UnmarshallerProperties.OBJECT_GRAPH, "full");
final Customer customer = unmarshaller.unmarshal(xmlStreamReader, Customer.class).getValue();
System.out.println(customer);
} catch (JAXBException exp) {
System.out.println(exp);
}
}
}
有人能提出一些解决办法吗
public class Unmarshalling {
public static void main(String[] args) throws JAXBException, XMLStreamException, FactoryConfigurationError, JsonProcessingException {
try {
final InputStream inputStream = Unmarshalling.class.getClassLoader().getResourceAsStream("customer.xml");
final InputStream xsdStream = Unmarshalling.class.getClassLoader().getResourceAsStream("customer.xsd");
final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
final JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setEventHandler(new DefaultValidationEventHandler());
unmarshaller.setProperty(UnmarshallerProperties.OBJECT_GRAPH, "full");
final Customer customer = unmarshaller.unmarshal(xmlStreamReader, Customer.class).getValue();
System.out.println(customer);
} catch (JAXBException exp) {
System.out.println(exp);
}
}
}