gpt4 book ai didi

java - 自定义使用 wsdl 文件生成的 SOAP 请求中的前缀和命名空间位置

转载 作者:行者123 更新时间:2023-12-02 05:04:09 25 4
gpt4 key购买 nike

我很难理解为什么左侧代码生成的肥皂请求不起作用,但如果我将其调整到右侧的内容,那么它就可以工作?

enter image description here现在我知道需要做什么才能使其正常工作,我该如何解决它?

我将jaxws-maven-plugin添加到我的java项目中:

     <build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.5</version>
<configuration>
<sourceDestDir>src/main/java</sourceDestDir>
<wsdlDirectory>src/main/resources/wsdl</wsdlDirectory>
<wsdlFiles>
<wsdlFile>Flattened_Integrator7.0.wsdl</wsdlFile>
</wsdlFiles>
<keep>true</keep>
</configuration>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

注意上图中没有前缀wsse,它不起作用。

一定是这个词。它存在于 wsdl 文件中。 enter image description here有谁知道怎么办:

  1. 我可以强制“ http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd ”的命名空间前缀wsse
  2. 强制代码在 soap 信封 中生成命名空间,而不是在 Security 部分中生成

最佳答案

因此,我必须手动向信封添加前缀/命名空间,并将所有子级的前缀重命名为 wsse

这是我的做法:

@Component
public class RequestClient {

private static final String WSSE_PREFIX = "wsse";
private static final String WSSE_NAMESPACE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
private static final String NS2_PREFIX = "ns2";
private static final String NS2_NAMESPACE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";

private buildSoaprequest(){
...
SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();
soapEnvelope.addNamespaceDeclaration(WSSE_PREFIX, WSSE_NAMESPACE);
soapEnvelope.addNamespaceDeclaration(NS2_PREFIX, NS2_NAMESPACE);
SOAPHeader soapHeader = soapMessage.getSOAPHeader();
removeUndesiredBodyNamespaceEntries(soapHeader.getChildElements());
soapHeader.setPrefix(WSSE_PREFIX);
addDesiredBodyNamespaceEntries(soapHeader.getChildElements());
soapMessage.saveChanges();
...
}

private void addDesiredBodyNamespaceEntries(Iterator childElements) {
while (childElements.hasNext()) {
final Object childElementNode = childElements.next();
if (childElementNode instanceof SOAPElement) {
SOAPElement soapElement = (SOAPElement) childElementNode;
soapElement.setPrefix(WSSE_PREFIX);
addDesiredBodyNamespaceEntries(soapElement.getChildElements());
}
}
}

private void removeUndesiredBodyNamespaceEntries(Iterator childElements) {
while (childElements.hasNext()) {
final Object childElementNode = childElements.next();
if (childElementNode instanceof SOAPElement) {
SOAPElement soapElement = (SOAPElement) childElementNode;

//remove any prefix/namespace entries added by JAX-WS in the body element
//it cannot be null, so it will leave wsse
for (String prefix : getNamespacePrefixList(soapElement.getNamespacePrefixes())) {
if (prefix != null) {
soapElement.removeNamespaceDeclaration(prefix);
}
}
// recursively remove prefix/namespace entries in child elements
removeUndesiredBodyNamespaceEntries(soapElement.getChildElements());
}
}
}

private Set<String> getNamespacePrefixList(Iterator namespacePrefixIter) {
Set<String> namespacePrefixesSet = new HashSet<>();
while (namespacePrefixIter.hasNext()) {
namespacePrefixesSet.add((String) namespacePrefixIter.next());
}
return namespacePrefixesSet;
}

关于java - 自定义使用 wsdl 文件生成的 SOAP 请求中的前缀和命名空间位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56364313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com