Prevent Jackson XML mapper from adding wstxns to namespaces

心已入冬 提交于 2019-12-19 10:45:16

问题


When serialising objects to XML and specifying namespaces for properties using @JacksonXmlRootElement(namespace = "http://...") Jackson will append or prepend ´wstxns1´ to the namespace. For example, say we have these classes:

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

I then use the XmlMapper to serialise and get:

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

Even though this is valid XML, the web service I'm working with doesn't accept it. I debugged it and it's due to the wstxns properties in the tags that Jackson adds for some reason. Is there a way to prevent Jackson from adding that to the tags. The only workaround I could come up with is performing a string.replaceAll on the resulting XML but it's obviously not ideal.


回答1:


To write XML Jackson uses javax.xml.stream.XMLStreamWriter. You can configure instance of that class and define your own prefixes for namespaces and set default one if needed. To do that we need to extend com.fasterxml.jackson.dataformat.xml.XmlFactory class and override a method which creates XMLStreamWriter instance. Example implementation could look like below:

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

You can use it as below:

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

In VtexSkuAttributeValues class you can declare:

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

and use it for every class and field where it should be used as default namespace. For example:

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

For properties, for which you do not want to change name you can use:

@JacksonXmlProperty(namespace = VtexSkuAttributeValues.DEF_NMS)

Above code prints for some random data:

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

If it is not what you want you can play with that code and try other methods which are available for you to configure this instance.

To create this example Jackson in version 2.9.9 was used.




回答2:


This seems to be the missing piece. It allows you to set the prefix and namespace.

   static 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 {
        XMLStreamWriter2 writer = (XMLStreamWriter2)super._createXmlWriter(ctxt, w);
        try {
            writer.setDefaultNamespace(defaultNamespace);
            writer.setPrefix("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            for (Map.Entry<String, String> e : prefix2Namespace.entrySet()) {
                writer.setPrefix(e.getKey(), e.getValue());
            }
        } catch (XMLStreamException e) {
            StaxUtil.throwAsGenerationException(e, null);
        }
        return writer;
    }
}

The only remaining issue I have is

    @JacksonXmlProperty(localName = "@xsi.type", isAttribute = true, namespace = "http://www.w3.org/2001/XMLSchema-instance")
@JsonProperty("@xsi.type")
private String type;

Creates the following output:

Still trying to resolve how to make it be xsi:type="networkObjectGroupDTO" instead.



来源:https://stackoverflow.com/questions/58052669/prevent-jackson-xml-mapper-from-adding-wstxns-to-namespaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!