码迷,mamicode.com
首页 > 编程语言 > 详细

CXF+Spring+Tomcat简明示例

时间:2015-10-20 19:55:10      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:

多系统(异构系统)进行交互时,一种良好的方式便是调用Web Service,本示例基于Apache组织的CXF,为了方便起见特将服务端和客户端写在同一个工程下,实际项目中是不可能的,但是客户端却依赖于服务 端的Web Service接口,那么可以通过导出jar的方式。

环境:
MyEclipse10
JDK6
Tomcat7
CXF2.5
Spring3

示例项目结构图:
技术分享

如上图所示,全部依赖的第三方库都在lib中,下面贴出全部代码。
IHelloService.java

技术分享package bing.server;
技术分享
技术分享
import javax.jws.WebService;
技术分享
技术分享
/**
技术分享 * <p>
技术分享 * WebService接口
技术分享 * </p>
技术分享 *
技术分享 *
@author IceWee
技术分享 * @date 2012-7-6
技术分享 *
@version 1.0
技术分享
*/

技术分享@WebService
技术分享
public interface IHelloService {
技术分享
技术分享   
public String sayHello(String username);
技术分享   
技术分享}

技术分享


HelloServiceImpl.java

技术分享package bing.server;
技术分享
技术分享
import javax.jws.WebService;
技术分享
技术分享
/**
技术分享 * <p>
技术分享 * WebService实现类
技术分享 * </p>
技术分享 *
技术分享 *
@author IceWee
技术分享 * @date 2012-7-6
技术分享 *
@version 1.0
技术分享
*/

技术分享@WebService(endpointInterface
= "bing.server.IHelloService", serviceName = "HelloService")
技术分享
public class HelloServiceImpl implements IHelloService {
技术分享
技术分享    @Override
技术分享   
public String sayHello(String username) {
技术分享       
return "hello, " + username;
技术分享    }

技术分享
技术分享}

技术分享


HelloServiceClient.java

技术分享package bing.client;
技术分享
技术分享
import org.springframework.context.ApplicationContext;
技术分享
import org.springframework.context.support.ClassPathXmlApplicationContext;
技术分享
技术分享
import bing.server.IHelloService;
技术分享
技术分享
/**
技术分享 * <p>
技术分享 * WebService调用方-客户端
技术分享 * </p>
技术分享 *
技术分享 *
@author IceWee
技术分享 * @date 2012-7-6
技术分享 *
@version 1.0
技术分享
*/

技术分享
public class HelloServiceClient {
技术分享
技术分享   
public static void main(String[] args) {
技术分享        ApplicationContext context
= new ClassPathXmlApplicationContext("applicationContext-client.xml");
技术分享        IHelloService helloService
= (IHelloService) context.getBean("client");
技术分享        String response
= helloService.sayHello("Peter");
技术分享        System.out.println(response);
技术分享    }

技术分享
技术分享}

技术分享


applicationContext-server.xml

技术分享<?xml version="1.0" encoding="UTF-8"?>
技术分享
<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"
>
技术分享   
<!--
技术分享        ***注意***
技术分享        手动添加的内容:
技术分享        xmlns:jaxws="http://cxf.apache.org/jaxws"
技术分享        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:endpoint id="helloService" implementor="bing.server.HelloServiceImpl" address="/helloService" />
技术分享       
技术分享
</beans>
技术分享


applicationContext-client.xml

技术分享<?xml version="1.0" encoding="UTF-8"?>
技术分享
<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"
>
技术分享   
<!--
技术分享        ***注意***
技术分享        手动添加的内容:
技术分享        xmlns:jaxws="http://cxf.apache.org/jaxws"
技术分享        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" />
技术分享
技术分享   
<bean id="client" class="bing.server.IHelloService" factory-bean="clientFactory" factory-method="create" />
技术分享
技术分享   
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
技术分享       
<property name="serviceClass" value="bing.server.IHelloService" />
技术分享       
<property name="address" value="http://localhost:8080/CXFDemo/ws/helloService" />
技术分享   
</bean>
技术分享
</beans>
技术分享


web.xml

技术分享<?xml version="1.0" encoding="UTF-8"?>
技术分享
<web-app version="3.0"
技术分享    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_3_0.xsd"
>
技术分享 
<display-name>CXFDemo</display-name>
技术分享 
技术分享 
<context-param>
技术分享     
<param-name>contextConfigLocation</param-name>
技术分享     
<param-value>classpath:applicationContext-server.xml</param-value>
技术分享 
</context-param>
技术分享
技术分享 
<listener>
技术分享   
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
技术分享 
</listener>
技术分享 
技术分享 
<servlet>
技术分享   
<servlet-name>CXFServlet</servlet-name>
技术分享   
<display-name>CXFServlet</display-name>
技术分享   
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
技术分享   
<load-on-startup>1</load-on-startup>
技术分享 
</servlet>
技术分享 
<servlet-mapping>
技术分享   
<servlet-name>CXFServlet</servlet-name>
技术分享   
<url-pattern>/ws/*</url-pattern>
技术分享 
</servlet-mapping>
技术分享 
<welcome-file-list>
技术分享   
<welcome-file>index.jsp</welcome-file>
技术分享 
</welcome-file-list>
技术分享
</web-app>
技术分享


所有项目都已配置完成,可以发布到Tomcat了,在浏览器中输入:http://localhost:8080/CXFDemo/ws,返回如图:
技术分享

从上图中可以看到我们对外发布的WebService接口,点击蓝色超链接,返回如图:
技术分享
到此,证明我们的Web Service已经发布成功,可以进行调用测试了。运行HelloServiceClient,返回如图:
技术分享

全文完!













CXF+Spring+Tomcat简明示例

标签:

原文地址:http://my.oschina.net/u/2357322/blog/519476

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!