@JacksonXmlProperty(isAttribute = true)使用Jacson XmlMapper附加唯一ID

时间:2017-08-31 17:22:17

标签: java jackson jackson-dataformat-xml xmlmapper

您好我正在尝试使用jackson-dataformat-xml 2.7.3 XmlMapper将我的POJO转换为xml。我正在POJO类中使用jackson注释,如下面的代码所示。但是我得到一些独特的ID会被附加到我的列表的每个标记中。如何删除这些独特的ID。

//下面是 ElementTag类

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;
}   
}

//下面是 ProfessionalLeadDetails类

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;
}
}

// 使用主方法中的XmlMapper转换为xml

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("Sandeep@gmail.com");
    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();
}

但是我在val之前添加了一些独特的id,如zdef1999262822:如下所示: 的输出

  <Leads xmlns=""><FL zdef2041716767:val="First Name"><value>Sandeep</value></FL><FL zdef1999262822:val="Email"><value>Sandeep@gmail.com</value></FL></Leads>

期望的输出:

 <Leads xmlns=""><FL val="First Name"><value>Sandeep</value></FL><FL val="Email"><value>Sandeep@gmail.com</value></FL></Leads>

提前致谢!

1 个答案:

答案 0 :(得分:0)

确保使用Woodstox Stax实现,而不是使用JDK的Stax实现Oracle捆绑包。这通常通过添加Maven依赖项来明确包含woodstox jar来完成。这在XML模块

上有解释
<dependency>
          <groupId>org.codehaus.woodstox</groupId>
          <artifactId>woodstox-core-asl</artifactId>
          <version>4.4.1</version>
        </dependency>