首先,建立一个WebService:
package garfield; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebService public class MyJ6WebService { public String SayHello(String strName) { return "Hello ,"+strName+"!"; } public static void main(String[] args) { //发布WebService,注意如果提示:Exception in thread "main" com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.BindException: Address already in use: bind //则需要修改一下发布端口 Endpoint.publish("http://localhost:8030/garfield.MyJ6WebService", new MyJ6WebService()); System.out.println("WebService was published success !"); } }
启动Tomcat,然后运行,系统会输出:WebService was published success !,表示WebService已启动。
可以在浏览器中输入地址:http://localhost:8030/garfield.MyJ6WebService?wsdl
显示WebService信息:
<?xml version="1.0" encoding="UTF-8" ?> - <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI‘s version is JAX-WS RI 2.2.4-b01. --> - <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI‘s version is JAX-WS RI 2.2.4-b01. --> - <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://garfield/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://garfield/" name="MyJ6WebServiceService"> - <types> - <xsd:schema> <xsd:import namespace="http://garfield/" schemaLocation="http://localhost:8030/garfield.MyJ6WebService?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="MyJ6WebService"> - <operation name="SayHello"> <input wsam:Action="http://garfield/MyJ6WebService/SayHelloRequest" message="tns:SayHello" /> <output wsam:Action="http://garfield/MyJ6WebService/SayHelloResponse" message="tns:SayHelloResponse" /> </operation> </portType> - <binding name="MyJ6WebServicePortBinding" type="tns:MyJ6WebService"> <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="MyJ6WebServiceService"> - <port name="MyJ6WebServicePort" binding="tns:MyJ6WebServicePortBinding"> <soap:address location="http://localhost:8030/garfield.MyJ6WebService" /> </port> </service> </definitions>
在WebService启动的状态下,在命令行中输入:
E:\Temp>wsimport -p garfield.garfieldj6wsclient -keep http://localhost:8030/garf ield.MyJ6WebService?wsdl parsing WSDL... generating code... compiling code... E:\Temp>
注意,一定要对应你发布的WebService信息!
然后系统会自动生成相关的接口文件,如下:
新建Java工程,将输出文件引入,然后建立测试类:
package xxh; import garfield.garfieldj6wsclient.*; public class WebClient { /** * @param args */ public static void main(String[] args) { //创建一个客户端服务对象 MyJ6WebService myJ6WS = new MyJ6WebServiceService().getMyJ6WebServicePort(); //调用服务方法,并得到方法返回值 String strTest = myJ6WS.sayHello("Garfield"); //打印服务的返回值 System.out.println(strTest); } }
在WebService运行情况下,运行客户端测试程序,输出:
Hello ,Garfield!
时间: 2024-10-13 23:30:40