born to code
born to code

Reputation: 133

Jackson Xml duplicate Tag name

I'm generating xml using jackson xml, My POJO class for jackson xml generation as below:

public class data { 

    @JacksonXmlProperty(localName="Element") 
    Element element = new Element();  

}
public class Element {
    @JacksonXmlProperty(localName="element1")
    private List<String> element1;
    public List<String> getElement1() {
        return element1;
    }    
    public void setElement1(List<String> element1) {
        this.element1 = element1;
    }
}

I'm expecting an output like:

<Element>
  <element1></element1>
  <element1></element1>
  <element1></element1>
</Element>

but I'm getting:

<Element>
 <element1>
   <element1></element1>
   <element1></element1>
   <element1></element1>
 <element1>
</Element>

how to solve this ?

Upvotes: 4

Views: 2937

Answers (2)

Dimitar II
Dimitar II

Reputation: 2519

You can disable collection elements duplication globally (to avoid repeating/using annotation on multiple places). Just create your mapper with setting defaultUseWrapper(false):

mapper = XmlMapper.builder().defaultUseWrapper(false).build();

Upvotes: 0

Ori Marko
Ori Marko

Reputation: 58882

Add JacksonXmlElementWrapper

 @JacksonXmlElementWrapper(useWrapping = false)   
 @JacksonXmlProperty(localName="element1")
    private List<String> element1;

Annotation that is similar to JAXB javax.xml.bind.annotation.XmlElementWrapper, to indicate wrapper element to use (if any) for Collection types (arrays, java.util.Collection). If defined, a separate container (wrapper) element is used; if not, entries are written without wrapping.

Upvotes: 6

Related Questions