服务端结构:
package service;
import javax.jws.WebMethod;
@javax.jws.WebService
public interface WebService {
@WebMethod
String sayHello(String name);
}
——————————————————————————————————————
package service;
@javax.jws.WebService
public class WebServiceImpl implements WebService{
@Override
public String sayHello(String name) {
// TODO Auto-generated method stub
System.out.println("呵呵哒");
String aaa = name + "。。。呵呵哒";
return aaa;
}
}
————————————————————————————————————————
package service;
import javax.xml.ws.Endpoint;
public class WebServicePublish {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/webs", new WebServiceImpl());
System.out.println("呵呵哒,发布啦。");
}
}
WebServicePublish 启动访问成功,输出"呵呵哒,发布啦。"
生产客户端代码
win+r - cmd命令
输入:
wsimport -keep -d D:\java\myeclipsepakeape\jcdemoc\src\service(新建客户端的src路径)http://localhost:8080/jcdemo/Webs?wsdl(被访问的接口)
服务端结构:
代码生成以后创建测试类Wsclient.java
package service;
public class Wsclient {
public static void main(String[] args) {
try {
WebServiceImplService factory = new WebServiceImplService();
WebServiceImpl wsImpl = factory.getWebServiceImplPort();
String resResult = wsImpl.sayHello("陈意涵");
System.out.println("调用WebService的sayHello方法返回的结果是:"+resResult);
} catch (Exception e) {
// TODO: handle exception
System.out.println("服务器未启动");
}
}