JAXB :Need Namespace Prefix to all the elements

前端 未结 6 2037
[愿得一人]
[愿得一人] 2020-11-28 06:15

I am Using Spring WebServiceTemplate to make webservice call which uses JAXB to generate request XML. My requirement needs all the elements (including root) to have a namesp

相关标签:
6条回答
  • 2020-11-28 06:29

    Another way is to tell the marshaller to always use a certain prefix

    marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper() {
                 @Override
                public String getPreferredPrefix(String arg0, String arg1, boolean arg2) {
                    return "ns1";
                }
            });'
    
    0 讨论(0)
  • 2020-11-28 06:31

    Was facing this issue, Solved by adding package-info in my package

    and the following code in it:

    @XmlSchema(
        namespace = "http://www.w3schools.com/xml/",
        elementFormDefault = XmlNsForm.QUALIFIED,
        xmlns = {
            @XmlNs(prefix="", namespaceURI="http://www.w3schools.com/xml/")
        }
    )  
    package com.gateway.ws.outbound.bean;
    
    import javax.xml.bind.annotation.XmlNs;
    import javax.xml.bind.annotation.XmlNsForm;
    import javax.xml.bind.annotation.XmlSchema;
    
    0 讨论(0)
  • 2020-11-28 06:37

    MSK,

    Have you tried setting a namespace declaration to your member variables like this? :

    @XmlElement(required = true, namespace = "http://example.com/a")
    protected String username;
    
    @XmlElement(required = true, namespace = "http://example.com/a")
    protected String password;
    

    For our project, it solved namespace issues. We also had to create NameSpacePrefixMappers.

    0 讨论(0)
  • 2020-11-28 06:50

    Solved by adding

    @XmlSchema(
        namespace = "http://www.example.com/a",
        elementFormDefault = XmlNsForm.QUALIFIED,
        xmlns = {
            @XmlNs(prefix="ns1", namespaceURI="http://www.example.com/a")
        }
    )  
    
    package authenticator.beans.login;
    import javax.xml.bind.annotation.*;
    

    in package-info.java

    Took help of jaxb-namespaces-missing : Answer provided by Blaise Doughan

    0 讨论(0)
  • 2020-11-28 06:51

    marshaller.setProperty only works on the JAX-B marshaller from Sun. The question was regarding the JAX-B marshaller from SpringSource, which does not support setProperty.

    0 讨论(0)
  • 2020-11-28 06:51

    To specify more than one namespace to provide prefixes, use something like:

    @javax.xml.bind.annotation.XmlSchema(
        namespace = "urn:oecd:ties:cbc:v1", 
        elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
        xmlns ={@XmlNs(prefix="cbc", namespaceURI="urn:oecd:ties:cbc:v1"), 
                @XmlNs(prefix="iso", namespaceURI="urn:oecd:ties:isocbctypes:v1"),
                @XmlNs(prefix="stf", namespaceURI="urn:oecd:ties:stf:v4")})
    

    ... in package-info.java

    0 讨论(0)
提交回复
热议问题