Restful风格实现CRUD源码.7z RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义。RESTFUL适用于移动互联网厂商作为业务使能接口的场景,实现第三方OTT调用移动网络资源的功能,动作类型为新增、变更、删除所调用资源。 这里附上源码文件,有需要的朋友可以下载翻阅,部分代码都有详细的注释,其中附带SQL文件,希望可以帮助到大家! Restful 一种软件架构风格、设计风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。 REST(英文:Representational State Transfer,简称REST)描述了一个架构样式的网络系统,比如 web 应用程序。它首次出现在 2000 年 Roy Fielding 的博士论文中,他是 HTTP 规范的主要编写者之一。在目前主流的三种Web服务交互方案中,REST相比于SOAP(Simple Object Access protocol,简单对象访问协议)以及XML-RPC更加简单明了,无论是对URL的处理还是对Payload的编码,REST都倾向于用更加简单轻量的方法设计和实现。值得注意的是REST并没有一个明确的标准,而更像是一种设计的风格。 Restful是一种设计风格。对于我们Web开发人员来说。就是使用一个url地址表示一个唯一的资源。然后把原来的请求参数加入到请求资源地址中。然后原来请求的增,删,改,查操作。改为使用HTTP协议中请求方式GET、POST、PUT、DELETE表示 使用Restful风格,这里需要明确一下几点 1. 就是把传统的请求参数加入到请求地址是什么样子? 2. Restful风格中请求方式GET、POST、PUT、DELETE分别表示查、增、改、删。 请求操作 表示要查询id为1的用户 表示查询全部的用户 POST 请求 表示要添加一个用户 PUT 请求 DELETE 请求 我们知道发起 GET 请求和 POST 请求,只需要在表单的 form 标签中,设置 method =”get” 就是GET请求。 设置 form 标签的method=”post”。就会发起 POST 请求。而 PUT 请求和 DELETE 请求。 简单代码实现 1. RestController.java 2. spring-mcv.xml 3. web.xml 4. index.jsp 1. Spring MVC 底层默认用的是请求转发.而在Tomcat8之后的一些高版本,使用restful风格访问然后转发到jsp页面。就会有如下的错误提示: 2. 解决方法一 [ 在 jsp 页面 page 标签中加 isErrorPage=”true” ] : 3. 解决方法二 [ 使用重定向 ] 如果文章对你有帮助记得 + 关注哦! QQ交流群:1101584918,欢迎大家加入。
Restful 风格的介绍
看总结简单明了
传统的方式是: 比如:https://ip:port/工程名/资源名?请求参数 举例:https://127.0.0.1:8080/springmvc/user?action=delete&id=1 Restful风格是: 比如:https://ip:port/工程名/资源名/请求参数/请求参数 举例:https://127.0.0.1:8080/springmvc/user/1
请求方式
操作内容
GET 请求
对应
查询
https://ip:port/工程名/user/1
HTTP 请求 GET
https://ip:port/工程名/user
HTTP 请求 GET
对应
添加
https://ip:port/工程名/user
HTTP 请求 POST
对应
修改
https://ip:port/工程名/user/1
HTTP 请求 PUT
表示要修改id为1的用户信息
对应
删除
https://ip:port/工程名/user/1
HTTP 请求 DELETE
表示要删除id为1的用户信息
SpringMVC中如何发送GET请求、POST请求、PUT请求、DELETE请求。
package com.spring.mvc.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; import org.springframework.web.servlet.ModelAndView; /** * Restful风格的 Controller * Created by YongXin Xue on 2020/05/12 9:55 */ @Controller public class RestController { ///// restful风格中请求方式GET、POST、PUT、DELETE分别表示查、增、改、删。 /** * @PathVariable 路径参数获取 * value = "/queryUserById/{id}" 中的 {id} 表示路径参数 ,表示一个可变的值。 {id} 里面的 id 是变量的名称 * * @PathVariable("id") Integer id 表示把指定 id 的路径参数值,赋给方法参数id。 * [ 也可以设置多个值 ] 如下 : Integer id; String name; */ @RequestMapping(value = "/queryUserById/{id}/{name}", method = RequestMethod.GET) public ModelAndView queryUserById(@PathVariable("id") Integer id, @PathVariable("name") String name){ System.out.println(" 查询一条用户记录信息!! " + id + name); // redirect 到 index.jsp 页面 return new ModelAndView("/index.jsp"); } @RequestMapping(value = "/queryUserList", method = RequestMethod.GET) public ModelAndView queryUserList() { System.out.println(" 查询全部用户记录信息!! "); // redirect 到 index.jsp 页面 return new ModelAndView("redirect:/index.jsp"); } @RequestMapping(value = "/saveUser", method = RequestMethod.POST) public ModelAndView saveUser() { System.out.println(" 保存用户记录信息!! "); // redirect 到 index.jsp 页面 return new ModelAndView("redirect:/index.jsp"); } @RequestMapping(value = "/updateUser/1", method = RequestMethod.PUT) public ModelAndView updateUser() { System.out.println(" 更新用户记录信息!! "); // redirect 到 index.jsp 页面 return new ModelAndView("/index.jsp"); } @RequestMapping(value = "/deleteUserById/1", method = RequestMethod.DELETE) public ModelAndView deleteUserById() { System.out.println(" 删除用户记录信息!! "); // redirect 到 index.jsp 页面 return new ModelAndView("redirect:/index.jsp"); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:context="https://www.springframework.org/schema/context" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 包扫描 --> <context:component-scan base-package="com.spring.mvc"/> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="https://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee https://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 配置前端控制器 --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置 SpringMVC 的容器配置文件路径 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 中配置 Restful 的 Filter 过滤器用来识别 PUT 和 DELETE 请求 --> <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>
<%-- Created by YongXin Xue on 2020/05/12 9:52 --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Restful 风格</title> </head> <body> <h2>Restful ADN CRUD</h2> <h3><a href="${pageContext.request.contextPath}/queryUserById/290/lance">查询一条用户记录</a></h3> <h3><a href="${pageContext.request.contextPath}/queryUserList">查询全部用户记录</a></h3> <form action="${pageContext.request.contextPath}/saveUser" method="post"> <input type="submit" value="保存用户"> </form> <%-- 如何发送 PUT请求,和 DELETE 请求 1. 首先要有一个 post 请求的表单 2. 要有一个隐藏域 <input type="hidden" name="_method" value="PUT"> <input type="hidden" name="_method" value="DELETE"> 3. 在 web.xml 中配置 Restful 的 Filter 过滤器用来识别 PUT 和 DELETE 请求 --%> <form action="${pageContext.request.contextPath}/updateUser/1" method="post"> <input type="hidden" name="_method" value="PUT"> <input type="submit" value="更新用户"> </form> <form action="${pageContext.request.contextPath}/deleteUserById/1" method="post"> <input type="hidden" name="_method" value="DELETE"> <input type="submit" value="删除用户"> </form> </body> </html>
需要注意Restful风格在高版本Tomcat 中无法转发到jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算