简体   繁体   中英

Jackson completely ignore Jaxb XML related annotations

Imagine a situation when you have a model in Java, and you have to serialize it both to XML and CSV.

I am using Jaxb Marshaller for XML and Jackson's CsvMapper ( ObjectMapper ) for CSV.

Since the formats are slightly different, I want Jackson's CsvMapper to ignore Jaxb related annotations like @XmlType or @XmlElement . Because Jackson is getting information/considers xml annotations as well and it leads to wrong result.

How can I do it?

This is the way how you probably create new CsvMapper :

CsvMapper csvMapper = new CsvMapper();
csvMapper.findAndRegisterModules();

findAndRegisterModules method invokes findModules which documentation says:

Method for locating available methods, using JDK ServiceLoader facility, along with module-provided SPI.

So, probably you have jackson-module-jaxb-annotations module on class path and this is why CsvMapper recognizes JAXB annotations.

To fix it, you need to remove invocation of this method findAndRegisterModules and register only these modules you needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM