需求是这样的
<samlp:Response xmlns:samlp="something1" >
<saml:EncryptedAssertion xmlns:saml="something2">
<EncryptedData xmlns="something3" >
some more tags with namespace
</EncryptedData>
</saml:EncryptedAssertion>
</samlp:Response>
我使用 package-info.java 获得了根级别的前缀和命名空间,但我无法获取非根级别的命名空间定义,即
这就是我能够实现的目标
<samlp:Response xmlns:samlp="something1" xmlns:saml="something2" xmlns:ns6="someting3">
<saml:EncryptedAssertion >
<EncryptedData >
some more tags with namespace
</EncryptedData>
</saml:EncryptedAssertion>
</samlp:Response>
包信息.java
@XmlSchema(
namespace="something1",
elementFormDefault=XmlNsForm.UNQUALIFIED,
attributeFormDefault =XmlNsForm.UNQUALIFIED,
xmlns={
@XmlNs(prefix = "samlp", namespaceURI = "somthing1"),
@XmlNs(prefix = "saml", namespaceURI = "something2"),
@XmlNs(prefix = "", namespaceURI = "something3"),
}
)
我知道命名空间可以在父/根节点中定义,并且它将是有效的,但我正在使用 Web 服务,因此我不确定在父级别中声明的命名空间在解码时是否会产生任何影响,这就是我尝试生成精确的 saml 响应的原因。
最佳答案
我就是这样做的。 不确定天气是对还是错,但它适用于我提到的有问题的场景。
添加了特定 JAXBclass 中的必需属性
public class EncryptedAssertionType { @XmlAttribute(name = "xmlns:saml") protected String samlNamespace;
为同一变量生成 getter/setter 并在编码时设置变量
EncryptedAssertionType encryptedAssertion = new EncryptedAssertionType(); encryptedAssertion.setSamlNamespace("something2");
如果需要,您可以将此类属性添加到所有级别
无需包含package-info.java
这给了我预期的回应
<samlp:Response xmlns:samlp="something1" >
<saml:EncryptedAssertion xmlns:saml="something2">
关于java - JAXB 为非根元素提供命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37518284/