首先,创建一个WebService。html
package cn.xzj.ws; import javax.jws.WebService; import javax.xml.ws.Endpoint; //使用jdk1.6.0_24以上版本 //一、添加注解 @WebService public class HelloWorld { //二、至少包含一个能够对外公开的服务 public String a(String name){ return "屌丝"+name; } //三、第一个参数称为Binding即绑定地址, //第二个参数是实现者,即谁提供服务 public static void main(String[] args) { HelloWorld h = new HelloWorld(); Endpoint.publish("http://localhost:8014/hello", h); } }
直接运行,jdk1.6会有信息,1.7为空,不报错就没有问题,表示启动WebService成功!java
在浏览器访问http://localhost:8014/hello?wsdl,得到WebService说明书,看到以下shell
<?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.1.6 in JDK 6. --> - <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> - <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.xzj.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.xzj.cn/" name="HelloWorldService">- <types>- <xsd:schema> <xsd:import namespace="http://ws.xzj.cn/" schemaLocation="http://localhost:8014/hello?xsd=1" /> </xsd:schema> </types>- <message name="a"> <part name="parameters" element="tns:a" /> </message>- <message name="aResponse"> <part name="parameters" element="tns:aResponse" /> </message>- <portType name="HelloWorld">- <operation name="a"> <input message="tns:a" /> <output message="tns:aResponse" /> </operation> </portType>- <binding name="HelloWorldPortBinding" type="tns:HelloWorld"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> - <operation name="a"> <soap:operation soapAction="" /> - <input> <soap:body use="literal" /> </input>- <output> <soap:body use="literal" /> </output> </operation> </binding>- <service name="HelloWorldService">- <port name="HelloWorldPort" binding="tns:HelloWorldPortBinding"> <soap:address location="http://localhost:8014/hello" /> </port> </service> </definitions>
在Jdk的当前版本下,经过wsimport这个工具来生成远程调用的源代码浏览器
D:\>wsimport -s . http://localhost:8014/hello?wsdl parsing WSDL... generating code... compiling code... D:\>
在D盘就能够看到cn/xzj/ws生成的class文件和java文件,拷贝java文件到项目中。工具
package cn.xzj.ws; public class Test { public static void main(String[] args) { ////建立一个客户端服务对象 HelloWorld h = new HelloWorldService().getHelloWorldPort(); //调用服务方法,并打印方法返回值 System.out.println(h.a("大白")); } }
输出:屌丝大白。spa
注意: .net
给类添加上@WebService注解后,类中全部的非静态方法都将对外公布。code
不支持静态方法,final方法。xml
若是但愿某个方法(非static,非final)不对外公开,能够在方法上添加htm
@WebMenthod(exclude=true),防止对外公开。
若是一个类上,被添加了@WebService注解,则此类必须至少有一个可公开的方法,不然将会启动失败。