1. 程式人生 > >JAX-RS 和 和 Spring 整合開發

JAX-RS 和 和 Spring 整合開發

進行 div 位置 ans 發包 http out app startup

JAX-RS 和 和 Spring 整合開發

1、建立maven項目

技術分享圖片

2、導入maven坐標

<dependencies>
          <!-- cxf 進行rs開發 必須導入  -->
          <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>3.0.1</
version> </dependency> <!-- 日誌引入 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> <!--
客戶端 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-client</artifactId> <version>3.0.1</version> </dependency> <!-- 擴展json提供者 --> <dependency> <
groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-extension-providers</artifactId> <version>3.0.1</version> </dependency> <!-- 轉換json工具包,被extension providers 依賴 --> <dependency> <groupId>org.codehaus.jettison</groupId> <artifactId>jettison</artifactId> <version>1.3.7</version> </dependency> <!-- spring 核心 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.7.RELEASE</version> </dependency> <!-- spring web集成 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.7.RELEASE</version> </dependency> <!-- spring 整合junit --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.7.RELEASE</version> </dependency> <!-- junit 開發包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>

3、配置web.xml

<!-- spring配置文件位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- spring核心監聽器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <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>

4、編寫實體類與Service

技術分享圖片

5、在applicationContext.xml中配置發布RS服務

技術分享圖片

<!-- 
        address 發布服務地址 
        servicesBeans 服務實現類 
     -->
    <jaxrs:server id="userService" address="/userService" >
        <jaxrs:serviceBeans>
            <bean class="cn.itcast.cxf.service.UserServiceImpl" />
        </jaxrs:serviceBeans>
        <jaxrs:inInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
        </jaxrs:inInterceptors>
        <jaxrs:outInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
        </jaxrs:outInterceptors>
    </jaxrs:server>

6、配置服務啟動端口

<build>
      <plugins>
          <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>tomcat-maven-plugin</artifactId>
              <version>1.1</version>
              <configuration>
                  <port>9996</port>
              </configuration>
          </plugin>
      </plugins>
  </build>

JAX-RS 和 和 Spring 整合開發