JavaWeb-15-SpringMVC的Rest接口

Table of Contents

 

一:Rest接口簡介

1:Rest介紹

2:Rest風格下的url 

二:後端接口

三:Rest前端請求

 

1:配置HiddenHttpMethodFilter攔截器(在web.xml中)

2:如何發其他形式請求?

3:tomcat8以上版本因返回頁面也是delete,put請求,不支持報錯


一:Rest接口簡介

1:Rest介紹

REST:即 Representational State Transfer。(資源)表現層狀態轉化。是目前最流行的一種互聯網軟件架構。它結構清晰、符合標準、易於理解、擴展方便,所以正得到越來越多網站的採用

  • 資源(Resources:網絡上的一個實體,或者說是網絡上的一個具體信息。

它可以是一段文本、一張圖片、一首歌曲、一種服務,總之就是一個具體的存在。

可以用一個URI(統一資源定位符)指向它,每種資源對應一個特定的 URI 。

獲取這個資源,訪問它的URI就可以,因此 URI 即爲每一個資源的獨一無二的識別符。

  • 表現層(Representation:把資源具體呈現出來的形式,叫做它的表現層(Representation)。比如,文本可以用 txt 格式表現,也可以用 HTML 格式、XML 格式、JSON 格式表現,甚至可以採用二進制格式。
  • 狀態轉化(State Transfer:每發出一個請求,就代表了客戶端和服務器的一次交互過程。HTTP協議,是一個無狀態協議,即所有的狀態都保存在服務器端。因此,如果客戶端想要操作服務器,必須通過某種手段,讓服務器端發生“狀態轉化”(State Transfer)。

而這種轉化是建立在表現層之上的,所以就是 “表現層狀態轉化”。

  • 具體說,就是 HTTP 協議裏面,四個表示操作方式的動詞:GET、POST、PUT、DELETE

它們分別對應四種基本操作:GET 用來獲取資源,POST 用來新建資源,PUT 用來更新資源,DELETE 用來刪除資源。

2:Rest風格下的url 

/order/1  HTTP GET :得到 id = 1 的 order   

/order/1  HTTP DELETE刪除 id = 1的 order   

/order/1  HTTP PUT:更新id = 1的 order   

/order     HTTP POST:新增 order 

二:後端接口

package com.wkl.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Description:
 * Date:       2020/7/5 - 下午 4:35
 * author:     wangkanglu
 * version:    V1.0
 */
@Controller
public class RestController {
    /**
     * 處理查詢圖書請求
     * @param id
     * @return
     */
    @RequestMapping(value="/books/{bid}",method= RequestMethod.GET)
    public String getBook(@PathVariable("bid")Integer id) {
        System.out.println("查詢到了"+id+"號圖書");
        return "success";
    }

    /**
     * 刪除圖書
     * @param id
     * @return
     */
    @RequestMapping(value = "/books/{bid}",method = RequestMethod.DELETE)
    public String deleteBook(@PathVariable("bid")Integer id){
        System.out.println("刪除了"+id+"號圖書");
        return "success";
    }

    /**
     * 圖書更新
     * @return
     */
    @RequestMapping(value="/book/{bid}",method=RequestMethod.PUT)
    public String updateBook(@PathVariable("bid")Integer id) {
        System.out.println("更新了"+id+"號圖書");
        return "success";
    }

    @RequestMapping(value="/book",method=RequestMethod.POST)
    public String addBook() {
        System.out.println("添加了新的圖書");
        return "success";
    }

}

三:Rest前端請求

 

1:配置HiddenHttpMethodFilter攔截器(在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_4_0.xsd"
         version="4.0">


  <display-name>Archetype Created Web Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--配置HiddenHttpMethodFilter,實現rest風格接口-->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

2:如何發其他形式請求?

表單如下:

<body>
    <h1>hello</h1>
    <a href="/books/1">查詢</a>

    <form action="/books/1" method="post">
        <input type="hidden" name="_method" value="delete">
        <input type="submit" value="刪除圖書">
    </form>

    <form action="/books/1" method="post">
        <input type="hidden" name="_method" value="put">
        <input type="submit" value="更新圖書">
    </form>
    <form action="/books/1" method="">
        <input type="submit" value="添加圖書">
    </form>
</body>

3:tomcat8以上版本因返回頁面也是delete,put請求,不支持報錯

解決辦法:在返回的相應頁面增加jsp的ErrorPage對象,不讓他拋出來

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章