使用Xfire和Spring配置web service
来源:互联网 发布:cos假毛淘宝店铺推荐 编辑:程序博客网 时间:2024/06/05 18:46
一.环境
Jdk1.5,Eclipse3.2,MyEclipse5.5,Xfire1.2.6,Spring1.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做如下配置
- <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>
<?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);
- }
package test;import java.util.List;import javax.jws.WebService;@WebServicepublic 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;
- }
- }
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. 以上接口和实现类用到的两个类Foo和Boo如下
- 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;
- }
- }
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>
<?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,注意这里的helloUT是web服务的名字,它是配在接口实现类的@WebService的标注的serviceName成员中的,若没有配置值则默认为接口实现类的名称(这里还有点要注意:若不使用JSR181的方式发布web服务则默认的名字是接口的名称)。若能看到正确的wsdl文件则表明服务端已大功告成了。
三.客户端
客户端调用web服务也有很多方法,这里采用的是获取服务端接口类再配合wsdl地址来访问威web服务。并且把所有的web服务以bean的形式配置在spring容器中,在实际的应用中就可以使用注入的方式来获取web服务。
1. 建立web工程,工程名为wsclient
2. 拷贝服务端的接口类和参数类(IHelloWS,Foo,Boo)到客户端,这里需要注意的是它们的包结构必须和服务端保持一致!
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>
<?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());
- }
- }
- }
- 使用Xfire和Spring配置web service
- 使用XFire+Spring构建Web Service
- 使用XFire+Spring构建Web Service
- 使用XFire+Spring构建Web Service
- 使用XFire+Spring构建Web Service
- 使用XFire+Spring构建Web Service
- 使用XFire+Spring构建Web Service
- 使用XFire+Spring构建Web Service
- 使用XFire+Spring构建Web Service
- 使用XFire+Spring构建Web Service
- Spring+XFire开发[web service]
- XFire+Spring发布Web Service
- 使用XFire+Spring构建Web Service(二)
- 使用XFire+Spring构建Web Service(二)
- 使用XFire+Spring构建Web Service(一)
- 使用XFire+Spring构建Web Service(二)
- 使用XFire+Spring+Struts构建Web Service及测试
- 使用XFire+Spring构建Web Service(二)
- HBase专题介绍 4
- <%=request.getContextPath()%>的作用
- PowerDesigner连接oracle数据库,oracle数据导入PowerDesigner
- jsm(二)
- 利息计算器(第九周上机任务3)。
- 使用Xfire和Spring配置web service
- 分段函数求值
- 0..
- HBase专题介绍 5
- 本月有几天
- 采用MVC设计模式实现一个简单程序:
- linux命令Tlist的安装
- 利息计算器
- HBase专题介绍6