cxf+spring完成webservice发布

 

1 下载cxf和spring的jar包,具体jar包可以自己搜索也可以直接在我的资源里面下载链接 http://download.csdn.net/detail/liuzhigang1237/3694045 这个资源中有完整的开发样例。其中lib包中有所需要的全部jar包。

2 修改web.xml,具体web.xml文件内容如下

 
 


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
    <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <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>/webservices/*</url-pattern>
 </servlet-mapping>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
3 修改spring配置文件如下

<?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:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:jaxws="http://cxf.apache.org/jaxws"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" >

   <!-- 通过Spring创建数据绑定的类-->  
    <bean id="aegisBean" class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
    <bean id="jaxWsServiceFactoryBean" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean" scope="prototype">
      <property name="wrapped" value="true" />
       <property name="dataBinding" ref="aegisBean" />
    </bean>
    <bean id="hospitalReservationService" class="com.cxf.HospitalReservationServiceImpl">
 </bean>
 <jaxws:endpoint id="reservationService" implementor="#hospitalReservationService" address="/reservationService">
   <jaxws:serviceFactory>
   <ref bean="jaxWsServiceFactoryBean" />
   </jaxws:serviceFactory>
 </jaxws:endpoint>
</beans>

4 新建一个接口和一个实现类如下:

要发布的webservice接口如下 HospitalExceptionReservationServiceIF

package com.cxf;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HospitalExceptionReservationServiceIF {
public String helloWord(@WebParam(name = "testStr")String testStr);
}

下面是它的实现类,这个不能错哦

package com.cxf;

import javax.jws.WebService;

@WebService(endpointInterface = "com.cxf.HospitalExceptionReservationServiceIF", serviceName = "reservationService")
public class HospitalReservationServiceImpl implements HospitalExceptionReservationServiceIF{
 public String helloWord(String testStr) {
  System.out.println("WebService返回");
  return "helloWord";
 }

}

ok,到此为止一个简单的cxf+spring的webservice已经完工,检查一下吧 http://localhost:9090/webservices/

根据自己的端口修改,然后可以看到这个下面你自己发布的方法。客户端调用如果有问题可以qq我731394253

你可能感兴趣的:(cxf+spring完成webservice发布)