发布Web服务和调用Web服务

1.jdk版本至少要的1.6.0_.04

2.jdk的环境变量已经配置好的

  1.JAVA_HOEM    JDK文件夹的地址      例: E:javajdk
  2.PATH    jdk文件夹下bin目录的地址;jdk文件夹下lib目录的地址    例:E:javajdkin;E:javajdklib  或  %JAVA_HOEM%in;%JAVA_HOEM%lib
  3.CLASSPATH   .;+jdk文件夹下lib目录的地址;jdk文件夹下jre文件夹lib目录的地址   例:  .;E:javajdklib;E:javajdkjrelib  或  .;%JAVA_HOEM%lib;%JAVA_HOEM%jrelib

3.发布web服务示例如下:

  1.定义接口,使用@WebService()注解作为一个web服务

 1 package cn.bd.weather.service;
 2 
 3 import java.util.Date;
 4 import java.util.List;
 5 
 6 import javax.jws.WebMethod;
 7 import javax.jws.WebParam;
 8 import javax.jws.WebResult;
 9 import javax.jws.WebService;
10 
11 import cn.bd.weather.entity.Weather;
12 
13 @WebService()
14 public interface WeatherService {
15     @WebMethod
16     List<Weather> getWeathers(String city,List<Date> dates);
17 }

  2.编写web服务接口的实现 类

 1 package cn.bd.weather.service.impl;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Date;
 5 
 6 import java.util.List;
 7 
 8 import javax.jws.WebMethod;
 9 import javax.jws.WebService;
10 
11 import cn.bd.weather.entity.Weather;
12 import cn.bd.weather.service.WeatherService;
13 
14 @WebService
15 public class WeatherServiceImpl implements WeatherService {
16     
17     @WebMethod
18     public List<Weather> getWeathers(String city, List<Date> dates) {
19         List<Weather> list=new ArrayList<Weather>();
20         for(Date date : dates){
21             list.add(getWeather(city,date));
22         }
23         return list;
24     }
25     
26     @WebMethod
27     public Weather getWeather(String city,Date date){
28         return new Weather(city,date,25,27,30,"晴");
29     }
30 
31 }

  3.发布web服务的测试

 1 package cn.bd.weather.test;
 2 
 3 import javax.xml.ws.Endpoint;
 4 
 5 import cn.bd.weather.service.impl.WeatherServiceImpl;
 6 
 7 public class Service {
 8     
 9     protected Service(){
10         System.out.println("启动");
11         WeatherServiceImpl weatherServiceImpl=new WeatherServiceImpl();
12         String address="http://localhost:10086/weatherservice";//对外发布的接口
13         Endpoint.publish(address, weatherServiceImpl);//接口发布
14     }
15     
16     public static void main(String[] args) throws Exception {
17         new Service();
18         System.out.println("服务准备.........");
19         Thread.sleep(5*60*1000);
20         System.out.println("服务关闭.........");
21         System.exit(0);//关闭服务 
22     }
23 }

运行程序,在浏览器输入:http://localhost:10086/weatherservice?wsdl,出现xml文件表示发布成功.

调用web服务

1.在控制台输入 wsimport -s data http://localhost:10086/weatherservice?wsdl,其中data是已经建好的文件夹,data后面的发布的地址

2.将会在data文件夹生成java类,如图所示

3.将Weather.java和WeatherService.java复制到程序

4.编写客户端代码

 1 package cn.bd.weather.client;
 2 import java.net.MalformedURLException;
 3 import java.net.URL;
 4 import java.text.DateFormat;
 5 import java.text.ParseException;
 6 import java.text.SimpleDateFormat;
 7 import java.util.ArrayList;
 8 import java.util.Date;
 9 import java.util.List;
10 import javax.xml.namespace.QName;
11 import javax.xml.ws.Service;
12 
13 public class Client {
14         //指定服务名
15         private static final QName SERVICE_NAME = new QName(
16                 "http://impl.service.weather.bd.cn/", "WeatherServiceImplService");
17         //指定端口名
18         private static final QName PORT_NAME = new QName(
19                 "http://impl.service.weather.bd.cn/", "WeatherServiceImplPort");
20         //指定位置
21         private static final String  WSDL_LOCATION = "http://localhost:10086/weatherservice?wsdl";
22         
23         public static void main(String[] args) throws ParseException, MalformedURLException {
24             //根据服务器地址创建URL对象
25             URL wsdlUrl=new URL(WSDL_LOCATION);
26             //根据URL对象和服务器名称创建Service类
27             Service service=Service.create(wsdlUrl, SERVICE_NAME);
28             //获取服务实现类
29             WeatherServiceImpl port=service.getPort(PORT_NAME, WeatherServiceImpl.class);
30             List<Date> dates=new ArrayList<Date>();
31             DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
32             Date date1=format.parse("2016-12-1");
33             Date date2=format.parse("2016-12-2");
34             Date date3=format.parse("2016-12-3");
35             dates.add(date1);
36             dates.add(date2);
37             dates.add(date3);
38             //调用服务
39             List<Weather> obj=port.getWeathers("beijing", dates);
40             for (Weather weather : obj) {
41                 System.out.println(weather.getCity()+"	"
42                         +format.format(weather.getDate())+"	"
43                         +weather.getMax()+"	"
44                         +weather.getMin()+"	"
45                         +weather.getAverage()+"	"
46                         +weather.getDesc());
47                 System.out.println("
");
48             }
49         }
50     }
原文地址:https://www.cnblogs.com/taobd/p/6680033.html