首页 > 解决方案 > 防止 Jackson XML 映射器将 wstxns 添加到命名空间

问题描述

当将对象序列化为 XML 并使用 Jackson 为属性指定命名空间时, @JacksonXmlRootElement(namespace = "http://...") 会将“wstxns1”附加或添加到命名空间。例如,假设我们有这些类:

VtexSkuAttributeValues.java

@JacksonXmlRootElement(localName = "listStockKeepingUnitName")
public class VtexSkuAttributeValues {

    @JacksonXmlProperty(localName = "StockKeepingUnitFieldNameDTO", namespace = "http://schemas.datacontract.org/2004/07/Vtex.Commerce.WebApps.AdminWcfService.Contracts")
    @JacksonXmlElementWrapper(useWrapping = false)
    private VtexSkuAttributeValue[] stockKeepingUnitFieldNameDTO;

    public VtexSkuAttributeValue[] getStockKeepingUnitFieldNameDTO() {
        return stockKeepingUnitFieldNameDTO;
    }

    public void setValues(VtexSkuAttributeValue[] values) {
        this.stockKeepingUnitFieldNameDTO = values;
    }
}

VtexSkuAttributeValue.java

@JacksonXmlRootElement(localName = "StockKeepingUnitFieldNameDTO", namespace = "http://schemas.datacontract.org/2004/07/Vtex.Commerce.WebApps.AdminWcfService.Contracts")
public class VtexSkuAttributeValue {

    private String fieldName;
    private FieldValues fieldValues;
    private int idSku;

    public int getIdSku() {
        return idSku;
    }

    public String getFieldName() {
        return fieldName;
    }

    public FieldValues getFieldValues() {
        return fieldValues;
    }

    public void setIdSku(int idSku) {
        this.idSku = idSku;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public void setFieldValues(FieldValues fieldValues) {
        this.fieldValues = fieldValues;
    }

    @JacksonXmlRootElement(localName = "fieldValues", namespace = "http://schemas.datacontract.org/2004/07/Vtex.Commerce.WebApps.AdminWcfService.Contracts")
    public static class FieldValues {
        @JacksonXmlProperty(namespace = "http://schemas.microsoft.com/2003/10/Serialization/Arrays")
        @JacksonXmlElementWrapper(useWrapping = false)
        public String[] string;

        public String[] getString() {
            return string;
        }

        public void setValues(String[] values) {
            this.string = values;
        }
    }
}

然后我使用XmlMapper来序列化并获取:

<listStockKeepingUnitName>
    <wstxns1:StockKeepingUnitFieldNameDTO xmlns:wstxns1="http://schemas.datacontract.org/2004/07/Vtex.Commerce.WebApps.AdminWcfService.Contracts">
        <fieldName>talle</fieldName>
        <fieldValues>
            <wstxns2:string xmlns:wstxns2="http://schemas.microsoft.com/2003/10/Serialization/Arrays">6184</wstxns2:string>
        </fieldValues>
        <idSku>258645</idSku>
    </wstxns1:StockKeepingUnitFieldNameDTO>
    <wstxns3:StockKeepingUnitFieldNameDTO xmlns:wstxns3="http://schemas.datacontract.org/2004/07/Vtex.Commerce.WebApps.AdminWcfService.Contracts">
        <fieldName>color</fieldName>
        <fieldValues>
            <wstxns4:string xmlns:wstxns4="http://schemas.microsoft.com/2003/10/Serialization/Arrays">6244</wstxns4:string>
        </fieldValues>
        <idSku>258645</idSku>
    </wstxns3:StockKeepingUnitFieldNameDTO>
</listStockKeepingUnitName>

即使这是有效的 XML,我正在使用的 Web 服务也不接受它。我调试了它,这是由于wstxns杰克逊出于某种原因添加的标签中的属性。有没有办法阻止杰克逊将其添加到标签中。我能想出的唯一解决方法是对生成的 XML 执行 string.replaceAll ,但这显然并不理想。

标签: javaxmljacksonjackson-dataformat-xml

解决方案


XML Jackson使用javax.xml.stream.XMLStreamWriter. 您可以配置该类的实例并为命名空间定义自己的前缀,并在需要时设置默认值。为此,我们需要扩展com.fasterxml.jackson.dataformat.xml.XmlFactory类并覆盖创建XMLStreamWriter实例的方法。示例实现可能如下所示:

class NamespaceXmlFactory extends XmlFactory {

    private final String defaultNamespace;
    private final Map<String, String> prefix2Namespace;

    public NamespaceXmlFactory(String defaultNamespace, Map<String, String> prefix2Namespace) {
        this.defaultNamespace = Objects.requireNonNull(defaultNamespace);
        this.prefix2Namespace = Objects.requireNonNull(prefix2Namespace);
    }

    @Override
    protected XMLStreamWriter _createXmlWriter(IOContext ctxt, Writer w) throws IOException {
        XMLStreamWriter writer = super._createXmlWriter(ctxt, w);
        try {
            writer.setDefaultNamespace(defaultNamespace);
            for (Map.Entry<String, String> e : prefix2Namespace.entrySet()) {
                writer.setPrefix(e.getKey(), e.getValue());
            }
        } catch (XMLStreamException e) {
            StaxUtil.throwAsGenerationException(e, null);
        }
        return writer;
    }
}

您可以按如下方式使用它:

import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.fasterxml.jackson.dataformat.xml.util.StaxUtil;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;

public class XmlMapperApp {

    public static void main(String[] args) throws Exception {
        String defaultNamespace = "http://schemas.datacontract.org/2004/07/Vtex.Commerce.WebApps.AdminWcfService.Contracts";
        Map<String, String> otherNamespaces = Collections.singletonMap("a", "http://schemas.microsoft.com/2003/10/Serialization/Arrays");

        XmlMapper xmlMapper = new XmlMapper(new NamespaceXmlFactory(defaultNamespace, otherNamespaces));
        xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);

        System.out.println(xmlMapper.writeValueAsString(new VtexSkuAttributeValues()));
    }
}

VtexSkuAttributeValues课堂上,您可以声明:

public static final String DEF_NMS = "http://schemas.datacontract.org/2004/07/Vtex.Commerce.WebApps.AdminWcfService.Contracts";

并将它用于应该用作默认命名空间的每个类和字段。例如:

@JacksonXmlProperty(localName = "StockKeepingUnitFieldNameDTO", namespace = DEF_NMS)

对于您不想更改名称的属性,您可以使用:

@JacksonXmlProperty(namespace = VtexSkuAttributeValues.DEF_NMS)

上面的代码打印了一些随机数据:

<listStockKeepingUnitName>
  <StockKeepingUnitFieldNameDTO xmlns="http://schemas.datacontract.org/2004/07/Vtex.Commerce.WebApps.AdminWcfService.Contracts">
    <fieldName>Name1</fieldName>
    <fieldValues>
      <a:string xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">6184</a:string>
    </fieldValues>
    <idSku>123</idSku>
  </StockKeepingUnitFieldNameDTO>
  <StockKeepingUnitFieldNameDTO xmlns="http://schemas.datacontract.org/2004/07/Vtex.Commerce.WebApps.AdminWcfService.Contracts">
    <fieldName>Name1</fieldName>
    <fieldValues>
      <a:string xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">6184</a:string>
    </fieldValues>
    <idSku>123</idSku>
  </StockKeepingUnitFieldNameDTO>
</listStockKeepingUnitName>

如果这不是您想要的,您可以使用该代码并尝试其他可用于配置此实例的方法。

Jackson使用在版本中创建此示例2.9.9


推荐阅读