首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据我的需求配置Jackson XmlMapper

根据我的需求配置Jackson XmlMapper
EN

Stack Overflow用户
提问于 2016-06-22 13:29:49
回答 1查看 433关注 0票数 0

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

代码语言:javascript
复制
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> 

我尝试了不同的组合:

代码语言:javascript
复制
   //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);     

谢谢马德胡

EN

回答 1

Stack Overflow用户

发布于 2016-06-23 12:32:35

我按如下方式使用了JAXB类,并删除了jackson XmlMapper。Marshaller jc = JAXBContext.newInstance(ShowInvoice.class);JAXBContext m= jc.createMarshaller();这提供了我想要的格式

谢谢马德胡

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37959267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档