您的位置:首页 > 编程语言 > Java开发

CXF WebService整合Spring

2015-09-20 15:31 295 查看
首先,CXF和spring整合需要准备如下jar包文件:



这边我是用Spring的jar包是Spring官方提供的,并没有使用CXF中的Spring的jar文件。
添加这么多文件后,首先在web.xml中添加如下配置:

<!--加载Spring容器配置-->

<listener>

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

</listener>

<!--设置Spring容器加载配置文件路径-->

<context-param>

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

<param-value>classpath*:applicationContext-server.xml</param-value>

</context-param>


<listener>

<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

</listener>


<servlet>

<servlet-name>CXFService</servlet-name>

<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

</servlet>


<servlet-mapping>

<servlet-name>CXFService</servlet-name>

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

</servlet-mapping>


然后在src目录中,新建一个applicationContext-server.xml文件,文件内容如下:

<?xmlversion="1.0"encoding="UTF-8"?>

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

xmlns:context="http://www.springframework.org/schema/context"

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

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

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

'target='_blank'>http://www.springframework.org/schema/beans/spring-beans-3.0.xsd[/code]
'target='_blank'>http://www.springframework.org/schema/context[/code]
'target='_blank'>http://www.springframework.org/schema/context/spring-context-3.0.xsd[/code]
http://cxf.apache.org/jaxws

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


注意上面的带下划线加粗部分,这个很重要的哦!不能写错或是遗漏了。
添加完这个文件后,还需要在这个文件中导入这么几个文件。文件内容如下:

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

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

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


下面开始写服务器端代码,首先定制服务器端的接口,代码如下:

packagecom.hoo.service;


importjavax.jws.WebParam;

importjavax.jws.WebService;

importjavax.jws.soap.SOAPBinding;

importjavax.jws.soap.SOAPBinding.Style;

importcom.hoo.entity.User;

importcom.hoo.entity.Users;


/**

*<b>function:</b>定制客户端请求WebService所需要的接口

*@authorhoojo

*@createDate2011-3-18上午08:22:55

*@fileComplexUserService.java

*@packagecom.hoo.service

*@projectCXFWebService

*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code]
*@emailhoojo_@126.com

*@version1.0

*/

@WebService

@SOAPBinding(style=Style.RPC)

publicinterfaceIComplexUserService{


publicUsergetUserByName(@WebParam(name="name")Stringname);


publicvoidsetUser(Useruser);

}


下面编写WebService的实现类,服务器端实现代码如下:

packagecom.hoo.service;


importjava.util.ArrayList;

importjava.util.Date;

importjava.util.HashMap;

importjava.util.List;

importjavax.jws.WebParam;

importjavax.jws.WebService;

importjavax.jws.soap.SOAPBinding;

importjavax.jws.soap.SOAPBinding.Style;

importcom.hoo.entity.User;

importcom.hoo.entity.Users;


/**

*<b>function:</b>WebService传递复杂对象,如JavaBean、Array、List、Map等

*@authorhoojo

*@createDate2011-3-18上午08:22:55

*@fileComplexUserService.java

*@packagecom.hoo.service

*@projectCXFWebService

*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code]
*@emailhoojo_@126.com

*@version1.0

*/

@WebService

@SOAPBinding(style=Style.RPC)

@SuppressWarnings("deprecation")

publicclassComplexUserServiceimplementsIComplexUserService{


publicUsergetUserByName(@WebParam(name="name")Stringname){

Useruser=newUser();

user.setId(newDate().getSeconds());

user.setName(name);

user.setAddress("china");

user.setEmail(name+"@hoo.com");

returnuser;

}


publicvoidsetUser(Useruser){

System.out.println("############ServersetUser###########");

System.out.println("setUser:"+user);

}

}


注意的是和Spring集成,这里一定要完成接口实现,如果没有接口的话会有错误的。
下面要在applicationContext-server.xml文件中添加如下配置:

<beanid="userServiceBean"class="com.hoo.service.ComplexUserService"/>


<beanid="inMessageInterceptor"class="com.hoo.interceptor.MessageInterceptor">

<constructor-argvalue="receive"/>

</bean>


<beanid="outLoggingInterceptor"class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>

<!--注意下面的address,这里的address的名称就是访问的WebService的name-->

<jaxws:serverid="userService"serviceClass="com.hoo.service.IComplexUserService"address="/Users">

<jaxws:serviceBean>

<!--要暴露的bean的引用-->

