WebService之Spring中集成CXF

WebService的实现方式有好多种,对比AXIS,XFIRE而言,CXF的发布/调用显得更加简单方便,本文就一个DEMO介绍在Spring种集成CXF,实现WebService的发布/调用.

1.新建一个Web项目CXFDemo,导入如下包

CXF包:

cxf-2.1.jar

aopalliance-1.0.jar

commons-logging-1.1.jar

geronimo-activation_1.1_spec-1.0-M1.jar

geronimo-annotation_1.0_spec-1.1.jar

geronimo-javamail_1.4_spec-1.0-M1.jar

geronimo-servlet_2.5_spec-1.1-M1.jar

geronimo-ws-metadata_2.0_spec-1.1.1.jar

geronimo-stax-api_1.0_spec-1.0.1.jar

jaxb-api-2.1.jar

jaxb-impl-2.1.6.jar

jaxws-api-2.1.jar

neethi-2.0.jar

saaj-api-1.3.jar

saaj-impl-1.3.jar

wsdl4j-1.6.1.jar

wstx-asl-3.2.1.jar

XmlSchema-1.2.jar

xml-resolver-1.2.jar

Spring包:

spring-core-2.5.jar

spring-beans-2.5.jar

spring-context-2.5.jar

spring-web-2.5.jar

2.新建一接口类:HelloWorld

java代码如下:

package com.cxf;



import java.util.List;



import javax.jws.WebParam;

import javax.jws.WebService;



import com.cxf.req.Request;

import com.cxf.rsp.Response;



@WebService  

public interface HelloWorld {

	

    List<String> sayHi(@WebParam(name="aName") String aName, @WebParam(name="bName") String bName);

    

    Response authentcateUser(Request req);

    

}

创建其实现类:HelloWorldImpl

java代码如下:

package com.cxf.impl;



import java.util.ArrayList;

import java.util.List;



import javax.jws.WebService;



import com.cxf.HelloWorld;

import com.cxf.req.Request;

import com.cxf.rsp.Response;



@WebService(endpointInterface = "com.cxf.HelloWorld")   

public class HelloWorldImpl implements HelloWorld {   

	

	public List<String> sayHi(String aName, String bName) {

		ArrayList<String> ans = new ArrayList<String>();

		ans.add(aName);

		ans.add(bName);

		ans.add("Hello!");

		System.out.println("Yeah!");

		return ans;   

	}



	public Response authentcateUser(Request req) {

		

		Response rsp = new Response();

		

		rsp.resultCode = req.times + 1;

		rsp.streamingNo = req.streamingNO;

		

		return rsp;

	}

	

}  

3.修改配置文件Web.xml

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" 

	xmlns="http://java.sun.com/xml/ns/javaee" 

	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<welcome-file-list>

    	<welcome-file>index.jsp</welcome-file>

  	</welcome-file-list>

  	<context-param>  

    	<param-name>contextConfigLocation</param-name>  

    	<param-value>WEB-INF/beans.xml</param-value>  

	</context-param>  

  

	<listener>  

    	<listener-class>  

        		org.springframework.web.context.ContextLoaderListener   

    	</listener-class>  

	</listener>  

  

	<servlet>

		<servlet-name>CXFServlet</servlet-name>

		<servlet-class>

			org.apache.cxf.transport.servlet.CXFServlet

		</servlet-class>

	</servlet>

	<servlet-mapping>

		<servlet-name>CXFServlet</servlet-name>

		<url-pattern>/services/*</url-pattern>

	</servlet-mapping>

</web-app>

4.添加Spring配置文件beans.xml

beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"

	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

	xmlns:jaxws="http://cxf.apache.org/jaxws"

	xsi:schemaLocation=

	"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">



	<import resource="classpath:META-INF/cxf/cxf.xml" />

	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />

	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />



	<jaxws:server id="helloWorld"

  		serviceClass="com.cxf.HelloWorld"

    	address="/HelloWorld">

    	<jaxws:serviceBean>

    		<bean class="com.cxf.impl.HelloWorldImpl"/>

    	</jaxws:serviceBean>

    </jaxws:server>



	<!--

	<jaxws:endpoint 另一种方式endpoint,与server不同的是显示给用户的wsdl不同

	  id="helloWorld" 

	  implementor="com.cxf.impl.HelloWorldImpl" 

	  address="/CXFDemo" />

	-->

	

</beans>

5.发布CXFDemo到Tomcat,发布成功后可以通过 http://localhost:9090/CXFDemo/services/HelloWorld?wsdl 访问wsdl文档.(Tomcat的端口依自己设置而定,默认为8080)

6.增加测试客户端:

可以通过Spring配置文件设置,也可以直接在代码中设置.

Spring配置文件client-beans.xml:

<beans xmlns="http://www.springframework.org/schema/beans"  

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

    xmlns:jaxws="http://cxf.apache.org/jaxws"  

    xsi:schemaLocation="http://www.springframework.org/schema/beans   

    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   

    http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">  

       

    <bean id="helloWorld" class="com.cxf.HelloWorld"    

      factory-bean="clientFactory" factory-method="create"/>  

       

    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  

      <property name="serviceClass" value="com.cxf.HelloWorld"/>  

      <property name="address" value="http://localhost:9090/CXFDemo/services/HelloWorld"/>   

    </bean>

    

</beans>  

Client java代码如下:

package com.cxf.client;



import java.util.Iterator;

import java.util.List;



import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;



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



import com.cxf.HelloWorld;

import com.cxf.req.Request;

import com.cxf.rsp.Response;



public class Client {



    public static void main(String args[]) throws Exception {

    	

//     ClassPathXmlApplicationContext context    
//            = new ClassPathXmlApplicationContext("client-beans.xml");
//     
//     HelloWorld client = (HelloWorld) context.getBean("helloWorld");
    	

    	JaxWsProxyFactoryBean jwfb = new JaxWsProxyFactoryBean();



    	jwfb.setServiceClass(HelloWorld.class);

    	jwfb.setAddress("http://localhost:9090/CXFDemo/services/HelloWorld");

        HelloWorld client = (HelloWorld)jwfb.create();

        

        client.sayHi("Joe","Ruby");

        List ans = client.sayHi("Joe","Ruby");

        Iterator it = ans.iterator();

        System.out.println("Hi");

        while(it.hasNext()) {

        	System.out.println(it.next());

        }

        

        Request req = new Request();

        req.streamingNO = "123456";

        req.times = 9;

        

        Response rsp = client.authentcateUser(req);

        

        System.out.println("StreamingNo is : " + rsp.streamingNo + " ResultCode is : " + rsp.resultCode);

        

    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值