网站建设定制开发RESTful的详解

文章目录


1. 是什么?

  • RESTful 也称为REST(英文:Representational State Transfer)网站建设定制开发即表现层状态传递,网站建设定制开发它是一种软件架构风格网站建设定制开发或设计风格,网站建设定制开发而不是一个标准。

  • REST 是Roy Fielding博士在2000年他的博士论文中提出来的。它是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。

2. 传统风格与RESTful风格对比

2.1 传统风格

如果是原来的风格,需要发送四个请求,分别是?
查询用户:http://localhost:8080/springmvc/selectuser?id=1GET
增加用户: http://localhost:8080/springmvc/insertuserPOST
修改用户: http://localhost:8080/springmvc/updateuserPUT
删除用户: http://localhost:8080/springmvc/deleteuser?id=1 DELETE

2.1 RESTful风格

按照传统方式发送请求的时候比较麻烦,需要定义多种请求,而RESTful在HTTP协议中,有不同的发送请求的方式,分别是GET、POST、PUT和DELETE方式,分别对应查询、修改、添加和删除操作。我们如果能让不同的请求方式表示不同的请求类型就可以简化我们的查询。

查询用户: http://localhost:8080/springmvc/user/1 GET ­­查询
查询多个用户: http://localhost:8080/springmvc/user GET
添加用户: http://localhost:8080/springmvc/user POST ­­­添加
修改用户: http://localhost:8080/springmvc/user PUT ­­修改
删除用户:http://localhost:8080/springmvc/user DELETE ­­删除

注意:RESTful风格中的URL不存在动词形式的路径,如selectuser表示查询用户,是一个动词,要改为名词user。

3. RESTful的实现

RESTful 风格提倡URL地址使用统一的风格设计,各单词之间用斜杠分开。

3.1 GET、POST方式

3.1.1 创建控制器类

@Controllerpublic class UserController {    @RequestMapping(value = "/user", method = RequestMethod.GET)    public String getAllUser(){        System.out.println("查询所有用户信息");        return "success";    }    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)    public String getUserById(){        System.out.println("根据用户ID查询用户信息");        return "success";    }	@RequestMapping(value = "/user",method = RequestMethod.POST)    public String insertUser(String username,String password){        System.out.println("添加用户信息:" + username + ","+ password);        return "success";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

3.1.2 创建一个jsp页面

  • 通过超链接的方式进行测试
<a href="${pageContext.request.contextPath}/user">查询全部</a><br><a href="${pageContext.request.contextPath}/user/1">根据id查询信息</a><br><form action="${pageContext.request.contextPath}/user" method="post">    用户名:<input type="text" name="username"><br>    密码:<input type="password" name="password"><br>    <input type="submit" value="添加"><br></form>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 运行之后可以在控制台正常输出

3.2 PUT、DELETE方式

一切看起来都非常美好,但是大家需要注意了,我们在发送请求的时候只能发送post或者get,没有办法发送put和delete请求,那么应该如何处理呢?下面开始进入代码环节:

3.2.1 编写控制器方法

@RequestMapping(value = "/user",method = RequestMethod.PUT)public String updateUser(String username,String password){    System.out.println("修改用户信息:" + username + ","+ password);    return "success";}@RequestMapping(value = "/user",method = RequestMethod.DELETE)public String deleteUser(String username,String password){    System.out.println("删除用户信息:" + username + ","+ password);    return "success";}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3.2.2 配置HiddenHttpMethodFilter

  • 在web.xml文件中配置HiddenHttpMethodFilter过滤器来处理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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3.2.3 编写jsp页面

处理put和delete请求方式注意事项:

  • 请求方式必须为: post
  • 请求参数必须为:_method
<form action="${pageContext.request.contextPath}/user" method="post">    <input name="_method" value="put" type="hidden"/>    用户名:<input type="text" name="username"><br>    密码:<input type="password" name="password"><br>    <input type="submit" value="修改"><br></form><form action="${pageContext.request.contextPath}/user" method="post">    <input name="_method" value="delete" type="hidden"/>    用户名:<input type="text" name="username"><br>    密码:<input type="password" name="password"><br>    <input type="submit" value="删除"><br></form>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

测试结果:

3.3 Http405 解决方法

在处理put和delete请求方式时,可能会遇到这种情况:控制台能够正常输出,但是浏览器会报405错误

解决办法:
1.加入 @ResponseBody 注解。
2.请求先转给一个Controller,再返回jsp页面。
注意:注解添加位置在控制器方法处

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发