简单粗暴,直接上步骤
一、先建立一个web项目,名字叫MyService
名字为MyService
新建Java类
package com.webService; import javax.jws.WebService;//别倒错包哦 import javax.xml.ws.Endpoint;//别倒错包哦 @WebService//注解别忘了 public class ServiceTest { public String getMessage(String name) { return name+"你过来一下"; } public static void main(String[] args) { Endpoint.publish("http://localhost:8080/MyService/ServiceTest", new ServiceTest());//发布服务 System.out.println("ServiceTest已启动"); } }
运行main方法
说明服务已经启动
访问http://localhost:8080/MyService/ServiceTest?wsdl可以看到
说明发布成功了
二、生成客户端
再新建一个web项目,名字叫MyClient
在src下建立com.client包
win+R cmd打开windows命令窗口
输入
wsimport -s I:\\eclipse_jee\\workspaces\\MyClient\\src -p com.webClient -keep http://localhost:8080/MyService/ServiceTest?wsdl
wsimport -s "C:\\Users\\user\\AppData\\Local\\MyEclipse 2016 CI\\MyEclipse 2016 CI\\MyClient\\src" -p com.webClient -keep http://localhost:8080/MyService/ServiceTest?wsdl
wsimport -s "C:\\Users\\user\\AppData\\Local\\MyEclipse 2016 CI\\MyEclipse 2016 CI\\MyClient\\src" -p com.webClient -keep http://192.168.9.5:8080/MyService/ServiceTest?wsdl
wsimport -s "C:\\Users\\user\\AppData\\Local\\MyEclipse 2016 CI\\MyEclipse 2016 CI\\MyClient\\src" -p com.webClient -keep http://192.168.9.5:8080/MyService/ServiceTest?wsdl
有空格用“”整个包起来
就可以看到
I:\eclipse_jee\workspaces\MyClient\src 客户端项目所在目录
com.webClient 包名
http://localhost:8080/MyService/ServiceTest?wsdl wsdl地址
然后refresh MyClient项目,生成类出现了
在src下建立test包,再建一个测试类ClientTest,代码如下
package test; import com.webClient.ServiceTest; import com.webClient.ServiceTestService; public class ClientTest { public static void main(String[] args) { ServiceTest serviceTest = new ServiceTestService().getServiceTestPort();//初始化对象 String name = serviceTest.getMessage("那个谁");//调用服务端方法 System.out.println(name);//打印返回结果 } }
运行main方法
完美!!
注意事项:
1、jdk1.7及以上
2、cmd命令很容易填错