Web Services(3)-使用CXF開發REST 服務

1. REST服務介紹

REST 全稱是 Representational State Transfer (表述性狀態轉移), 它是Roy Fielding 博士在2000年寫的一篇關於軟件架構風格的論文, 簡稱REST 服務html

REST 本質上是使用URL來訪問資源的一種方式, 其中包括兩部分: 請求方式和請求路徑,  比較常見的請求方式是GET與POST但在REST中又有其餘類型的請求方式, GET ,POST, PUT,DELETE,HEAD,OPTIONS,REST 是一個「無狀態」的架構模式,任什麼時候候能夠由客戶端發出請求到服務端,最終返回本身想要的數據,也就是說, 服務端將內部資源發佈REST服務,客戶端經過URL來訪問這些資源java

2.使用Spring+CXF發佈REST服務

第一步:添加maven依賴:web

<?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>demo.ws</groupId>
    <artifactId>rest_spring_cxf</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.0.6.RELEASE</spring.version>
        <cxf.version>3.0.0</cxf.version>
        <jackson.version>2.4.1</jackson.version>
    </properties>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- CXF -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <!-- Jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>${jackson.version}</version>
        </dependency>
    </dependencies></project>

第二步:  配置web.xml
ajax

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <!-- Spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- CXF -->
    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping></web-app>


第三步: 將接口實現類發佈springbeanspring

@Component
public class ProductServiceImpl implements ProductService {

    private static final List<Product> productList = new ArrayList<Product>();

    static {
        productList.add(new Product(1, "iphone6", 6500));
        productList.add(new Product(2, "iphone6s", 7000));
    }


    public List<Product> retrieveAllProducts() {
        return productList;
    }
}


第四步: 配置springapache

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">

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

       <import resource="spring-cxf.xml"/>

</beans>


第五步:REST服務調用:json

$(function() {
        $.ajax({            
            type: 'get',
            url: 'http://localhost:8080/ws/rest/products',
            dataType: 'json',
            success: function(data) {                
                 var template = $("#product_table_template").html();                
                 var render = Handlebars.compile(template);                
                 var html = render({
                    data: data
                });
                $('#product').html(html);
            }
        });
    });

3.總結

至此, 關於什麼是REST服務,如何使用REST服務,與spring集成咱們已經學習完了,但願對你們有幫助架構

相關文章
相關標籤/搜索