`
donggua63966659
  • 浏览: 16739 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

cxf spring 结合问题

阅读更多
   前言,某些服务要发布成webService。webService 不精通,但是还知道一些工具包,庆幸以前用过xfire。ok 发布成功。哇嘎嘎,glassfish v2.2 测试有问题。郁闷。搞了半天不知道如何设置webService中方法的命名空间。哭了。哪就换吧。cxf。嘻嘻。一直想用这个就是没有时间。现在终于可以体验下了。
    进入正文,记录下使用cxf 的一些问题。 使用的是cxf v2.3 + spring v3.0.5

使用cxf 发布webSerivce 服务。

老生常谈,几个配置文件的书写。

1.web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*.xml</param-value>
</context-param>
<!-- listener start -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- listener end -->
<!-- filter start -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- filter end -->
<!-- Servlet start -->
<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>/service/*</url-pattern>
</servlet-mapping>

2.spring-webService.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:endpoint
  id="helloWorldService"
  implementor="com.bsoft.icp.bi6341.WebServiceImpl"
  address="/helloWorldWebService" />
 
</beans>

3.注意对要发布成webService 服务的接口使用注解进行说明。顺道可以定义自己的命名空间。(测试。不使用注解会抛异常)
      @WebService(targetNamespace="http://www.sun.com")

ok,服务器端发布成功,
输入: http://localhost:8080/appName/service/helloWorldWebService?wsdl  显示wsdl 文件。



客户端配置,也使用spring 配置:

<bean id="helloWorldWebService" class="com.test.IHelloworld" factory-bean="clientFactory"
factory-method="create" />

<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.test.IHelloworld" />
<property name="address" value="${webservice.empi}" />
</bean>



客户端配置有几个问题需要注意下:
1.接口需要加 webService 注解。
2.如果服务器端和客户端的命名空间不一样,也会抛出异常。所以最好统一一下命名空键的问题。













分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics