通过CXF,开发rest协议接口(简单明了)

1. 引入cxf的jar包
  pom文件里面直接增加依赖
      < dependency>
          junit
          junit
          4.11
          test
     
     
          org.springframework
          spring- webmvc
          4.0.0.RELEASE 
     
     
           org.apache.cxf
            apache-cxf
           2.4.3
            pom
     

2. 配置web.xml文件
   
   < 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
  < context-param >
            contextConfigLocation
           classpath:config/spring/metadataWebService-spring.xml
     
     
           org.springframework.web.context.ContextLoaderListener
     
     
            CXFServlet
           org.apache.cxf.transport.servlet.CXFServlet
     
     
            CXFServlet
            /services/*
     
     
        encodingFilter
        < filter-class> org.springframework.web.filter.CharacterEncodingFilter
       
            encoding
            UTF-8
       
       
            forceEncoding
            true
       
   
   
        encodingFilter
        /*
   

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

     
     
     

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

      
     
           
                
           
     
     
           
                
           
     

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/

你可能感兴趣的:(Java)