`
mengyang
  • 浏览: 263794 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

使用Xfire和Spring配置web service

    博客分类:
  • SOA
阅读更多

 

 

一.环境

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());
		}
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics