服务端:
1.创建正常j2ee项目,创建service接口
1 package service; 2 import javax.jws.WebMethod; 3 import javax.jws.WebService; 4
5 /**
6 * WebService2 7 */
8 @WebService 9 public interface HelloService { 10
11 @WebMethod 12 String sayHello(String name, int i); 13
14 }
实现类:
1 package service.impl; 2
3 import javax.jws.WebService; 4
5 import service.HelloService; 6
7 /**
8 * HelloServiceImpl 9 */
10 @WebService 11 public class HelloServiceImpl implements HelloService { 12
13 @Override 14 public String sayHello(String name, int i) { 15 System.out.println("服务端的服务被调用了............"); 16 return "hello" + name; 17 } 18
19
20
21 }
发布服务:
1 package service; 2
3 import javax.xml.ws.Endpoint; 4
5 import service.impl.HelloServiceImpl; 6
7
8 /**
9 * ServicePublish 10 */
11 public class ServicePublish { 12
13 public static void main(String[] args) { 14 String address = "http://localhost:8099/hello"; 15 Object implementor = new HelloServiceImpl(); 16 Endpoint.publish(address, implementor); 17 System.out.println("发布成功"); 18 } 19 }
直接运行main方法启动服务端,然后再浏览器访问: http://localhost:8099/hello?wsdl
看到如下,说明发布成功
客户端:
1.用wsimport 工具解析wsdl生成代码
wsimport -s "生成代码的物理路径" http://localhost:8099/hello?wsdl
2.,生成的代码如图(服务端类的名称不一样,生成结果也不一样)
3.创建一个测试类调用测试
1 package client; 2
3 /**
4 * 1.通过wsimport解析wsdl创建代码 5 * 2.通过本地代码创建代理 6 * 3.通过代理对象调用方法 7 * WsClient 8 */
9 public class WsClient { 10 public static void main(String[] args) { 11 HelloServiceImplService ss = new HelloServiceImplService(); 12 HelloServiceImpl prosy = ss.getHelloServiceImplPort(); 13 String res = prosy.sayHello("佩佩", 33); 14 System.out.println(res); 15 } 16
17 }
备注:
以上环境是基于jdk1.8