1. 引入cxf的jar包

  pom文件里面直接增加依赖

< dependency> 

 

            <groupId > junit</ groupId> 

 

            <artifactId > junit</ artifactId> 

 

            <version >4.11 </version > 

 

            <scope >test </scope > 

 

        </dependency > 

 

        <dependency > 

 

            <groupId >org.springframework </groupId > 

 

            <artifactId >spring- webmvc</ artifactId> 

 

            <version >4.0.0.RELEASE </version > 

 

        </dependency > 

 

        <dependency > 

 

             <groupId >org.apache.cxf </groupId > 

 

             <artifactId > apache-cxf </artifactId > 

 

             <version >2.4.3</version > 

 

             <type > pom</ type> 

 

        </dependency > 

 
 
 
 

  2. 配置web.xml文件 

 

     <? xml version= "1.0" encoding= "UTF-8" ?> 

 

     < web-app xmlns= "http://xmlns.jcp.org/xml/ns/javaee" 

 
 
 
 

      xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" 

 

        xsi:schemaLocation= "http://xmlns.jcp.org/xml/ns/javaee 

 

       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version= "3.1" > 

 
 
 
 

    < display-name >Archetype Created Web Application </display-name > 

 

    < context-param > 

 

              <param-name >contextConfigLocation </param-name > 

 

             <param-value >classpath:config/spring/metadataWebService-spring.xml </param-value > 

 

        </context-param > 

 

        <listener > 

 

             <listener-class >org.springframework.web.context.ContextLoaderListener </listener-class > 

 

        </listener > 

 

        <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 >/services/* </url-pattern > 

 

        </servlet-mapping > 

 

        <filter > 

 

          <filter-name >encodingFilter </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 > 

 

          <init-param > 

 

              <param-name >forceEncoding </param-name > 

 

              <param-value >true </param-value > 

 

          </init-param > 

 

      </filter > 

 

      <filter-mapping > 

 

          <filter-name >encodingFilter </filter-name > 

 

          <url-pattern >/* </url-pattern > 

 

      </filter-mapping > 

 

  </ web-app> 

 
 
 
 

  3. 配置cxf.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" 

 

        xmlns:context= "http://www.springframework.org/schema/context" 

 

        xmlns:jaxrs= "http://cxf.apache.org/jaxrs" 

 

        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 

 

          http://www.springframework.org/schema/context 

 

          http://www.springframework.org/schema/context/spring-context.xsd 

 

          http://cxf.apache.org/jaxrs 

 

          http://cxf.apache.org/schemas/jaxrs.xsd" > 

 
 
 
 

        <import resource = "classpath:META-INF/cxf/cxf.xml" /> 

 

        <import resource = "classpath:META-INF/cxf/cxf-servlet.xml" /> 

 

        <context:annotation-config /> 

 
 
 
 

        <!-- 扫描开启服务的包路径,在base-package下面所有的类都会扫描 --> 

 

        < context:component-scan base-package= "com.sigmatrix.ws" /> 

 
 
 
 

        <!-- REST 服务开放 id和address都是唯一的,在这里开通了两个id,ref可以有多个,bean的名称为接口实现类第一个字母小写--> 

 

        <jaxrs:server id = "metadata" address= "/metadata" > 

 

              <jaxrs:serviceBeans > 

 

                   <ref bean = "metadataRestImpl"/> 

 

              </jaxrs:serviceBeans > 

 

        </jaxrs:server > 

 

        <jaxrs:server id = "dictionary" address= "/dictionary" > 

 

              <jaxrs:serviceBeans > 

 

                   <ref bean = "dictionaryRestImpl"/> 

 

              </jaxrs:serviceBeans > 

 

        </jaxrs:server > 

 

  </ beans> 

 
 
 
 

  4. 要开放的接口加上cxf服务的注解 

 

     接口类: 

 

     public interface IMetadataRest { 

 

        /** 

 

        * 查询所有字典字典项信息 

 

        * @return 

 

        */ 

 

        @POST 

 

        @Path( "findAllDictionary") 

 

        public abstract String findAllDictionary(); 

 

     } 

 

     接口实现类: 

 

     //加入注解 

 

     @Component 

 

     public class MetadataRestImpl implements IMetadataRest { 

 

        @Override 

 

        public String findAllDictionary () { 

 

              return "Hello World"; 

 

       } 

 

     }


5. 部署到tomcat启动即可,测试访问   ip:端口号//项目名称/services/