栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot基础(12):RESTful代码进化

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot基础(12):RESTful代码进化

文章目录
  • 前言
  • REST风格
  • 入门案例
    • 案例
    • 案例的解析
  • RESTful快速开发

前言

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

REST风格
  • 传统风格资源描述形式
    http://localhost/user/getById?id=1
    http://localhost/user/saveUser

  • REST 风格描述形式
    http://localhost/user/1

  • 优点:

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

按照REST风格访问资源时使用行为动作区分对资源进行了何种操作

  • http://localhost/users 查询全部用户信息(GET)
  • http://localhost/users/1 查询制定用户信息(GET)
  • http://localhost/users/ 添加用户信息(POST)
  • http://localhost/users 修改用户信息(PUT)
  • http://localhost/users/1 删除用户信息(DELETE)
  • 根据REST风格对资源进行访问称为RESTful

    上述行为时约定方式,约定不是规范,可以打破,所以称REST风格,而不是REST规范,描述模块的名称通常使用复数,也就是加s的格式描述,表示此类资源,而非单个资源。
入门案例 案例

传统的controller写法

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

    @RequestMapping("/save")
    @ResponseBody
    public String save(@RequestBody User user) {
        System.out.println("save " + user);
        return "{'module':'save'}";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(Integer id) {
        System.out.println("delete " + id);
        return "{'module':'delete'}";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(@RequestBody User user) {
        System.out.println("update  " + user);
        return "{'module':'update'}";
    }

    @RequestMapping("/getById")
    @ResponseBody
    public String getById(Integer id) {
        System.out.println("getById  " + id);
        return "{'module':'getById'}";
    }

    @RequestMapping("/getAll")
    @ResponseBody
    public String getAll() {
        System.out.println("getAll  ");
        return "{'module':'getAll'}";
    }

}

此时设置其访问路径和访问方法

import com.it2.springbootrestdemo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class UserController {


    @RequestMapping(value="/user",method = RequestMethod.POST)
    @ResponseBody
    public String save(@RequestBody User user) {
        System.out.println("save " + user);
        return "{'module':'save'}";
    }

    @RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id) {
        System.out.println("delete " + id);
        return "{'module':'delete'}";
    }

    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    @ResponseBody
    public String update(@RequestBody User user) {
        System.out.println("update  " + user);
        return "{'module':'update'}";
    }

    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String getById(@PathVariable Integer id) {
        System.out.println("getById  " + id);
        return "{'module':'getById'}";
    }

    @RequestMapping(value = "/user",method = RequestMethod.GET)
    @ResponseBody
    public String getAll() {
        System.out.println("getAll  ");
        return "{'module':'getAll'}";
    }

}

使用postman发起请求测试

请求的链接中不包含业务方法名,只在RequestMethod带入方法。
其他几个方法类似。

路径上的参数

案例的解析
  • 名称:@RequestMapping
  • 类型:方法注解
  • 位置:SpringMVC控制器方法定义上方
  • 作用:设置当前控制器方法请求访问路径
  • 范例: value 请求路径,method:http请求动作
 @RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id) {
        System.out.println("delete " + id);
        return "{'module':'delete'}";
    }
  • 名称:@PathVariable
  • 类型: 形参注解
  • 位置: SpringMVC控制器方法形参定义前面
  • 作用: 绑定路径参数与处理器方法形参间的关系,要求路径参数名与形参对应
@RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id) {
        System.out.println("delete " + id);
        return "{'module':'delete'}";
    }

@RequestBody @RequestParam @PathVariable

区别:

  • @RequestParam用于接收url地址传参或者表单传参
  • @RequestBody用于接收json数据
  • @PathVariable用于接收路径参数,例如/users/{id}

应用:

  • 开发中,发送请求参数超过1个时,以json格式为主,@RequestBody应用较广
  • 如果发送非json格式数据时,选用@RequestParam接收请求参数
  • 采用RESTful进行开发,当参数较少时,可以采用@PathVariable请求接收路径变量,通常用于传递id
RESTful快速开发

前面我们的代码需要在每一个方法前添加@RequestBody,比较重复繁琐。

修改后,此时将**@RequestBody和@RequestMapping**移到类名前。

此时我们可以使用**@RestController**替代@Controller和@ResponseBody,

就变成如下代码了

**@RequestMapping(method = RequestMethod.POST)**明显还可以修改,于是就变成了RESTful风格的代码。
@RequestMapping(method = RequestMethod.POST) >> @PostMapping
@RequestMapping(value = “/{id}”,method = RequestMethod.DELETE) >> @DeleteMapping(“/{id}”)
@RequestMapping(method = RequestMethod.PUT) >> @PutMapping

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/959697.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号