WebService CXF的使用

要学会cxf的使用,我们先看它的几种服务方式

第一种:基于soap的服务
第二种:基于http的服务

webservice restful接口编码风格
CRUD:POST,PUT,GET,DELETE

webservice jax-rs与spring的整合
一.导入pom坐标
    <dependency>
     <groupId>org.apache.cxf</groupId>
     <artifactId>cxf-rt-frontend-jaxrs</artifactId>
     <version>${cxf.version}</version>
   </dependency>

   <dependency>
     <groupId>org.apache.cxf</groupId>
     <artifactId>cxf-rt-rs-client</artifactId>
     <version>${cxf.version}</version>
   </dependency>

   <dependency>
     <groupId>org.apache.cxf</groupId>
     <artifactId>cxf-rt-rs-extension-providers</artifactId>
     <version>${cxf.version}</version>
   </dependency>

   <dependency>
     <groupId>org.codehaus.jettison</groupId>
     <artifactId>jettison</artifactId>
     <version>1.3.7</version>
   </dependency>

二.配置web.xml
配置CXFServlet
<servlet>
   <servlet-name>CXFService</servlet-name>  
    <servlet-class>
    org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
   <servlet-name>CXFService</servlet-name>
   <url-pattern>/services/*</url-pattern>
  </servlet-mapping>

三.配置applicationContext-webservice.xml
配置请求接口.主要配置:address属性
    <jaxrs:server id="customerService" address="/customerService">
   <jaxrs:serviceBeans>
     <bean class="com.itcast.main.service.impl.CustomerServiceImpl" />
   </jaxrs:serviceBeans>
   <jaxrs:inInterceptors>
     <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
   </jaxrs:inInterceptors>
   <jaxrs:outInterceptors>
     <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
   </jaxrs:outInterceptors>
  </jaxrs:server>

四.测试
编写一个接口,在接口中定义一个restful的风格的方法,实现这个方法,通过浏览器进行测试
访问接口中的方法路径 拼接:
工程访问的根路径+web.xml(cxfservlet的配置)+applicationContext-webservice.xml(address)+方法上的@Path

猜你喜欢

转载自blog.csdn.net/zzhhenggg/article/details/82110040