首先认识两个缩写:
SEI (Service Endpoint Interface) 服务提供的接口
SIB (Service Implement Bean) 实现类
1 IMyService.java // SEI 2 3 package having.service; 4 5 import javax.jws.WebService; 6 7 @WebService 8 9 public interface IMyService { 10 11 public int add(int a , int b); 12 13 public int minus(int a , int b); 14 15 } 16 17 18 19 MyServiceImpl.java // SIB 20 21 package having.service; 22 23 import javax.jws.WebService; 24 25 @WebService(endpointInterface = "having.service.IMyService") 26 27 public class MyServiceImpl implements IMyService { 28 29 @Override 30 31 public int add(int a, int b) { 32 33 System.out.println(a + "+" + b + "=" + (a+b)); 34 35 return a+b; 36 37 } 38 39 @Override 40 41 public int minus(int a, int b) { 42 43 System.out.println(a + "-" + b + "=" + (a-b)); 44 45 return a-b; 46 47 } 48 49 } 50 51 52 53 MyServer.java //服务器端 54 55 package having.service; 56 57 import javax.xml.ws.Endpoint; 58 59 public class MyServer { 60 61 public static void main(String[] args) { 62 63 String address = "http://localhost:6666/ws"; 64 65 Endpoint.publish(address, new MyServiceImpl()); 66 67 } 68 69 } 70 71 72 73 TestClient //客户端 74 75 package having.service; 76 77 import java.net.MalformedURLException; 78 79 import java.net.URL; 80 81 import javax.xml.namespace.QName; 82 83 import javax.xml.ws.Service; 84 85 public class TestClient { 86 87 public static void main(String[] args) { 88 89 try { 90 91 //创建访问wsdl服务地址的url 92 93 URL url = new URL("http://localhost:6666/ws?wsdl"); 94 95 //通过QName指明服务的具体信息 96 97 QName qname = new QName("http://service.having/","MyServiceImplService"); 98 99 //创建服务 100 101 Service service = Service.create(url,qname); 102 103 //实现接口 104 105 IMyService ms = service.getPort(IMyService.class); 106 107 System.out.println(ms.add(12, 5)); 108 109 //以上服务依然有问题,原因是:还是依赖IMyService接口 110 111 } catch (MalformedURLException e) { 112 113 e.printStackTrace(); 114 115 } 116 117 } 118 119 }