<refbean="userServiceBean"/>

</jaxws:serviceBean>

<jaxws:inInterceptors>

<refbean="inMessageInterceptor"/>

</jaxws:inInterceptors>

<jaxws:outInterceptors>

<refbean="outLoggingInterceptor"/>

</jaxws:outInterceptors>

</jaxws:server>


下面启动tomcat服务器后,在WebBrowser中请求:
http://localhost:8080/CXFWebService/Users?wsdl
如果你能看到wsdl的xml文件的内容,就说明你成功了,注意的是上面地址的Users就是上面xml配置中的address的名称,是一一对应的。
下面编写客户端请求的代码,代码如下:

packagecom.hoo.client;


importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;

importcom.hoo.entity.User;

importcom.hoo.service.IComplexUserService;


/**

*<b>function:</b>请求Spring整合CXF的WebService客户端

*@authorhoojo

*@createDate2011-3-28下午03:20:35

*@fileSpringUsersWsClient.java

*@packagecom.hoo.client

*@projectCXFWebService

*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code]
*@emailhoojo_@126.com

*@version1.0

*/

publicclassSpringUsersWsClient{


publicstaticvoidmain(String[]args){

//调用WebService

JaxWsProxyFactoryBeanfactory=newJaxWsProxyFactoryBean();

factory.setServiceClass(IComplexUserService.class);

factory.setAddress("http://localhost:8080/CXFWebService/Users");


IComplexUserServiceservice=(IComplexUserService)factory.create();


System.out.println("#############ClientgetUserByName##############");

Useruser=service.getUserByName("hoojo");

System.out.println(user);


user.setAddress("China-Guangzhou");

service.setUser(user);

}

}


运行后,可以在控制台中看到
log4j:WARNNoappenderscouldbefoundforlogger(org.apache.cxf.bus.spring.BusApplicationContext).
log4j:WARNPleaseinitializethelog4jsystemproperly.
log4j:WARNSeehttp://logging.apache.org/log4j/1.2/faq.html#noconfigformoreinfo.
2011-3-2818:12:26org.apache.cxf.service.factory.ReflectionServiceFactoryBeanbuildServiceFromClass
信息:CreatingService{http://service.hoo.com/}IComplexUserServiceServicefromclasscom.hoo.service.IComplexUserService
#############ClientgetUserByName##############
27#hoojo#hoojo@hoo.com#china

Tomcat控制台



这个server端是通过Spring整合配置的,下面我们将Client端也通过Spring配置完成整合。
首先增加applicationContext-client.xml配置文件,文件内容如下:

<?xmlversion="1.0"encoding="UTF-8"?>

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

xmlns:context="http://www.springframework.org/schema/context"

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

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

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

'target='_blank'>http://www.springframework.org/schema/beans/spring-beans-3.0.xsd[/code]
'target='_blank'>http://www.springframework.org/schema/context[/code]
'target='_blank'>http://www.springframework.org/schema/context/spring-context-3.0.xsd[/code]
http://cxf.apache.org/jaxws

'target='_blank'>http://cxf.apache.org/schemas/jaxws.xsd"[/code]

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

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

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


<jaxws:clientid="userWsClient"serviceClass="com.hoo.service.IComplexUserService"

address="http://localhost:8080/CXFWebService/Users"/>

</beans>


客户端请求代码如下:

packagecom.hoo.client;


importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

importcom.hoo.entity.User;

importcom.hoo.service.IComplexUserService;


/**

*<b>function:</b>请求Spring整合CXF的WebService客户端

*@authorhoojo

*@createDate2011-3-28下午03:20:35

*@fileSpringUsersWsClient.java

*@packagecom.hoo.client

*@projectCXFWebService

*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code]
*@emailhoojo_@126.com

*@version1.0

*/

publicclassSpringUsersWsClient{


publicstaticvoidmain(String[]args){

ApplicationContextctx=newClassPathXmlApplicationContext("applicationContext-client.xml");


IComplexUserServiceservice=ctx.getBean("userWsClient",IComplexUserService.class);


System.out.println("#############ClientgetUserByName##############");

Useruser=service.getUserByName("hoojo");

System.out.println(user);


user.setAddress("China-Guangzhou");

service.setUser(user);

}

}


运行后结果如下:
#############ClientgetUserByName##############
45#hoojo#hoojo@hoo.com#china
############ServersetUser###########
setUser:45#hoojo#hoojo@hoo.com#China-Guangzhou
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: