Springboot基础篇-REST风格

71 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

什么是REST风格

REST(Representational State Transfer)  表现形式状态转换

​编辑

​编辑

为什么是风格而不是规范?

​编辑

REST风格优点

  • 隐藏资源的访问行为,无法通过地址得知对资源是何种操作
  • 简化书写

RESTful

RESTful含义是使用这种风格来访问资源

REST表示资源的描述形式 

入门案例

package com.example.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.bind.annotation.ResponseBody;

@Controller
public class UserController {
    //增加
    //http://localhost:8080/users      POST请求
    @RequestMapping(value = "/users",method = RequestMethod.POST)
    @ResponseBody
    public String save(){
        System.out.println("user save");
        return "{'module':'user save'}";
    }
    //删除
    //http://localhost:8080/users/1     DELETE请求
    @RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id){
        System.out.println("user delete"+id);
        return "{'module':'user delete'}";
    }
    //修改
    //http://localhost:8080/users       PUT请求
    @RequestMapping(value = "/users",method = RequestMethod.PUT)
    @ResponseBody
    public String update(){
        System.out.println("user update");
        return "{'module':'user update'}";
    }
    //查询单个
    //http://localhost:8080/users/1     GET请求
    @RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String getById(@PathVariable Integer id){
        System.out.println("user select");
        return "{'module':'user select'}";
    }
    //查询多个
    //http://localhost:8080/users       GET请求
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    @ResponseBody
    public String getAll(){
        System.out.println("user selectAll");
        return "{'module':'user selectAll'}";
    }
}

step1

设定HTTP请求动作method

step2

需要参数的需要增加{},{}从路径中给里面的变量赋值,@PathVariable变量表示从上面的路径中去取值,注意{}中的变量名要和下面形参的变量名相同

基于REST风格的注解开发

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//@Controller
//@ResponseBody  //代替下面所有方法的@ResponseBody
@RequestMapping("/users")//代替下面所有的value=”/users“,如果有参数的话还得带着value
@RestController//代替@ResponseBody和@Controller
public class UserController {
    //增加
    //http://localhost:8080/users      POST请求
    @PostMapping//@RequestMapping(method = RequestMethod.POST)
    public String save(){
        System.out.println("user save");
        return "{'module':'user save'}";
    }
    //删除
    //http://localhost:8080/users/1     DELETE请求
    @DeleteMapping("/{id}")//代替 @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public String delete(@PathVariable Integer id){
        System.out.println("user delete"+id);
        return "{'module':'user delete'}";
    }
    //修改
    //http://localhost:8080/users       PUT请求
    @PutMapping//代替 @RequestMapping(method = RequestMethod.PUT)
    public String update(){
        System.out.println("user update");
        return "{'module':'user update'}";
    }
    //查询单个
    //http://localhost:8080/users/1     GET请求
    @GetMapping("{/id}")//代替@RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public String getById(@PathVariable Integer id){
        System.out.println("user select");
        return "{'module':'user select'}";
    }
    //查询多个
    //http://localhost:8080/users       GET请求
    @GetMapping//代替 @RequestMapping(value = "/users",method = RequestMethod.GET)
    public String getAll(){
        System.out.println("user selectAll");
        return "{'module':'user selectAll'}";
    }
}

三种获取参数值的方式

​编辑

\