JSR-181的元数据清单
下面介绍JSR-181里面各个元数据的相关参数及用途
Annotation Retention Target Description
WebService Runtime Type
标注要暴露为Web Services的类或接口
WebParam Runtime Parameter 自定义服务方法参数到WSDL的映射
WebResult Runtime Method 自定义服务方法返回值到WSDL的映射
WebMethod Runtime Method 自定义单个服务方法到WSDL的映射
Oneway Runtime Method 必须与@WebMethod连用,表明被标注方法只有输入没有输出,这就要求被标注方法不能有返回值,也不能声明checked exception
HandlerChain Runtime Type,Method,Field 将Web服务与外部Handler chain关联起来
SOAPBinding Runtime Type,Method 自定义SOAPBinding
JSR-181元数据使用示例
package WebServices;
import java.io.File;
import java.io.IOException;
import javax.jws.Oneway;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
/**
* @author chinajash
*/
@WebService(targetNamespace="http://blog.csdn.net/chinajash",serviceName="HelloService")
public class WSProvider {
@WebResult(name="Greetings")//自定义该方法返回值在WSDL中相关的描述
@WebMethod
public String sayHi(@WebParam(name="MyName") String name){
return "Hi,"+name; //@WebParam是自定义参数name在WSDL中相关的描述
}
@Oneway //表明该服务方法是单向的,既没有返回值,也不应该声明检查异常
@WebMethod(action="printSystemTime",operationName="printSystemTime")//自定义该方法在WSDL中相关的描述
public void printTime(){
System.out.println(System.currentTimeMillis());
}
public static void main(String[] args) {
Thread wsPublisher = new Thread(new WSPublisher());
wsPublisher.start();
}
private static class WSPublisher implements Runnable{
public void run() {
//发布WSProvider到http://localhost:8888/chinajash/WSProvider这个地址,之前必须调用wsgen命令
//生成服务类WSProvider的支持类,命令如下:
//wsgen -cp . WebServices.WSProvider
Endpoint.publish("http://localhost:8888/chinajash/WSProvider",new WSProvider());
}
}
}
如果想看到Web Services Engine生成的WSDL文件是否遵守上面的元数据, 我们没有必要将上面的WSProvider部署到支持JSR-181的应用服务器或Servlet形式的Web Services Engine,现在JDK6已经提供了一个很简单的机制可以用来测试和发布Web Services,下面讲讲如何在JDK6环境下发布Web Services和查看生成的WSDL
1.将/bin加入path环境变量
2.在命令行下切换当前目录到WSProvider的class文件所在的目录,运行下面命令
wsgen -cp . WebServices.WSProvider
在这个例子中会生成以下3个类的源代码文件及class文件
SayHi
SayHiResponse
PrintTime
3.执行如下代码发布WSProvider到http://localhost:8888/chinajash/WSProvider,在这里可以执行WSProvider类的main方法就可以
Endpoint.publish("http://localhost:8888/chinajash/WSProvider",new WSProvider());
4.在浏览器输入http://localhost:8888/chinajash/WSProvider?wsdl就可以看到生成的WSDL文件
/**.用wsimport为服务消费者(也就是服务的客户端)生成必要的帮助类,命令如下:
wsimport http://localhost:8888/chinajash/WSProvider?wsdl
这会在<当前目录>\net\csdn\blog\chinajash下生成客户端的帮助类,在这个例子中会生成7个类
HelloService.class
ObjectFactory.class
package-info.class
PrintSystemTime.class
SayHi.class
SayHiResponse.class
WSProvider.class
在客户端用下面代码即可调用步骤1定义的Web Service
HelloService hs = new HelloService();
WSProvider ws = hs.getWSProviderPort();
System.out.println(ws.sayHi("chinajash"));
ws.printSystemTime();
调用上述代码后客户端控制台输出
hi,chinajash
服务端控制台输出服务器当前系统时间
*/