JAXB:所有元素都需要命名空间前缀
我正在使用 Spring WebServiceTemplate 进行 Web 服务调用,该调用使用 JAXB 生成请求 XML。我的要求需要 SOAP 请求中的所有元素(包括根)都具有名称空间前缀(只有一个名称空间)。
例如:
<ns1:Login xmlns:ns1="www.example.com/a">
<ns1:username>abc</ns1:username>
<ns1:password>abc</ns1:password>
</ns1:Login>
但是我得到了
<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>
生成的 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;
......
}
从 XSD package-info.java
@javax.xml.bind.annotation.XmlSchema(
namespace = "www.example.com/a",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package authenticator.beans.login;
想知道如何生成带有命名空间前缀的请求 XML 到所有元素(包括根)。
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.
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 :
<?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
@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
@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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
通过添加
package-info.java
来解决获取 jaxb-namespaces-missing 的帮助:Blaise 提供的答案道安
Solved by adding
in package-info.java
Took help of jaxb-namespaces-missing : Answer provided by Blaise Doughan
MSK,
您是否尝试过像这样为成员变量设置命名空间声明? :
对于我们的项目,它解决了命名空间问题。我们还必须创建 NameSpacePrefixMappers。
MSK,
Have you tried setting a namespace declaration to your member variables like this? :
For our project, it solved namespace issues. We also had to create NameSpacePrefixMappers.
要指定多个名称空间来提供前缀,请使用类似以下内容的内容:
... in package-info.java
To specify more than one namespace to provide prefixes, use something like:
... in package-info.java
遇到了这个问题,通过在我的包中添加 package-info
并在其中添加以下代码来解决:
Was facing this issue, Solved by adding package-info in my package
and the following code in it:
另一种方法是告诉编组器始终使用某个前缀
Another way is to tell the marshaller to always use a certain prefix
marshaller.setProperty
仅适用于 Sun 的 JAX-B 编组器。问题是关于SpringSource
的 JAX-B 编组器,它不支持setProperty
。marshaller.setProperty
only works on the JAX-B marshaller from Sun. The question was regarding the JAX-B marshaller fromSpringSource
, which does not supportsetProperty
.