Web Service CXF 两种开发的两种方式

本文介绍了WebService开发的两种常见方式:wsdl优先和代码优先。详细阐述了每种方式的步骤,包括环境搭建、编写服务、发布服务和客户端访问服务。同时,对比了两种方式在服务端接口改变时的处理差异,帮助开发者选择最适合的开发策略。
Web Service__CXF__两种开发的两种方式

步骤:
    1、环境搭建
    2、编写服务
    3、发布服务
    4、客户端访问服务 
   
=========================================================================================================
wsdl优先 方式
   
1、环境搭建:引入相应的jar包等
2、编写服务:编写服务接口和实现服务接口的类
Java代码 复制代码 收藏代码
  1. package com.yaha.ws; 
  2.  
  3. import javax.jws.WebService; 
  4.  
  5. @WebService 
  6. public interface WeatherForecast { 
  7.      
  8.     public String getWeatherTomorrow(String city); 
  9.      
  10.     public int gettTemperature(String city); 
  11.      
  12.     public boolean hasTomorrowWeather(); 
		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();
		}

-------------------------------------------------------------------------------------------
Java代码 复制代码 收藏代码
  1. package com.yaha.ws.impl; 
  2.  
  3. import javax.jws.WebParam; 
  4. import javax.jws.WebService; 
  5.  
  6. import com.yaha.ws.WeatherForecast; 
  7.  
  8. @WebService(endpointInterface="com.yaha.ws.WeatherForecast"
  9.             name="WeatherForecast"
  10. public class WeatherForecastImpl implements WeatherForecast { 
  11.  
  12.     public String getWeatherTomorrow(@WebParam(name="city") String city) { 
  13.         if("beijing".equalsIgnoreCase(city)) { 
  14.             return "snow"
  15.         }else if("shanghai".equalsIgnoreCase(city)) { 
  16.             return "sun"
  17.         }else if("nanchang".equalsIgnoreCase(city)) { 
  18.             return "wind soft and sun beautiful"
  19.         }else if("chongqing".equalsIgnoreCase(city)) { 
  20.             return "wind soft and sun beautiful too"
  21.         } 
  22.         return "sorry, no message for the city:" + city; 
  23.     } 
  24.  
  25.     public int gettTemperature(String city) { 
  26.         if("beijing".equalsIgnoreCase(city)) { 
  27.             return -3
  28.         }else if("shanghai".equalsIgnoreCase(city)) { 
  29.             return 17
  30.         }else if("nanchang".equalsIgnoreCase(city)) { 
  31.             return 15
  32.         }else if("chongqing".equalsIgnoreCase(city)) { 
  33.             return 15
  34.         } 
  35.         return -227
  36.     } 
  37.  
  38.     public boolean hasTomorrowWeather() { 
  39.          
  40.         return true
  41.     } 
  42.  
		
		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以在开始或结束时做一些处理


Java代码 复制代码 收藏代码
  1. package com.yaha.ws.service; 
  2.  
  3. import javax.xml.ws.Endpoint; 
  4.  
  5. import com.yaha.ws.WeatherForecast; 
  6. import com.yaha.ws.impl.WeatherForecastImpl; 
  7.  
  8. public class WSDeploy { 
  9.  
  10.     /**
  11.      * @param args
  12.      */ 
  13.     public static void main(String[] args) { 
  14.         System.out.println("service start....."); 
  15.         WeatherForecast weatherForecast = new WeatherForecastImpl(); 
  16.         String address = "http://localhost:9000/WeatherForecast"
  17.         Endpoint.publish(address, weatherForecast); 
  18.     } 
  19.  
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包

Java代码 复制代码 收藏代码
  1. package com.yaha.ws.client; 
  2.  
  3. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; 
  4. import org.junit.Test; 
  5.  
  6. import com.yaha.ws.WeatherForecast; 
  7.  
  8.  
  9. public class Test01 { 
  10.     public static WeatherForecast weatherForecast; 
  11.     static
  12.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); 
  13.         factory.setServiceClass(WeatherForecast.class); 
  14.         factory.setAddress("http://localhost:9000/WeatherForecast"); 
  15.         weatherForecast = (WeatherForecast)factory.create(); 
  16.     } 
  17.  
  18.     @Test 
  19.     public void getWeatherTormorrow() { 
  20.          
  21.         System.out.println(weatherForecast.getWeatherTomorrow("beijing")); 
  22.         System.out.println(weatherForecast.getWeatherTomorrow("shanghai")); 
  23.         System.out.println(weatherForecast.getWeatherTomorrow("nanchang")); 
  24.         System.out.println(weatherForecast.getWeatherTomorrow("chongqing")); 
  25.         System.out.println(weatherForecast.getWeatherTomorrow("huaihua")); 
  26.     } 
  27.      
  28.     @Test 
  29.     public void getTemperatrue() { 
  30.         System.out.println(weatherForecast.gettTemperature("beijing")); 
  31.         System.out.println(weatherForecast.gettTemperature("shanghai")); 
  32.         System.out.println(weatherForecast.gettTemperature("nanchang")); 
  33.         System.out.println(weatherForecast.gettTemperature("chongqing")); 
  34.         System.out.println(weatherForecast.gettTemperature("huaihua")); 
  35.     } 
  36.      
  37.     @Test 
  38.     public void hasWeatherTomorrow() { 
  39.         System.out.println(weatherForecast.hasTomorrowWeather()); 
  40.     } 
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、编写调用服务的客户端代码。如下:

Java代码 复制代码 收藏代码
  1. package com.yaha.ws.client; 
  2.  
  3.  
  4.     import org.junit.Test; 
  5.      
  6.     import clientstub.weatherforecast.WeatherForecast; 
  7.     import clientstub.weatherforecast.WeatherForecastImplService; 
  8.      
  9.     public class StubTest01 { 
  10.          
  11.         @Test 
  12.         public void test01() { 
  13.             WeatherForecast weatherForecast = new WeatherForecastImplService(). 
  14.                                                                                         getWeatherForecastPort(); 
  15.              
  16.             System.out.println(weatherForecast.hasTomorrowWeather()); 
  17.              
  18.             System.out.println(weatherForecast.getWeatherTomorrow("shanghai")); 
  19.             System.out.println(weatherForecast.getWeatherTomorrow("beijing")); 
  20.             System.out.println(weatherForecast.getWeatherTomorrow("nanchang")); 
  21.             System.out.println(weatherForecast.getWeatherTomorrow("chongqing")); 
  22.             System.out.println(weatherForecast.getWeatherTomorrow("huaihua")); 
  23.              
  24.             System.out.println(weatherForecast.gettTemperature("shanghai")); 
  25.             System.out.println(weatherForecast.gettTemperature("beijing")); 
  26.             System.out.println(weatherForecast.gettTemperature("nanchang")); 
  27.             System.out.println(weatherForecast.gettTemperature("chongqing")); 
  28.             System.out.println(weatherForecast.gettTemperature("huaihua")); 
  29.         } 
  30.      
  31.     } 
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优先(也有叫契约优先)的区别?在应用上面有什么不同?各的优缺点?
如:当服务端接口有改变时,我们就必须得重新生成新的客户端存根类
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值