zoukankan      html  css  js  c++  java
  • RestFul风格

    概念:Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格 设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

    功能资源:互联网所有的事物都可以被抽象为资源

    资源操作:使用POST、DELETE、PUT、GET,使用 不同方法对资源进行操作。 分别对应 添加、 删除、修改、查询。

    传统方式操作资源 :通过不同的参数来实现不同的效果!方法单一,post 和 get

    http://127.0.0.1/item/queryItem.action?id=1 查询,GET 
    http://127.0.0.1/item/saveItem.action 新 增,POST  
    http://127.0.0.1/item/updateItem.action 更新,POST 
    http://127.0.0.1/item/deleteItem.acti on?id=1 删除,GET或POST

    使用RESTful操作资源 : 可以通过不同的请求方式来实现不同的效果!如下:请求地址一样,但是功能 可以不同!

    http://127.0.0.1/item/1 查询,GET 
    http://127.0.0.1/item 新增,POST
    http://127.0.0.1/item 更新,PUT
    http://127.0.0.1/item/1 删除,DELETE

    代码测试

    @Controller
    public class RestFulController {
    
        //原来请求url:http://localhost:8080/springmvc_04_controller_war_exploded/hello?a=1&b=9
    
        //restFul:http://localhost:8080/springmvc_04_controller_war_exploded/hello/1/9
    
        //@RequestMapping(value = "/hello/{a}/{b}",method = RequestMethod.GET)
        @GetMapping("/hello/{a}/{b}")
        public String Test(@PathVariable int a,@PathVariable int b, Model model){
    
            int result=a+b;
    
            model.addAttribute("msg","1、结果为"+result);
    
            return "test";
        }
    
        //@RequestMapping(value = "/hello/{a}/{b}",method = RequestMethod.POST)
        @PostMapping("/hello/{a}/{b}")
        public String Test2(@PathVariable int a,@PathVariable int b, Model model){
    
            int result=a+b;
    
            model.addAttribute("msg","2、结果为"+result);
    
            return "test";
        }
    }
  • 相关阅读:
    软件测试
    python学习之路
    好用的在线画图工具processon
    spring-boot集成dubbo
    公众号开放,关注软件开发过程中的哪些坑
    crontab 中curl命令无法正常执行
    近一月翻阅资料小结
    nginx+tomat8负载后,利用redis实现tomcat8的session共享
    centos7 安装nginx
    centos 上安装redis 3.0.5
  • 原文地址:https://www.cnblogs.com/wdsjg/p/13636263.html
Copyright © 2011-2022 走看看