码迷,mamicode.com
首页 > Web开发 > 详细

webservice - 使用JAX-WS注解的方式快速搭建服务端和客户端

时间:2016-06-08 20:24:00      阅读:629      评论:0      收藏:0      [点我收藏+]

标签:

1.Define the interface

 1 import javax.jws.WebMethod;
 2 import javax.jws.WebParam;
 3 import javax.jws.WebResult;
 4 import javax.jws.WebService;
 5 import javax.jws.soap.SOAPBinding;
 6 import javax.jws.soap.SOAPBinding.Style;
 7 
 8 @WebService
 9 
10 @SOAPBinding(style = Style.RPC)
11 public interface ISay {
12     @WebMethod(operationName="HansayHello")
13     @WebResult(name="myReturn")
14     public String sayHello(@WebParam(name="name") String name);
15     
16     @WebMethod(operationName="HansayGoodBye")
17     @WebResult(name="myReturn")
18     public String sayGoodbye(@WebParam(name="name")String name);
19     
20     @WebMethod(exclude=true)//当前方法不被发布出去
21     public String sayHello2(@WebParam(name="name")String name);
22 }

 

2.Implement the interface

 1 import javax.jws.WebService;
 2 
 3 
 4 /**
 5  * WebService
 6  * 将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service 接口
 7  */
 8 @WebService(endpointInterface = "com.mobile263.ISay")
 9 public class SayImpl implements ISay {
10     
11  
12     public String sayHello(String name){
13     System.out.println("sayHello got the request:"+name);    
14         return  "hello: " + name;
15     }
16     
17    
18     public String sayGoodbye(String name){
19     System.out.println("sayGoodBye got the request:"+name);
20         return  "goodbye: " + name;
21     }
22     
23    
24     public String sayHello2(String name){
25         return "hello " + name;
26     }
27 
28 
29 }

 

3. Publish the ws in server

 1 import javax.xml.ws.Endpoint;
 2 
 3 /**
 4  * WebService
 5  * 将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service 接口
 6  */
 7 
 8 public class Wsserver {
 9 
10     public static void main(String[] args) {
11         /**
12          * 参数1:服务的发布地址
13          * 参数2:服务的实现者
14          *  Endpoint  会重新启动一个线程
15          */
16         Endpoint.publish("http://localhost:8090/WStest/com.mobile.Wsserver", new SayImpl());
17         System.out.println("Server ready...");
18     }
19 
20 }

 

4. Input the link on brouswer and enter,the wsdl will be shown.   --> http://localhost:8090/WStest/com.mobile.Wsserver?wsdl

 

5. Use wsimport to export the client files

wsimport -d d: -keep -verbose http://localhost:8090/WStest/com.mobile.Wsserver?wsdl

 

 

 



webservice - 使用JAX-WS注解的方式快速搭建服务端和客户端

标签:

原文地址:http://www.cnblogs.com/stronghan/p/5571157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!