Sandeep Soni
Sandeep Soni

Reputation: 31

@JacksonXmlProperty(isAttribute = true) appending unique id using Jackson XmlMapper

Hi i am trying to convert my POJO to xml using jackson-dataformat-xml 2.7.3 XmlMapper. I am using jackson annotations in POJO class as given in below code But i am getting some unique Ids getting appended in each tag of my list. How can i remove these unique ids.

// Below is ElementTag Class

import java.util.List;

import 
com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
/**
*Element class
*/
public class ElementTag {
@JacksonXmlProperty(localName = "FL")
@JacksonXmlElementWrapper(useWrapping = false)
private List<ProfessionalLeadDetails> pf;

/**
 * @return the pf
 */
public List<ProfessionalLeadDetails> getPf() {
    return pf;
}

/**
 * @param pf the pf to set
 */
public void setPf(List<ProfessionalLeadDetails> pf) {
    this.pf = pf;
}   
}

// Below is ProfessionalLeadDetails Class

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;


public class ProfessionalLeadDetails implements Serializable {

/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;


@JacksonXmlProperty(isAttribute = true)
 private String val;

 private String value;
    /**
 * @return the val
 */
public String getVal() {
    return val;
}
/**
 * @param val the val to set
 */
public void setVal(String val) {
    this.val = val;
}
/**
 * @return the value
 */
public String getValue() {
    return value;
}
/**
 * @param value the value to set
 */
public void setValue(String value) {
    this.value = value;
}
}

// converting to xml using XmlMapper inside main method

XmlMapper xmlMapper = new XmlMapper();
    ElementTag et = new ElementTag();
    List<ProfessionalLeadDetails> pfList = new 
ArrayList<ProfessionalLeadDetails>();
    ProfessionalLeadDetails pf = new ProfessionalLeadDetails();
    pf.setVal("First Name");
    pf.setValue("Sandeep");
    pfList.add(pf);
    pf = new ProfessionalLeadDetails();
    pf.setVal("Email");
    pf.setValue("[email protected]");
    pfList.add(pf);

    pfList.add(pf2);
    et.setPf(pfList);
    try {

System.out.println(xmlMapper.writer()
.with(SerializationFeature.WRAP_ROOT_VALUE)
            .withRootName("Leads").writeValueAsString(et));
} catch (JsonProcessingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

But i am getting some unique ids appended before val like zdef1999262822: as shown below : OUTPUT

  <Leads xmlns=""><FL zdef2041716767:val="First Name"><value>Sandeep</value></FL><FL zdef1999262822:val="Email"><value>[email protected]</value></FL></Leads>

DESIRED OUTPUT:

 <Leads xmlns=""><FL val="First Name"><value>Sandeep</value></FL><FL val="Email"><value>[email protected]</value></FL></Leads>

Thanks in advance!

Upvotes: 1

Views: 2438

Answers (1)

Sandeep Soni
Sandeep Soni

Reputation: 31

Make sure to use Woodstox Stax implementation, and not Stax implementation Oracle bundles with JDK. This is usually done by adding Maven dependency to explicitly include woodstox jar. This is explained on XML module

<dependency>
          <groupId>org.codehaus.woodstox</groupId>
          <artifactId>woodstox-core-asl</artifactId>
          <version>4.4.1</version>
        </dependency>

Upvotes: 0

Related Questions