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