程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Spring集成Apache CXF開發JAX-RS Web Service,cxfjax-rs

Spring集成Apache CXF開發JAX-RS Web Service,cxfjax-rs

編輯:JAVA綜合教程

Spring集成Apache CXF開發JAX-RS Web Service,cxfjax-rs


什麼是JAX-RS

在JSR-311規范中定義,即Java API for RESTful Web Services,一套Java API,用於開發 RESTful風格的Webservice。

工程概況

主要功能:用戶增刪改查controller(not RESTful,懶得改了) + 查詢用戶webservice(RESTful)

框架:spring+spring mvc+mybatis

數據庫:mysql

構建工具:maven

創建工程

使用IDE創建一個maven工程,pom.xml文件如下,引入cxf、spring、mybatis等依賴,添加tomcat-maven插件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.edu.hdu.wsdemo</groupId> <artifactId>wsdemo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>wsdemo Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <cxf.version>3.0.3</cxf.version> <httpclient.version>3.1</httpclient.version> <jax.ws.rs>2.0.1</jax.ws.rs> <spring.version>4.1.4.RELEASE</spring.version> <mybatis.version>3.2.8</mybatis.version> <jackson.version>1.1.1</jackson.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- http://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <!-- Simple Logging Facade for Java,簡單日志門面 ,允許最終用戶在部署其應用時使用其所希望的日志系統,本身並不是日志解決方案 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency> <!-- http://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version> </dependency> <!-- http://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- http://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.2</version> </dependency> <!-- http://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <!--spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.15</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.5</version> </dependency> <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-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-service-description</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>${httpclient.version}</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>${jax.ws.rs}</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>${jackson.version}</version> </dependency> </dependencies> <build> <finalName>wsdemo</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> </plugin> </plugins> </build> </project> View Code

關於spring+spring mvc+mybatis框架的相關配置就不細說了,這裡主要關注與CXF相關的配置。

首先,是在web.xml添加一個servlet,攔截Web Service的請求:

  <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>/ws/*</url-pattern>
  </servlet-mapping>

然後,配置jaxrs:server:

    <bean id="userWSService" class="cn.edu.hdu.wsdemo.wsservice.UserWSService"></bean>
    <bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />

    <jaxrs:server id="users" address="/apis">
        <jaxrs:serviceBeans>
            <ref bean="userWSService" />
        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <ref bean='jsonProvider' />
        </jaxrs:providers>
    </jaxrs:server>

最後,編寫我們的webservice具體實現:

package cn.edu.hdu.wsdemo.wsservice;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.hdu.wsdemo.model.User;
import cn.edu.hdu.wsdemo.service.user.IUserService;

@Path("/userwsservice/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class UserWSService
{

    @Autowired
    private IUserService userService;

    @GET
    @Path("/users/{id}/")
    public User getUser(@PathParam("id") String id)
    {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("id", id);
        List<User> userList = userService.query(param);

        return userList.size() > 0 ? userList.get(0) : null;
    }

    @POST
    @Path("/users/getall")
    public List<User> getAllUsers()
    {
        List<User> userList = userService.query(new HashMap<String, Object>());
        return userList;
    }

}

測試服務

執行maven命令:clean tomcat:run

啟動成功後,浏覽器輸入:http://localhost:8080/wsdemo/ws

結果:

輸入:http://localhost:8080/wsdemo/ws/apis?_wadl

結果:

 

輸入:http://localhost:8080/wsdemo/ws/apis/userwsservice/users/4

結果:

源碼

 https://github.com/peterchenhdu/demos/tree/master/wsdemo

參考資料

https://examples.javacodegeeks.com/enterprise-java/rest/creating-jax-rs-web-service-using-apache-cxf/

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved