建立一個java project。名稱随意。我的工程名稱為:webservice1
第一步:拷貝cxf提供的jar包.
第二步:編寫webservice服務端的接口和實作類
接口
@WebService
public interface WeatherService {
public String getWeatherByCityName(String cityName);
}
實作類
public class WeatherServiceImpl implements WeatherService {
@Override
public String getWeatherByCityName(String cityName) {
// TODO Auto-generated method stub
if("北京".equals(cityName))
return "萬裡無雲";
else if("上海".equals(cityName))
return "小雨";
else if("信陽".equals(cityName))
return "陰天";
return "未知";
}
}
第三步:編寫測試類(此處不能使用Junit,必須用main函數)
public class PublisherWeatherService {
public static void main(String[] args) {
// TODO Auto-generated method stub
JaxWsServerFactoryBean factory=new JaxWsServerFactoryBean();
factory.setAddress("http://localhost:12345/weather");
factory.setServiceBean(new WeatherServiceImpl());
factory.create();
}
}
第四步:在浏覽器中運作
http://localhost:12345/weather?wsdl