根据我的要求配置Jackson XmlMapper

时间:2016-06-22 05:29:49

标签: xml jackson marshalling

我想配置jackson XmlMapper,以便writeValueAsString方法提供以下要求:

1. Eliminate null/empty tags: Ex: <carrierDocumentId/>
2. Tags to display its namespace: Ex: <documentId> to displayed as   
  <ims:documentId>
3. Tags to use the upper case letter as provided in 
     @XmlElement(name = "DocumentId")
4. List objects to not to include a wrapper tag: 
<documentId><documentId>12345</documentId></documentId>
5. Attribute tags to display as attribute instead of element: 
  Ex: <id><value>6802554587</value><idOrigin>Invoice</idOrigin></id>
  to be displayed as <id  idOrigin="Invoice">6802554587</id> 

我尝试了不同的组合:

   //JaxbAnnotationModule module = new JaxbAnnotationModule();
        //AnnotationIntrospector introspector = new 
        JaxbAnnotationIntrospector(serializeMapper.getTypeFactory());   
        //serializeMapper.setAnnotationIntrospector(introspector);
        //serializeMapper.registerModule(module);       

     //serializeMapper.configure(SerializationFeature.WRAP_ROOT_VALUE,false);      

   //serializeMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

   //serializeMapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);

   //serializeMapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

        //JacksonXmlModule module = new JacksonXmlModule();
        JaxbAnnotationModule module = new JaxbAnnotationModule();
        // and then configure, for example:
        //module.setDefaultUseWrapper(false);
        serializeMapper = new XmlMapper();  
        AnnotationIntrospector introspector = new 
         JaxbAnnotationIntrospector(serializeMapper.getTypeFactory());   
        serializeMapper.setAnnotationIntrospector(introspector);
        serializeMapper.registerModule(module);     

感谢 马杜

1 个答案:

答案 0 :(得分:0)

我使用了如下的JAXB类并删除了jackson XmlMapper。        JAXBContext jc = JAXBContext.newInstance(ShowInvoice.class);        Marshaller m = jc.createMarshaller(); 这提供了我正在寻找的格式化

由于 马杜