首页 > 其他分享 >Rest-优雅的请求风格(图书增删改查的案例)

Rest-优雅的请求风格(图书增删改查的案例)

时间:2024-03-23 11:11:07浏览次数:13  
标签:return String success 改查 Rest public 增删 id 图书

前的浏览器只支持 post/get 请求,因此为了得到 put/delete 的请求方式需要使用 Spring
提供的 HiddenHttpMethodFilter 过滤器进行转换(只能转换post).
前端代码

<%--
  Created by IntelliJ IDEA.
  User: YRX
  Date: 2024/3/13
  Time: 13:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<script type="text/javascript"  src="script/jquery-3.6.0.min.js"></script>
<script type="text/javascript">
    $(function () {
       // alert("asdfg")
         $("#deleteBook").click(function (){
             $("#hiddenBook").attr("action",this.href);
             //alert("asfaf")
             $(":hidden").val("DELETE");
             $("#hiddenBook").submit();
             return false;
       });
    })
</script>
<body>
<%--查询图书--%>
<h1>查询指定id图书</h1>
<a href="user/book/600">查询图书</a><br>
<h1>Rest添加图书</h1><br>
<form action="user/book" method="post">
    <input type="text" name="bookName">
    <input type="submit">
</form>
<h1>删除指定id的图书</h1>
<a href="user/book/600" id="deleteBook"  >删除指定id的书</a><br>
<form action="" method="post" id="hiddenBook">
    <input type="hidden" name="_method">
</form>
<h1>修改图书图书</h1>
<form action="user/book/700" method="post">
    <input type="hidden" name="_method" value="PUT">
    <input TYPE="submit" value="提交">
</form>
</body>
</html>

后端代码

package com.ysbt.Rest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/user")
public class RestHandle {
    //查询指定图书
    @GetMapping("/book/{id}")
    public String selectBook(@PathVariable("id") String id){
        System.out.println("查询到"+id+"的书");
        return "success";
    }

    //添加图书
    @PostMapping("/book")
    public String addBook(String bookName){
        System.out.println("bookName="+bookName);
        return "success";
    }
    //删除图书
    @RequestMapping(value = "/book/{id}" ,method = RequestMethod.DELETE)
    public String deleteBook(@PathVariable("id") String id){
        System.out.println("删除"+id+"的图书");
        return "redirect:/user/success";
    }
    //修改图书
    @PutMapping("book/{id}")
    public String updateBook(@PathVariable("id") String id){
        System.out.println("修改的图书的id为"+"id");
        return "redirect:/user/success";
    }
    @RequestMapping("/success")
    public String success(){
        return "success";
    }
}

springDispatcherServlet-servlet.xml

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.ysbt.*"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
<!--当时就是因为没有配置这两个,无法识别jquery,-->
<!--    能够支持springmvc的高级功能,比如JSR303校验,支持映射动态请求-->
    <mvc:annotation-driven></mvc:annotation-driven>
<!--    将springmvc不能处理的请求,交给tomcat处理,比如css,js-->
    <mvc:default-servlet-handler/>
</beans>

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">
<!--配置过滤器,不配置的话就无法把post请求转换别的请求-->
    <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>
	<!--    配置前端控制器、中央控制器、分发控制器,用户的请求都会经过它的处理-->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--        配置属性contextConfigLocation,指定DispatcherServlet去操作的spring配置文件-->
<!--        项目启动时自动加载DispatcherServlet-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

标签:return,String,success,改查,Rest,public,增删,id,图书
From: https://www.cnblogs.com/yousuobutong/p/18070418

相关文章

  • 【测试开发学习历程】MySQL增删改操作 + 备份与还原 + 索引、视图、存储过程
    前言:SQL内容的连载,到这里就是最后一期啦!如果有小伙伴要其他内容的话,我会追加内容的。(前提是我有学过,或者能学会)接下来,我们就要开始python内容的学习了~~ 目录1 MySQL增删改操作1.1数据添加操作1.1.1插入完整的行1.1.2插入多行1.2数据更新操作1.3数据删除操......
  • 【数据库】MySQL表的增删改查(二)
    文章目录一、数据库约束一、数据库约束1.1约束类型NOTNULL-指示某列不能存储NULL值。UNIQUE-保证某列的每行必须有唯一的值。DEFAULT-规定没有给列赋值时的默认值。PRIMARYKEY-NOTNULL和UNIQUE的结合。确保某列(或两个列多个列的结合)有唯一标识......
  • Windows Server 2019 Oracle 19c Restore & Recovery
    RMAN>CONNECTTARGET/;RMAN>run{ SQL>shutdownimmediate; startupmountforce; startupmount; setuntiltime"to_date('2024-03-1905:36:58','yyyy-mm-ddhh24:mi:ss')"; restoredatabase; recoverdatabase; al......
  • Django一对多、多对多的增删改查
    一对多的增删改查多对多的增删改查正反向概念书和出版社,外键字段建立在书表里那由书查出版社就是正向,而出版社查书就是反向正向:外键字段在我手里,从我查你反向:外键字段不在我手里,我查你多表查询子查询(基于对象的跨表查询)select*frompublishwhere......
  • RestCloud数据集成平台-Windows全量包安装部署详细教程
     1.安装准备1.1服务器硬件环境要求RestcloudDataOps服务器的最低运行环境如下:CPU:Intel1.6GHz4核或以上内存:可用内存4G或以上(不包括操作系统等其他程序占用内存)可用硬盘空间:40G或以上最少服务器数量:1台1.2服务器端软件环境要求支持操作系统:Windows11/Windows10/W......
  • RestTemplate 和 转发 的区别
    RestTemplate:RestTemplate是Spring框架提供的用于进行RESTful风格的HTTP请求的客户端工具。它封装了HTTP请求所需的各种操作,包括GET、POST、PUT、DELETE等,同时也支持对响应结果的处理。使用RestTemplate可以方便地向其他服务发送HTTP请求,并处理返回的结果,通常用于在微服务架构......
  • 数据库中利用二进制实现多个布尔属性的增删改查
    需求背景业务场景中经常需要一些布尔型的属性来标记数据状态,如果每个属性值都新增一个数据库字段来保存会造成空间上的浪费,在工作中进行建模设计时难免会想到用二进制来节省空间,以整形32位为例,去除最高位的符号位总共可以用来保存31个布尔属性值。实现细节为方便讲解,这里......
  • 全局异常捕获(@RestControllerAdvice)介绍和使用
    @RestControllerAdvice是什么@RestControllerAdvice是Spring框架提供的一个注解,用于定义全局异常处理器和全局数据绑定设置。它结合了@ControllerAdvice和@ResponseBody两个注解的功能。@ControllerAdvice@ControllerAdvice是一个用于定义全局控制器增强(即全局异常处理和......
  • JAVA--数据库(增删改)
    增(INSERT)#给指定字段添加数据insertinto表名(字段1,字段2...)values(值1,值2...);给全部字段添加数据insertinto表名values(值1,值2...);批量添加数据insertinto表名(字段1,字段2...)values(值1,值2...),(值1,值2...),(值1,值2...);   insertinto......
  • Azure REST API (5) Azure创建Service Principal设置Client Secret过期时间100年
    《WindowsAzurePlatform系列文章目录》 我们在使用AzureServicePrinciple,通过应用程序开发API方式部署或修改Azure资源的时候,默认的ClientSecret过期时间为2年。很多客户希望ClientSecret过期时间大于2年。我们可以通过使用应用管理策略(AppManagem......