10_Jaxws使用自定义pojo发布服务

【简述】

查询三天的天气信息(天气概况、日期、温度),测试jaxws是否支持自定义pojo发布服务。

 

【开发过程】

服务端:

1.自定义pojo(天气概况、日期、温度)

2.开发SEI接口及实现类

3.发布服务

客户端:(采用service类标准方法调用webService)

1.根据wsdl说明书生成客户端调用代码。

2.将生成代码拷贝到工程中,编写客户端类。

 

【服务端工程截图】

 

【WeatherModel.java】

package com.Higgin.ws.pojo;
import java.util.Date;

public class WeatherModel {
    
    //天气概况
    private String detail;
    
    //日期
    private Date date;
    
    //最高温度
    private  int  temperature_max;
    
    //最低温度
    private int temperature_min;
    //忽略get/set方法...........
}

【WeatherInterface.java】

package com.Higgin.ws.service;

import java.util.List;
import com.Higgin.ws.pojo.WeatherModel;

public interface WeatherInterface {
    public List<WeatherModel> queryWeatheer(String cityName);
}

【WeatherInterfaceImpl.java】

package com.Higgin.ws.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.jws.WebService;

import com.Higgin.ws.pojo.WeatherModel;

@WebService
public class WeatherInterfaceImpl implements WeatherInterface{

    @Override
    public List<WeatherModel> queryWeatheer(String cityName) {
        
        //构造三天天气
        List<WeatherModel> list =new ArrayList<WeatherModel>();
        Calendar calendar=Calendar.getInstance();
        int day=calendar.get(Calendar.DATE);        
        
        WeatherModel weatherModel_1=new WeatherModel();
        weatherModel_1.setDetail("晴天");
        weatherModel_1.setDate(new Date());
        weatherModel_1.setTemperature_max(10);
        weatherModel_1.setTemperature_min(-10);
        
        WeatherModel weatherModel_2=new WeatherModel();
        weatherModel_2.setDetail("阴天");
        calendar.set(Calendar.DATE, day+1);
        weatherModel_2.setDate(calendar.getTime());
        weatherModel_2.setTemperature_max(6);
        weatherModel_2.setTemperature_min(-2);
        
        WeatherModel weatherModel_3=new WeatherModel();
        weatherModel_3.setDetail("晴天");
        calendar.set(Calendar.DATE, day+2);
        weatherModel_3.setDate(calendar.getTime());
        weatherModel_3.setTemperature_max(30);
        weatherModel_3.setTemperature_min(3);
        
        
        list.add(weatherModel_1);
        list.add(weatherModel_2);
        list.add(weatherModel_3);
        return list;
    }

}

【WeatherServer.java】

package com.Higgin.ws.service;

import javax.xml.ws.Endpoint;

public class WeatherServer {
    public static void main(String[] args) {
        Endpoint.publish("http://127.0.0.1:12345/weather", new WeatherInterfaceImpl());
    }
}

 

【客户端工程截图】

已经使用Wsimport生成了相应的客户端代码

【WeatherClient.java】

package com.higgin.ws.client;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.higgin.ws.service.WeatherInterfaceImpl;
import com.higgin.ws.service.WeatherModel;

public class WeatherClient {
    
    public static void main(String[] args) throws MalformedURLException {
        //使用说明书          
        URL wsdlDocumentLocation =new URL("http://127.0.0.1:12345/weather?wsdl");
        
        QName serviceName=new QName("http://service.ws.Higgin.com/", "WeatherInterfaceImplService");
        
        //创建服务视图
        Service service=Service.create(wsdlDocumentLocation,serviceName);
        
        //创建portType对象
        WeatherInterfaceImpl weatherInterfaceImpl=service.getPort(WeatherInterfaceImpl.class);
        
        //调用WeatherInterfaceImpl方法
        List<WeatherModel> list=weatherInterfaceImpl.queryWeatheer("杭州");
        
                
        
        for(WeatherModel weatherModel:list){
            System.out.println(weatherModel.getDetail());
            Date date=weatherModel.getDate().toGregorianCalendar().getTime();
            System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
            System.out.println(weatherModel.getTemperatureMax());
            System.out.println(weatherModel.getTemperatureMin());
        }
        
        
    }
        
}

【运行结果】

 

posted @ 2016-09-05 11:46  HigginCui  阅读(730)  评论(0编辑  收藏  举报