簡體   English   中英

使用wsdl文件生成的soap請求中自定義前綴和命名空間位置

[英]Customizing prefix and namespace location in soap request generated using wsdl file

我很難理解為什么左邊的代碼生成的肥皂請求不起作用,但是如果我把它調整到右邊的那個,那么它有效嗎?

在此輸入圖像描述 現在我知道需要做些什么來使它工作,我該如何解決它?

我在我的java項目中添加了jaxws-maven-plugin

     <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>

請注意上圖中沒有prefix wsse ,它不起作用。

必須是那個詞。 它存在於wsdl文件中。 在此輸入圖像描述 有誰知道如何:

  1. 我可以強制namespace prefix為“ http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd ”的namespace prefixwsse
  2. 強制代碼在soap envelope生成名稱空間,而不是在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;
      }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM