2023-10-23   


参考来源:https://www.baeldung.com/java-soap-web-service

依赖 和 插件

java11及以上:

<dependency>  
    <groupId>jakarta.xml.ws</groupId>  
    <artifactId>jakarta.xml.ws-api</artifactId>  
    <version>4.0.0</version>  
</dependency>  
<dependency>  
    <groupId>com.sun.xml.ws</groupId>  
    <artifactId>jaxws-rt</artifactId>  
    <version>4.0.1</version>  
    <scope>runtime</scope>  
</dependency>  
<dependency>  
    <groupId>com.sun.xml.ws</groupId>  
    <artifactId>jaxws-ri</artifactId>  
    <version>4.0.1</version>  
    <type>pom</type>  
</dependency>

...
        <plugins>
            <plugin>
                <groupId>com.sun.xml.ws</groupId>
                <artifactId>jaxws-maven-plugin</artifactId>
                <version>4.0.1</version>
                <configuration>
                    <wsdlUrls>
                        <wsdlUrl>http://127.0.0.1/uapws/service/demo.IHelloWorld?wsdl</wsdlUrl>
                    </wsdlUrls>
                    <keep>true</keep>
                    <packageName>com.baeldung.soap.ws.client.generated</packageName>
                    <sourceDestDir>src/main/java</sourceDestDir>
                </configuration>
            </plugin>
        </plugins>

java8:

<dependencies>  
    <dependency>
		<groupId>jakarta.xml.ws</groupId>  
        <artifactId>jakarta.xml.ws-api</artifactId>  
        <version>3.0.0</version>  
    </dependency>    
    <dependency>        
    <groupId>com.sun.xml.ws</groupId>  
        <artifactId>jaxws-rt</artifactId>  
        <version>3.0.0</version>  
        <scope>runtime</scope>  
    </dependency>    
    <dependency>        
    <groupId>com.sun.xml.ws</groupId>  
        <artifactId>jaxws-ri</artifactId>  
        <version>3.0.2</version>  
        <type>pom</type>  
    </dependency></dependencies>  
  
<build>  
    <plugins>        
	    <plugin>            
		    <groupId>com.sun.xml.ws</groupId>  
	            <artifactId>jaxws-maven-plugin</artifactId>  
	            <version>3.0.2</version>  
	            <configuration>                
		            <wsdlUrls>                    
			            <wsdlUrl>http://127.0.0.1/uapws/service/demo.IHelloWorld?wsdl</wsdlUrl>  
	                </wsdlUrls>                
	                <keep>true</keep>  
	                <packageName>com.baeldung.soap.ws.client.generated</packageName>  
	                <sourceDestDir>src/main/java</sourceDestDir>  
	            </configuration>        
	    </plugin>    
	</plugins>
</build>

主要就是依赖版本不同。

生成代码

调用插件生成 mvn jaxws:wsimport

代码调用

IHelloWorld iHelloWorld = new IHelloWorld();  
String result = iHelloWorld.getIHelloWorldSOAP11PortHttp().sayHelloName("生成代码方式调用", DateUtil.now());  
System.out.println(result);

最后贴上wsdl文件定义

<wsdl:definitions jaxb:version="2.0" targetNamespace="http://demo/IHelloWorld" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:jaxws="http://java.sun.com/xml/ns/jaxws" xmlns:ns0="http://demo/IHelloWorld" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <wsdl:types>
      <xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" jaxb:version="2.0" targetNamespace="http://demo/IHelloWorld" xmlns:ns="http://demo/IHelloWorld">
         <xsd:annotation>
            <xsd:appinfo>
               <jaxb:schemaBindings>
                  <jaxb:package name="demo"/>
               </jaxb:schemaBindings>
            </xsd:appinfo>
         </xsd:annotation>
         <xsd:element name="sayHelloName">
            <xsd:complexType>
               <xsd:sequence>
                  <xsd:element minOccurs="0" name="string" nillable="true" type="xsd:string"/>
                  <xsd:element minOccurs="0" name="string1" nillable="true" type="xsd:string"/>
               </xsd:sequence>
            </xsd:complexType>
         </xsd:element>
         <xsd:element name="sayHelloNameResponse">
            <xsd:complexType>
               <xsd:sequence>
                  <xsd:element minOccurs="0" name="return" nillable="true" type="xsd:string"/>
               </xsd:sequence>
            </xsd:complexType>
         </xsd:element>
      </xsd:schema>
   </wsdl:types>
   <wsdl:message name="sayHelloNameRequest">
      <wsdl:part element="ns0:sayHelloName" name="parameters"/>
   </wsdl:message>
   <wsdl:message name="sayHelloNameResponse">
      <wsdl:part element="ns0:sayHelloNameResponse" name="parameters"/>
   </wsdl:message>
   <wsdl:portType name="IHelloWorldPortType">
      <wsdl:operation name="sayHelloName">
         <wsdl:input message="ns0:sayHelloNameRequest" wsaw:Action="urn:sayHelloName"/>
         <wsdl:output message="ns0:sayHelloNameResponse" wsaw:Action="urn:sayHelloNameResponse"/>
      </wsdl:operation>
   </wsdl:portType>
   <wsdl:binding name="IHelloWorldSOAP11Binding" type="ns0:IHelloWorldPortType">
      <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
      <wsdl:operation name="sayHelloName">
         <soap:operation soapAction="urn:sayHelloName" style="document"/>
         <wsdl:input>
            <soap:body use="literal"/>
         </wsdl:input>
         <wsdl:output>
            <soap:body use="literal"/>
         </wsdl:output>
      </wsdl:operation>
   </wsdl:binding>
   <wsdl:service name="IHelloWorld">
      <wsdl:port binding="ns0:IHelloWorldSOAP11Binding" name="IHelloWorldSOAP11port_http">
         <soap:address location="http://127.0.0.1/uapws/service/demo.IHelloWorld"/>
      </wsdl:port>
   </wsdl:service>
   <jaxws:bindings>
      <jaxws:package name="demo"/>
   </jaxws:bindings>
</wsdl:definitions>

Q.E.D.


我并不是什么都知道,我只是知道我所知道的。