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前端请求

从页面发起PUT、DELETE形式的请求?Spring提供了对Rest风格的支持,利用HiddenHttpMethodFilter进行method转换
 

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:如何发其他形式请求?

    按照以下要求;1、创建一个post类型的表单
                             2、表单项中携带一个_method的参数,
                             3、这个_method的值就是DELETE、PUT
 

表单如下:

<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对象,不让他抛出来

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苍煜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值