ice world

There is nothing too difficult if you put your heart into it.
posts - 104, comments - 103, trackbacks - 0, articles - 0

CXF+Spring+Tomcat简明示例

Posted on 2012-07-06 17:29 IceWee 阅读(51136) 评论(27)  编辑  收藏 所属分类: OthersTomcat
多系统(异构系统)进行交互时,一种良好的方式便是调用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,返回如图:


全文完!


























Feedback

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2012-11-06 14:54 by 林祥
<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" />

这些文件没有啊

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2012-11-27 13:09 by 狼湖
HelloServiceImpl 类的注解:
@WebService(endpointInterface = "bind.server.IHelloService", serviceName = "helloService")

注意:serviceName 是小写开头的helloService否则出错

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2012-11-27 13:10 by 狼湖
@林祥
那几个xml在cxf-xxx.jar包里面

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2012-11-28 11:07 by qss
1、非常感谢楼主,我运行成功了。
2、我运行时报错误:
我看了原因是jar不全,我将apache-cxf-2.7.0解压后的lib中的jar包全部引入就好了。
3.再次感谢楼主!//:

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2012-11-28 11:24 by IceWee
@qss
^_^

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2012-11-28 11:53 by qss
1.IceWee,你好。
我想问一下,我返回的是对象时,对象有个属性error_message,部署tomcat时,报:类的两个属性具有相同名称 "error_message" ?

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2012-11-28 15:32 by qss
IceWee,我已经解决了,^_^

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2013-01-16 10:25 by Ameri
谢谢

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2013-03-09 15:44 by asfas d
在网上找了两天 最后还是在你这里把问题解决了 能不能再写个传复杂数据的例子呀LZ

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2013-03-13 10:39 by 周炎婷
你好,我想问一下,如何运行客户端?

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2013-04-26 23:16 by 毕淑敏
好帖子,比网上n个cxf实例都强!

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2013-08-06 15:55 by derekwu
少了 Woodstox这个包,否则报错java.lang.RuntimeException: Cannot create a secure XMLInputFactory

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2013-08-07 13:59 by 开发吧
博主能否提供源码啊?谢谢

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2013-11-25 11:31 by 3000
@derekwu
加了Woodstox之后,报找不到org/codehaus/stax2/XMLInputFactory2类,又加了stax2-api-3.1.1.jar 才好。
我用的apache-cxf-2.6.10

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2013-12-03 14:52 by 啊啊啊啊
啊啊啊啊啊啊啊啊

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2014-01-20 15:56 by 求知者 QQ 285678313
我的applicationContext-server.xml编译不过,“jaxws:endpoint ”那一行报错,错误信息是“Failed to read schema document ‘http://cxf.apache.org/schemas/jaxws.xsd’, because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

说找不到jaxws.xsd文件,我的网络良好,这个URL放在浏览器里可以访问到,求解,上面找不到你的联系方式,我的QQ 285678313

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2014-04-17 13:39 by 黑色柳丁
一模一样的配置,运行后提示:No ASM ClassWriterFound

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2014-04-17 13:39 by 黑色柳丁
然后引入asm的jar包就ok了。

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2014-09-24 11:22 by 555
555

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2014-09-25 14:55 by 邓丰坡
再次感谢,之前找了N多资料,也没有你的一个实例明了。
不过你的jar少说了
stax2-api-3.1.1.jar
woodstox-core-asl-4.2.0.jar
这个确实

# re: CXF+Spring+Tomcat简明示例[未登录]  回复  更多评论   

2014-10-10 11:08 by william
非常感谢楼主的分享,实例太棒啦,我了两天,这是最棒的

# re: CXF+Spring+Tomcat简明示例[未登录]  回复  更多评论   

2014-11-10 13:30 by ada
谢谢楼主,困扰我两天的问题终于解决了!

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2014-11-29 10:51 by
@求知者 QQ 285678313
我也遇到了同样的问题,请问怎么解决。谢谢

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2015-09-19 19:31 by 枯荷听雨
怎样调用那个接口,客户端如何访问呢?

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2015-12-17 14:20 by 匿名者
@林祥
在cxf.jar中

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2016-07-16 17:44 by chiangpan
CXF版本不同,有些其实不需要<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />这些配置

# re: CXF+Spring+Tomcat简明示例  回复  更多评论   

2016-07-23 15:01 by 无异
请问有没有遇到这个问题的?

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'wbService': Invocation of init method failed; nested exception is java.lang.VerifyError: (class: com/wfy/service/jaxws_asm/Aaaa, method: setUserName signature: (Ljava/lang/String;)V) Illegal instruction found at offset 1
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1507)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:644)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:493)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1113)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1671)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

只有注册用户登录后才能发表评论。


网站导航: