论坛首页 Java企业应用论坛

Web Service__CXF__两种开发的两种方式

浏览 5660 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (16) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-12-08   最后修改:2012-01-17
SOA
Web Service__CXF__两种开发的两种方式

步骤:
    1、环境搭建
    2、编写服务
    3、发布服务
    4、客户端访问服务 
   
=========================================================================================================
wsdl优先 方式
   
1、环境搭建:引入相应的jar包等
2、编写服务:编写服务接口和实现服务接口的类
		package com.yaha.ws;
		
		import javax.jws.WebService;
		
		@WebService
		public interface WeatherForecast {
			
			public String getWeatherTomorrow(String city);
			
			public int gettTemperature(String city);
			
			public boolean hasTomorrowWeather();
		}

-------------------------------------------------------------------------------------------
		
		package com.yaha.ws.impl;
		
		import javax.jws.WebParam;
		import javax.jws.WebService;
		
		import com.yaha.ws.WeatherForecast;
		
		@WebService(endpointInterface="com.yaha.ws.WeatherForecast" ,
					name="WeatherForecast")
		public class WeatherForecastImpl implements WeatherForecast {
		
			public String getWeatherTomorrow(@WebParam(name="city") String city) {
				if("beijing".equalsIgnoreCase(city)) {
					return "snow";
				}else if("shanghai".equalsIgnoreCase(city)) {
					return "sun";
				}else if("nanchang".equalsIgnoreCase(city)) {
					return "wind soft and sun beautiful";
				}else if("chongqing".equalsIgnoreCase(city)) {
					return "wind soft and sun beautiful too";
				}
				return "sorry, no message for the city:" + city;
			}
		
			public int gettTemperature(String city) {
				if("beijing".equalsIgnoreCase(city)) {
					return -3;
				}else if("shanghai".equalsIgnoreCase(city)) {
					return 17;
				}else if("nanchang".equalsIgnoreCase(city)) {
					return 15;
				}else if("chongqing".equalsIgnoreCase(city)) {
					return 15;
				}
				return -227;
			}
		
			public boolean hasTomorrowWeather() {
				
				return true;
			}
		
		}
--------------------------------------------------------------------------------------------------

3、发布服务
还可以用其他方式发布服务,有些方式更为细腻,你可以添加一些interceptor以在开始或结束时做一些处理


package com.yaha.ws.service;

import javax.xml.ws.Endpoint;

import com.yaha.ws.WeatherForecast;
import com.yaha.ws.impl.WeatherForecastImpl;

public class WSDeploy {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("service start.....");
		WeatherForecast weatherForecast = new WeatherForecastImpl();
		String address = "http://localhost:9000/WeatherForecast";
		Endpoint.publish(address, weatherForecast);
	}

}

--------------------------------------------------------------------------------------------------

4、客户端访问服务
先把服务端的接口类打成jar包(此测试中,jar包只需包含WeatherForecast接口就行),然后在客户端的ClassPath引入该jar包

package com.yaha.ws.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.Test;

import com.yaha.ws.WeatherForecast;


public class Test01 {
	public static WeatherForecast weatherForecast;
	static {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(WeatherForecast.class);
		factory.setAddress("http://localhost:9000/WeatherForecast");
		weatherForecast = (WeatherForecast)factory.create();
	}

	@Test
	public void getWeatherTormorrow() {
		
		System.out.println(weatherForecast.getWeatherTomorrow("beijing"));
		System.out.println(weatherForecast.getWeatherTomorrow("shanghai"));
		System.out.println(weatherForecast.getWeatherTomorrow("nanchang"));
		System.out.println(weatherForecast.getWeatherTomorrow("chongqing"));
		System.out.println(weatherForecast.getWeatherTomorrow("huaihua"));
	}
	
	@Test
	public void getTemperatrue() {
		System.out.println(weatherForecast.gettTemperature("beijing"));
		System.out.println(weatherForecast.gettTemperature("shanghai"));
		System.out.println(weatherForecast.gettTemperature("nanchang"));
		System.out.println(weatherForecast.gettTemperature("chongqing"));
		System.out.println(weatherForecast.gettTemperature("huaihua"));
	}
	
	@Test
	public void hasWeatherTomorrow() {
		System.out.println(weatherForecast.hasTomorrowWeather());
	}
}

--------------------------------------------------------------------------------------------------

