RestFul风格

suaxi
2020-12-17 / 0 评论 / 78 阅读 / 正在检测是否收录...

RestFul风格

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

传统资源操作方式:

使用RestFul风格:

常用的资源操作:

POST、DELETE、PUT、GET

package com.sw.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @Author suaxi
 * @Date 2020/12/17 11:25
 */

@Controller
public class RestFulController {

    //传统风格 http://localhost:8088/add01?a=1&b=1
    @RequestMapping(value = "/add01/{a}/{b}",method = RequestMethod.GET)
    public String test01(@PathVariable int a,@PathVariable int b, Model m){
        int result = a + b;
        m.addAttribute("msg","结果1为:"+result);
        return "hello";
    }

    //简化  http://localhost:8088/add02/1/2
    @PostMapping("/add02/{a}/{b}")
    public String test02(@PathVariable int a,@PathVariable int b, Model m){
        int result = a + b;
        m.addAttribute("msg","结果2为:"+result);
        return "hello";
    }
}

注:@PathVariable 让方法参数的值对应绑定到一个URI模板变量上

0

评论 (0)

取消