WebService之XFire+Spring集成(使用注解)

        本文只是介绍XFire+Spring使用注解的集成,版本为Xfire 1.2.6+Spring 3.1.1,测试环境为Tomcat6.0。Xfire 1.2.6下载

        1.首先建一个Web工程,引入相应的jar包,Xfire开发最精简jar包下载

服务器端:

commons-logging-1.1.1.jar
jdom-1.0.jar
org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.web.servlet-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
wsdl4j-1.6.1.jar
xfire-all-1.2.6.jar

客户端:

com.springsource.org.junit-4.7.0.jar
commons-codec-1.3.jar
commons-httpclient-3.0.jar
commons-logging-1.1.1.jar
jdom-1.0.jar
wsdl4j-1.6.1.jar
xfire-all-1.2.6.jar
XmlSchema-1.1.jar

        2.修改web.xml,加入以下代码:

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>xfireServlet</servlet-name>
		<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>xfireServlet</servlet-name>
		<url-pattern>/service/*</url-pattern>
	</servlet-mapping>
        3.在classpath下加入Spring配置文件applicationContext.xml,加入以下代码:

	<context:component-scan base-package="my.webservice" />
	
	<!-- XFire start -->
	<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
	<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>
	<!-- XFire end -->
        4.定义WebService接口,添加相应注解:

@WebService
public interface IBookService {

	@WebMethod
	public Book getBook();
}
        5.接口实现类,注解中serviceName定义发布的服务名,endpointInterface定义实现的接口:

@Component
@WebService(serviceName="BookService",
		endpointInterface = "my.webservice.IBookService")
public class BookServiceImpl implements IBookService {

	@Override
	public Book getBook() {
		
		Book b = new Book(1, "Java核心思想", 100);
		
		System.out.println(">>>>>>Server: " + b);
		
		return b;
	}
}

        6.以上便是服务端的配置及实现,是不是很简单。把工程发布到Tomcat并启动,在浏览器中输入:http://127.0.0.1:8080/XFireTest/service/BookService?wsdl(BookService为上面serviceName定义的名称),如果浏览器中显示BookService相关xml信息,则表示WebService发布成功。

        7.客户端调用服务端方法:

        (1)使用接口调用,此方法需要发布服务者提供接口,或我们自己通过wsdl生成接口。自己生成接口方法:

wsimport -keep -p my.client http://127.0.0.1:8080/XFireTest/service/BookService?wsdl
-keep 指示保留生成的文件,-p 指定需要在其中生成构件的包名称。http://127.0.0.1:8080/XFireTest/service/BookService?wsdl 是WSDL文件的位置。
wsimport在JAVA_HOME/bin目录下,应该已加到path环境变量中,可直接在cmd中运行以上命令,注意先进入要生成类的目录(如src)再执行。

	@Test
	public void testBookService() {
		Service serviceModel = new ObjectServiceFactory().create(IBookService.class);
		String url = "http://127.0.0.1:8080/XFireTest/service/BookService";
		IBookService service = null;
		
		try {
			service = (IBookService) new XFireProxyFactory().create(serviceModel, url);
			Book b = service.getBook();
			System.out.println(">>>>>>>>Client: " + b);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

        (2)通过wsdl调用,该方法如果返回值是String可正常使用,如果像本列中返回Book类型,则打印值为[#document: null],返回类型是org.apache.xerces.dom.DocumentImpl,需手动解析。我在项目中使用时,发布服务的时候把数据用xml形式返回,客户端用dom4j解析。

	@Test
	public void wsdlTest() throws MalformedURLException, Exception  {
		Client client = new Client(new URL("http://127.0.0.1:8080/XFireTest/service/BookService?wsdl"));
		Object[] results = client.invoke("getBook", new Object[] {});
		System.out.println(results[0]); 
	}


已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页