1.创建接口和实现类 (模拟实现查询天气)
接口:
package com.learning.weather; /** * * weather 接口 :实现模拟wsdl */ public interface WeatherInterface { /** * 查询天气 * @param name * @return */ public String queryWeather(String name); }
实现类:
package com.learning.weather; import javax.jws.WebService; @WebService public class WeatherInterfaceImpl implements WeatherInterface { @Override public String queryWeather(String name) { System.out.println("查询天气"+ name); return "nihao "; } }
main方法测试,开启服务:
package com.learning.weather; import javax.xml.ws.Endpoint; public class TestMain { public static void main(String[] args) { String address="Http://127.0.0.1:1234/weather"; Object implementor=new WeatherInterfaceImpl(); Endpoint.publish(address, implementor); } }
访问 Http://127.0.0.1:1234/weather,点击wsdl
出现以下页面
在src目录下 执行wsimport命令 : wsimport -p com.learning.client -s . http://127.0.0.1:1234/weather?wsdl
src下刷新出现新的包com.learning.client
写客户端程序
package com.learning.client; public class TestMain { public static void main(String[] args) { //1.创建服务视图 WeatherInterfaceImplService weatherInterfaceImplService =new WeatherInterfaceImplService(); //2.得到实现类 WeatherInterfaceImpl weatherInterfaceImpl = weatherInterfaceImplService.getPort(WeatherInterfaceImpl.class); //发送数据 String weather = weatherInterfaceImpl.queryWeather("湖北"); System.out.println(weather); } }
运行测试即可