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
时间: 2024-09-28 20:50:28