SEI即(Service Endpoint Interface)SEI在ws中稱爲portType,在java中稱爲接口java
package jaxws.server; /** * @className: HelloService * @description: jaxws服務端口 * @author: hanson * @version: V1.0 */ public interface HelloService { /** * 問候 * @param name 名稱 * @return 問候語 */ String sayHello(String name); }
package jaxws.server.impl; import jaxws.server.HelloService; import javax.jws.WebService; /** * @className: HelloServiceImpl * @description: SEI實現類 * @author: HanSon.Q * @version: V1.0 */ @WebService public class HelloServiceImpl implements HelloService { /** * 問候 * @param name 名稱 * @return 問候語 */ @Override public String sayHello(String name) { return "你好哇! " + name; } }
使用了一個類級別的註解@Webservice
,使用了這個註解的類、接口、枚舉、註解的全部方法都將會公開爲Web服務,若是想屏蔽SEI中的某個方法,可使用方法註解@WebMethod(exclude=true)
.web
package jaxws.server; import jaxws.server.impl.HelloServiceImpl; import jaxws.server.impl.HelloServiceSoap12Impl; import javax.xml.ws.Endpoint; /** * @className: JaxwsServerApp * @description: 主程序, 用於發佈服務 * @author: HanSon.Q * @date: 2018/2/6 11:33 * @version:V1.0 */ public class JaxwsServerApp { public static void main(String[] args) { HelloService helloService = new HelloServiceImpl(); Endpoint.publish("http://127.0.0.1:1234/hello", helloService); } }
點擊運行,沒有發生錯誤說明服務發佈成功shell
發佈程序啓動成功以後,經過瀏覽器訪問 http://127.0.0.1:1234/hello?wsdl
來驗證web服務的正確性。 經過WSDL能夠知道如何調用web服務!瀏覽器
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.server.jaxws/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.server.jaxws/" name="HelloServiceImplService"> <types> <xsd:schema> <xsd:import namespace="http://impl.server.jaxws/" schemaLocation="http://127.0.0.1:1234/hello?xsd=1"/> </xsd:schema> </types> <message name="sayHello"> <part name="parameters" element="tns:sayHello"/> </message> <message name="sayHelloResponse"> <part name="parameters" element="tns:sayHelloResponse"/> </message> <portType name="HelloServiceImpl"> <operation name="sayHello"> <input wsam:Action="http://impl.server.jaxws/HelloServiceImpl/sayHelloRequest" message="tns:sayHello"/> <output wsam:Action="http://impl.server.jaxws/HelloServiceImpl/sayHelloResponse" message="tns:sayHelloResponse"/> </operation> </portType> <binding name="HelloServiceImplPortBinding" type="tns:HelloServiceImpl"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <operation name="sayHello"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="HelloServiceImplService"> <port name="HelloServiceImplPort" binding="tns:HelloServiceImplPortBinding"> <soap:address location="http://127.0.0.1:1234/hello"/> </port> </service> </definitions>
wsimport是JDK自帶的ws客戶端工具,它能夠根據wsdl生成客戶端調用代碼(java),不用考慮服務端開發語言。app
wsimport.exe 在%JAVA_HOME%\bin目錄下面ide
首先在d盤下建立temp目錄並在此目錄下建立s文件夾和d文件夾分別用來存儲.java文件和.class文件。 而後使用下面的命令回車執行工具
E:\>wsimport -keep -d D:\temp\d -s D:\temp\s -p jaxws.client -verbose http://127.0.0.1:1234/hello?wsdl
package jaxws.client.app; import jaxws.client.HelloServiceImpl; import jaxws.client.HelloServiceImplService; /** * @className: JaxwsClientApp * @description: 客戶端 * @author: HanSon.Q * @version: V1.0 */ public class JaxwsClientApp { public static void main(String[] args) { //一、建立服務視圖 HelloServiceImplService helloServiceImplService = new HelloServiceImplService(); //二、經過服務視圖獲得服務端點(SEI) HelloServiceImpl helloServiceImplPort = helloServiceImplService.getPort(HelloServiceImpl.class); //三、調用服務方法 String result = helloServiceImplPort.sayHello("程序猿"); System.out.println(result); } }
運行結果url
你好哇! 程序猿
package jaxws.client.app; import jaxws.client.HelloServiceImpl; import javax.xml.namespace.QName; import javax.xml.ws.Service; import java.net.MalformedURLException; import java.net.URL; /** * @className: SimpleObjectAccessProtocolClientApp * @description: SOAP客戶端 * @author: HanSon.Q * @date: 2018/2/6 14:23 * @version: V1.0 */ public class SimpleObjectAccessProtocolClientApp { public static void main(String[] args) throws MalformedURLException { //一、定義url,參數爲wsdl地址 URL url = new URL("http://127.0.0.1:1234/hello?wsdl"); //二、定義qname,第一個參數是命名空間,第二個參數名稱是wsdl裏邊的服務名 QName qName = new QName("http://impl.server.jaxws/", "HelloServiceImplService"); //三、建立服務視圖 Service service = Service.create(url,qName); //四、經過服務視圖獲得服務端點 HelloServiceImpl helloService = service.getPort(HelloServiceImpl.class); //五、調用web服務 String result = helloService.sayHello("Python"); System.out.println(result); } }
運行結果spa
你好哇! Python