JAXB: Necesita Prefijo de Espacio de nombres para todos los elementos


Estoy usando Spring WebServiceTemplate para hacer una llamada a webservice que usa JAXB para generar XML de solicitud. My requirement necesita que todos los elementos (incluyendo root) tengan un prefijo de espacio de nombres (solo hay un espacio de nombres) en la solicitud SOAP.

Ex:

<ns1:Login xmlns:ns1="www.example.com/a">
    <ns1:username>abc</ns1:username>
    <ns1:password>abc</ns1:password>
</ns1:Login>

, Pero estoy recibiendo

<Login xmlns="www.example.com/a">
    <username>abc<username>
    <password>abc<password>
</Login>

Xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="www.example.com/a"   xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ilreq="www.example.com/a" elementFormDefault="qualified" attributeFormDefault="unqualified">

<xs:complexType name="Login">
    <xs:sequence>
        <xs:element name="username" type="xs:string"/>
        <xs:element name="password" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

Clase Java generada desde XSD

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Login", propOrder = {
    "username",
    "password"
})

@XmlRootElement
public class Login {

@XmlElement(required = true)
protected String username;
@XmlElement(required = true)
protected String password;
......
}

Información del paquete.java

@javax.xml.bind.annotation.XmlSchema(
    namespace = "www.example.com/a",
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package authenticator.beans.login;

Desea saber cómo generar el XML de solicitud con Espacio de nombres prefijo a todos los elementos incluyendo root.

Author: Jin Kwon, 2011-08-01

6 answers

Resuelto añadiendo

@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.*;

En package-info.java

Tomó la ayuda de jaxb-namespaces-missing : Respuesta proporcionada por Blaise Doughan

 65
Author: Sai Kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:26:24

MSK,

¿Ha intentado establecer una declaración de espacio de nombres para sus variables miembro como esta? :

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

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

Para nuestro proyecto, resolvió problemas de espacio de nombres. También tuvimos que crear NameSpacePrefixMappers.

 6
Author: Pat B,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-10-04 13:22:36

Otra forma es decirle al marshaller que siempre use un cierto prefijo

marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper() {
             @Override
            public String getPreferredPrefix(String arg0, String arg1, boolean arg2) {
                return "ns1";
            }
        });'
 3
Author: devnoo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-10-06 14:59:23

Se enfrentaba a este problema, Resuelto agregando package-info en mi paquete

Y el siguiente código en él:

@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;
 3
Author: Mohan Seth,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-22 09:51:19

marshaller.setProperty sólo funciona en el JAX-B marshaller de Sun. La pregunta era sobre el marshaller JAX-B de SpringSource, que no soporta setProperty.

 2
Author: A. Rick,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-26 00:48:18

Para especificar más de un espacio de nombres para proporcionar prefijos, use algo como:

@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")})

... en el paquete-info.java

 2
Author: D. Thorne,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-14 20:40:32