使用Xfire和Spring配置web service

 

 

一.環境

Jdk1.5Eclipse3.2MyEclipse5.5Xfire1.2.6Spring1.2

二.服務端

使用Xfire配合spring把一個pojo發佈成web服務有很多種方法,這裏採用的是配置最簡單的JSR181註解。

1.       建立web工程,工程名爲wsserver

2.       spring的核心庫(Spring1.2 Core Libraries)導入工程

3.       xfire包導入工程,在Eclipse裏右擊工程,MyEclipse->Add Web Service Cabalities->servlet class裏選擇org.codehaus.xfire.spring.XfireSpringServlet->servlet mapping裏把名字改爲/webservices/*(避免和OpenSessionInViewFilter衝突)->finish,在正確配置這兩步後,在web.xml做如下配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/applicationContext.xml
		</param-value>
	</context-param>
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<listener>
		<listener-class>
			org.springframework.web.util.IntrospectorCleanupListener
		</listener-class>
	</listener>

	<!-- begin XFire 配置 -->
	<servlet>
		<!-- 配合Spring容器中XFire一起工作的Servlet-->
		<servlet-name>xfireServlet</servlet-name>
		<servlet-class>
			org.codehaus.xfire.spring.XFireSpringServlet
		</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>xfireServlet</servlet-name>
		<!-- 在這個URI下開放Web Service服務 -->
		<url-pattern>/webservices/*</url-pattern>
	</servlet-mapping>
	<!-- end XFire 配置 -->
</web-app>

 

 

 

 4.    編寫接口IHelloWS,並在類上做@WebService標註

  

package test;

import java.util.List;

import javax.jws.WebService;

@WebService
public interface IHelloWS {
	
	public void sayHello();
	
	public String whatSay();
	
	public void sayHello(Foo foo);
	
	public List<Foo> says(List<Boo> list);
	
}

 

以上需要注意的是web服務中的方法的輸入/出參數中若有使用到集合對象,則要使用泛型。否則需要做aegis配置。

 

5.       編寫IHelloWS的實現類HelloWSImpl,並在類上做@WebService(serviceName="helloUT",endpointInterface="test.IHelloWS")標註,其中serviceName是這這個服務的名稱,默認爲接口實現類的名稱,endpointInterface是該類實現的接口的類全名。

 

package test;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

@WebService(serviceName="helloUT",endpointInterface="test.IHelloWS")
public class HelloWSImpl implements IHelloWS {
	
	public void sayHello() {
		System.out.println("hello ws!");
	}

	public String whatSay() {
		return "hello ws!";
	}

	public void sayHello(Foo foo) {
		System.out.println("hello "+ foo.getName());
	}

	public List<Foo> says(List<Boo> list) {
		List<Foo> lists = new ArrayList<Foo>(); 
		for (int i = 0;i<list.size();i++) {
			Foo foo = new Foo();
			foo.setName(list.get(i).getFirstName()+list.get(i).getLastName());
			lists.add(foo);
		}
		return lists;
	}

}

 

 

6.    以上接口和實現類用到的兩個類FooBoo如下

 

package test;

public class Foo {

	public String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}


package test;

public class Boo {
	public String firstName;

	public String lastName;

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

}

 

7.    ApplicationContext中做如下配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	
	<!-- 要發佈成web服務的pojo -->
	<bean id="serviceBean" class="test.HelloWSImpl"/>
	
	<!-- 引入XFire預配置信息 -->
	<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
	
	<!-- 獲得applicationContext中所有bean的JSR181 annotation -->
	<bean id="webAnnotations"
		class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" />
	<bean id="jsr181HandlerMapping"
		class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">
		<property name="xfire" ref="xfire" />
		<property name="webAnnotations" ref="webAnnotations" />
	</bean> 
</beans>

  

8.    啓動服務,在瀏覽器地址欄中輸入http://localhost:8090/wsserver/webservice/helloUT?wsdl,注意這裏的helloUTweb服務的名字,它是配在接口實現類的@WebService的標註的serviceName成員中的,若沒有配置值則默認爲接口實現類的名稱(這裏還有點要注意:若不使用JSR181的方式發佈web服務則默認的名字是接口的名稱)。若能看到正確的wsdl文件則表明服務端已大功告成了。

 

三.客戶端

客戶端調用web服務也有很多方法,這裏採用的是獲取服務端接口類再配合wsdl地址來訪問威web服務。並且把所有的web服務以bean的形式配置在spring容器中,在實際的應用中就可以使用注入的方式來獲取web服務。

1.       建立web工程,工程名爲wsclient

2.       拷貝服務端的接口類和參數類(IHelloWSFooBoo)到客戶端,這裏需要注意的是它們的包結構必須和服務端保持一致!

3.       spring的核心庫(Spring1.2 Core Libraries)導入工程

4.       xfire包導入工程,在Eclipse裏右擊工程,MyEclipse->Add Web Service Cabalities->finish

5.       commons-httpclient.jar包導入工程

6.       ApplicationContext中配置服務bean

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

	<bean id="helloService"
		class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
		<!-- web服務接口類 -->
		<property name="serviceClass" value="test.IHelloWS" />
		<!-- web服務wsdl地址 -->
		<property name="wsdlDocumentUrl"
			value="http://localhost:8090/wsserver/webservice/helloUT?wsdl" />
	</bean>

</beans>

 

 

 

 

7.       寫個測試類HelloWSTest

package test;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWSTest {

	public static void main(String[] args) {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		IHelloWS service = (IHelloWS)ctx.getBean("helloService");
		
		service.sayHello();
		System.out.println(service.whatSay());
		
		Foo foo = new Foo();
		foo.setName("zhouQ");
		service.sayHello(foo);
		
		List<Boo> list = new ArrayList<Boo>();
		Boo boo1 = new Boo();
		boo1.setFirstName("zhou");
		boo1.setLastName("Q");
		list.add(boo1);
		Boo boo2 = new Boo();
		boo2.setFirstName("YY");
		boo2.setLastName("ning");
		list.add(boo2);
		
		List<Foo> lists = service.says(list);
		for (int i = 0;i<lists.size();i++) {
			foo = lists.get(i);
			System.out.println(foo.getName());
		}
	}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章