测试运行输出结果 output:
snow
sun
wind soft and sun beautiful
wind soft and sun beautiful too
sorry, no message for the city:huaihua
-3
17
15
15
-227
true

================================================================================================================

代码优先方式

    1、环境搭建(同wsdl优先)
    2、编写服务(同wsdl优先)
    3、发布服务(同wsdl优先)
   
----------------------------------------------------------------------------------------------------------------
4、客户端访问服务

4.1、用cxf里面的wsdl2java工具把wsdl文件生成相应的java存根(stub)类

工具使用命令:
wsdl2java -p clientstub.weatherforecast -d weatherforecast -client http://localhost:9000/WeatherForecast?wsdl

打开weatherforecast文件夹,可以看到生成的如下java文件:

GettTemperature.java
GettTemperatureResponse.java
GetWeatherTomorrow.java
GetWeatherTomorrowResponse.java
HasTomorrowWeather.java
HasTomorrowWeatherResponse.java
ObjectFactory.java
package-info.java
WeatherForecast_WeatherForecastPort_Client.java
WeatherForecast.java
WeatherForecastImplService.java

4.2、把生存的java存根类复制到项目里面

4.3、编写调用服务的客户端代码。如下:

package com.yaha.ws.client;


	import org.junit.Test;
	
	import clientstub.weatherforecast.WeatherForecast;
	import clientstub.weatherforecast.WeatherForecastImplService;
	
	public class StubTest01 {
		
		@Test
		public void test01() {
			WeatherForecast weatherForecast = new WeatherForecastImplService().
																						getWeatherForecastPort();
			
			System.out.println(weatherForecast.hasTomorrowWeather());
			
			System.out.println(weatherForecast.getWeatherTomorrow("shanghai"));
			System.out.println(weatherForecast.getWeatherTomorrow("beijing"));
			System.out.println(weatherForecast.getWeatherTomorrow("nanchang"));
			System.out.println(weatherForecast.getWeatherTomorrow("chongqing"));
			System.out.println(weatherForecast.getWeatherTomorrow("huaihua"));
			
			System.out.println(weatherForecast.gettTemperature("shanghai"));
			System.out.println(weatherForecast.gettTemperature("beijing"));
			System.out.println(weatherForecast.gettTemperature("nanchang"));
			System.out.println(weatherForecast.gettTemperature("chongqing"));
			System.out.println(weatherForecast.gettTemperature("huaihua"));
		}
	
	}


---------------------------------------------------------------------------------------------------------
输出结果:
true
sun
snow
wind soft and sun beautiful
wind soft and sun beautiful too
sorry, no message for the city:huaihua
17
-3
15
15
-227



==============================================================================================================
后话:
了解代码优先和wsdl优先(也有叫契约优先)的区别?在应用上面有什么不同?各的优缺点?
如:当服务端接口有改变时,我们就必须得重新生成新的客户端存根类。
[size=large][/size]
   发表时间:2012-01-12  
请问 代码优先和wsdl优先(也有叫契约优先)这是专业术语吗?在实际项目中用哪一种?
0 请登录后投票
   发表时间:2012-01-17  
whoshaofeng 写道
请问 代码优先和wsdl优先(也有叫契约优先)这是专业术语吗?在实际项目中用哪一种?




两种方法各有优缺点,具体的你可参考网上资料。
我公司项目用的是代码优先,就是用wdsl2java工具把wsdl生成相应的java类(存根),然后调用这些存根接口来对服务进行调用。 不过这样的话,当调用的远程服务接口改变时,我们就要用新的wsdl重新生存新的存根。
0 请登录后投票
   发表时间:2012-05-18  
好像讲的不对吧,代码优先还是契约优先是指服务端开发而言。
0 请登录后投票
   发表时间:2012-05-22  
jngxx 写道
好像讲的不对吧,代码优先还是契约优先是指服务端开发而言。


不是你说的这样子  你可以查资料验证
0 请登录后投票
   发表时间:2012-05-22   最后修改:2012-05-22
LZ的理解似乎是有偏差

wsdl优先是指手工编写wsdl文件,然后生成服务端的接口,在后去补实现

代码优先指的是手写服务端的接口及实现,然后生成wsdl,如JAX-WS的wsgen

---------------
对客户端来说就不存在什么代码优先了

肯定得先有wsdl再有客户端,无论是wsdl2java,wsimport,xjc还是手工编写客户端
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics