• 实用技术-Restful


    目录

    Rest(REpresentational State Transfer)

    Rest行为约定方式

    Restful开发入门

    Restful简化配置


    • Rest(REpresentational State Transfer)

    • 一种网络资源的访问风格,定义了网络资源的访问方式
    • 传统风格访问路径
    • Rest风格访问路径
    • Restful是按照Rest风格访问网络资源
    • 优点
      • 隐藏资源的访问行为,通过地址无法得知做的是何种操作
      • 书写简化
    • Rest行为约定方式

    • GET(查询)
    • http://localhost/user/1 GET
    • POST(保存)
    • http://localhost/user POST
    • PUT(更新)
    • http://localhost/user PUT
    • DELETE(删除)
    • http://localhost/user DELETE
    • 注意:上述行为是约定方式,约定不是规范,可以打破,所以称Rest风格,而不是Rest规范
    • Restful开发入门

    • SpringMVC支持Restful
      • 请求路径访问配置发生变化
        • 控制器定义方式发生变化
        • 页面调用方式发生变化
    • @RestController
    • public class UserController{
      • @RequestMapping(value="/user/{id}",method=RequestMethod.GET)
      • public String restGet(@PathVariable String id){
        • System.out.println("restful is running ....get:"+id);
        • return "success.jsp";
      • }
      • @RequestMapping(value="/user/{id}",method=RequestMethod.POST)
      • public String restpost(@PathVariable String id) {
        • System.out.println("restful is running ....post:"+id);
        • return "success.jsp";
      • }
      • @RequestMapping(value="/user/{id}",method=RequestMethod.PUT)
      • public String restPut(@PathVariable String id){
        • System.out-println("restful is running ....put:"+id);
        • return "success.jsp";
      • }
      • @RequestMapping(value="/user/{id}",method=RequestMethod.DELETE)
      • public String restDelete(@PathVariable String id){
        • System.out.println("restful is running ....delete:"+id);
        • return "success.jsp";
      • }
    • }
    • 开启SpringMVC对Restful风格的访问支持过滤器,即可通过页面表单提交PUT与DELETE请求
      • hiddenHttpMethodFilter
      • org.springframework.web. filter.HiddenHttpMethodFilter
      • hiddenHttpMethodFilter
      • DispatcherServlet
    • 页面表单使用隐藏域提交请求类型,参数名称固定为_method,必须配合提交类型method=post使用
    • Restful简化配置

    • Restful请求路径简化配置方式
    • @RestController
    • public class UserController{
      • @RequestMapping(value="/user/{id}",method=RequestMethod.DELETE)
      • public String restDelete(@PathVariable String id){
        • System.out.println("restful is running....delete:"+id);
        • return "success.jsp";
      • }
    • }
    • 等同于
    • @RestController
    • @RequestMapping("/user")
    • public class UserController{
      • @DeleteMapping("{id}")
      • public String restDelete(@PathVariable String id){
        • System.out.println("restful is running ....delete:"+id);
        • return "success.jsp";
      • }
    • }
  • 相关阅读:
    数据库、计算机网络,操作系统刷题笔记3
    【slam14】安装多个opencv版本
    eyb:JWT介绍
    残差网络为什么有效
    vuex如何安装、报错、安装版本注意事项
    炫酷3D按钮
    Python 文件加密
    第二章《Java程序世界初探》第8节:条件运算符
    win 安装 Xshell 5
    Java后台开发的前置说明
  • 原文地址:https://blog.csdn.net/weixin_59624686/article/details/128052671