一.环境
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做如下配置
<?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. 以上接口和实现类用到的两个类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;
}
}
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,注意这里的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>
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步骤以及源代码.rar
使用XFire+Spring使用XFire+Spring构建Web Service(二).doc构建使用XFire+Spring构建Web Service(二).docWeb Service(二).doc
使用XFire+Spring构建Web Service步骤 java
1 支持一系列Web Service的新标准--JSR181、WSDL2.0 、JAXB2、WS-Security等; 2 使用Stax解释XML,性能有了质的提高。XFire采用Woodstox 作Stax实现; 3 容易上手,可以方便快速地从pojo发布服务; 4 支持...
实现功能与特点 基于 J2EE 平台的 Web Service 服务 开发方便,配置简单 设计接口 实现服务 配置暴露接口 XFire 将自动生成对应的wsdl 支持高级详细配置 与 Spring 无缝集成
这里面介绍了关于使用spring 集成了xfire的功能, 同时提供了部分源码, 关于web.xml中的配置 关于spring.xml的配置 关于spring.xfire.xml配置 关于项目框架的搭建 如有需要全部源码的读者可以联系我: QQ :...
本文档主要描述xfire整合spring发布web services 绝对的真实可靠,按步骤操作一定成功
XFire 是与Axis 2并列的新一代Web Service框架,通过提供简单的API支持Web Service各项标准协议,帮助你方便快速地...并且因为XFire为Spring提供的支持,使得我们可以很容易在Spring中使用XFire构建Web Service应用。
实现 xFire与 Spring 的集成
NULL 博文链接:https://1021443917-qq-com.iteye.com/blog/1235380