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 namespace prefix (there is only a single namespace) in the SOAP request.
我正在使用Spring WebServiceTemplate进行webservice调用,该调用使用JAXB生成请求XML。我的需求需要所有元素(包括根)在SOAP请求中具有名称空间前缀(只有一个名称空间)。
Ex :
例:
<ns1:Login xmlns:ns1="www.example.com/a">
<ns1:username>abc</ns1:username>
<ns1:password>abc</ns1:password>
</ns1:Login>
But i am getting
但我得到
<Login xmlns="www.example.com/a">
<username>abc<username>
<password>abc<password>
</Login>
xsd :
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>
Generated Java Class from XSD
从XSD生成Java类
@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;
......
}
package-info.java
package-info.java
@javax.xml.bind.annotation.XmlSchema(
namespace = "www.example.com/a",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package authenticator.beans.login;
Want to know how to generate the request XML with Namespace prefix to all elements including root.
想知道如何为包括root在内的所有元素生成带有名称空间前缀的请求XML。
7 个解决方案
#1
62
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
在package-info.java
Took help of jaxb-namespaces-missing : Answer provided by Blaise Doughan
借助了Blaise Doughan提供的jaxb-namespace -missing:答案。
#2
4
MSK,
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.
对于我们的项目,它解决了命名空间问题。我们还必须创建名称空间前缀映射器。
#3
2
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";
}
});'
#4
2
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
…在package-info.java
#5
1
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
.
信号员。setProperty仅在Sun的JAX-B封送器上工作。问题是关于来自SpringSource的JAX-B marshaller,它不支持setProperty。
#6
1
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;
#7
0
For same query i want to set the xmlns for each element and the
prefix should not be there, e.g.
<Login xmlns="www.example.com/a">
<username xmlns="http://sample.com/user">abc<username>
<password xmlns="http://sample.com/user">abc<password>
</Login>
Please suggest....
#1
62
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
在package-info.java
Took help of jaxb-namespaces-missing : Answer provided by Blaise Doughan
借助了Blaise Doughan提供的jaxb-namespace -missing:答案。
#2
4
MSK,
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.
对于我们的项目,它解决了命名空间问题。我们还必须创建名称空间前缀映射器。
#3
2
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";
}
});'
#4
2
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
…在package-info.java
#5
1
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
.
信号员。setProperty仅在Sun的JAX-B封送器上工作。问题是关于来自SpringSource的JAX-B marshaller,它不支持setProperty。
#6
1
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;
#7
0
For same query i want to set the xmlns for each element and the
prefix should not be there, e.g.
<Login xmlns="www.example.com/a">
<username xmlns="http://sample.com/user">abc<username>
<password xmlns="http://sample.com/user">abc<password>
</Login>
Please suggest....