首页 > 其他分享 >springMVC08(REST风格的“入门案例”)

springMVC08(REST风格的“入门案例”)

时间:2022-12-15 23:44:23浏览次数:36  
标签:... users springMVC08 REST User user value id 入门

一、用REST风格,来演示"增、删、改、查"操作。

1.1:增POST

1.1.1:用PostMan测试:

增POST:代码块
 @RequestMapping(value = "/users", method = RequestMethod.POST)
    @ResponseBody
    public String save() {
        System.out.println("user save...");
        return "module: User save...";
    }

1.2:删DELETE:

(删是有"参数"的,比较复杂。)

(解析:1-需要在"行参"里面加上:@PathVariable)

(解析:2-这样"行参"的id就可以和:"value = "/users/{id}"的id联系起来)

(解析:3-联系图片进行"理解")

1.2.1:用PostMan测试:

删DELETE:代码块
 @RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id) { //形参里面加上@PathVariable注解,和"/users{id}"呼应
        System.out.println("user delete..." + id);
        return "module: User delete...";
    }

1.3:改PUT:(PUT复杂的是:传参)

(改是有"参数"的,而且参数是"实体类",比较复杂)

(解析:1-需要在"行参"里面加上:@RequestBody,加上这个才可以传入一个"实力类")

(解析:2-"value = "/users", method = RequestMethod.PUT"这个不需要参数,因为"实体类",传入的数据需要是"JSON"数据,在PostMan进行)

(解析:3-联系图片进行"理解")

1.3.1:用PostMan测试:

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

1.4:查GET

(思路和操作和上面一样,这边不多做解释)

1.4.1:PostMan测试(按Id查):

1.4.2:PostMan测试(全查):

查GET:代码块(按id查、全查)
 @RequestMapping(value = "/users/{id}",method = RequestMethod.GET) //GET是用来做查询的
    @ResponseBody
    public String getById(@PathVariable Integer id) {
        System.out.println("user getById..." + id);
        return "module: User getById...";
    }

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

二、总结:

//-------------------------------------------------------------------
    //REST风格


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

    //删
    @RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id) { //形参里面加上@PathVariable注解,和"/users{id}"呼应
        System.out.println("user delete..." + id);
        return "module: User delete...";
    }

    /*
    --> 上面传入单个参数
    传入"单个参数"和"传入一个类"写法是不一样的。
    --> 下面传入一个"类",PostMan传参不一样
     */

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


    //查
    @RequestMapping(value = "/users/{id}",method = RequestMethod.GET) //GET是用来做查询的
    @ResponseBody
    public String getById(@PathVariable Integer id) {
        System.out.println("user getById..." + id);
        return "module: User getById...";
    }

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

标签:...,users,springMVC08,REST,User,user,value,id,入门
From: https://www.cnblogs.com/chen-zhou1027/p/16986263.html

相关文章

  • 医学图像分析的入门说明 An introductory illustration of medical image analysis
    医学图像分析的入门说明Anintroductoryillustrationofmedicalimageanalysis内容1。介绍12岁。医学图像分析:问题和挑战23。所有贡献章节的摘要(挑战和调查结果)54。与......
  • IDEA入门级使用教程----你怎么还在用eclipse?
    上个月,idea的使用量超越eclipse的消息席卷了整个IT界,idea到底好在哪里呢?最智能的IDEIDEA相对于eclipse来说最大的优点就是它比eclipse聪明。聪明到什么程度呢?我们先来看几个......
  • c语言入门这一篇就够了-学习笔记
    c语言入门C语言一经出现就以其功能丰富、表达能力强、灵活方便、应用面广等特点迅速在全世界普及和推广。C语言不但执行效率高而且可移植性好,可以用来开发应用软件、驱动、......
  • Rancher入门实战
    强大多云混合多K8S集群管理平台Rancher入门实战 @目录概述定义为何使用其他产品安装简述规划基础环境Docker安装Rancher安装创建用户创建集群添......
  • Isolation forest阅读笔记
    IForest所基于的假设异常是由较少实例组成的少数派它们拥有与正常实例差别较大的属性换句话说,异常是少而不同的(fewanddifferent),这使得它们比正常的点更容易被孤立......
  • 【Dubbo】快速入门案例以及配置详情
    文章目录​​1、Dubbo的前世今生​​​​2、Dubbo的快速入门​​​​2.1、Dubbo的基本架构​​​​2.2、Nacos​​​​2.3、管理后台​​​​2.4、入门案例​​​​2.5、代......
  • 【在线教育】EasyExcel入门
    文章目录​​1.EasyExcel入门​​​​1.1EasyExcel概述​​​​1.2EasyExcel特点​​​​1.3环境搭建​​​​1.3.1测试父项目​​​​1.3.2测试excel项目​​​​1......
  • Spring Security 安全框架入门原理及实战
    SpringSecurity入门原理及实战在web应用开发中,安全无疑是十分重要的,选择SpringSecurity来保护web应用是一个非常好的选择。SpringSecurity是spring项目之中的一个安全......
  • springMVC07(REST风格)
    一、REST风格的解释:(资源的访问形式)二、总结:2.1-REST:资源访问形式2.2-4个动作(GET、POST、PUT、DELETE)2.3-用"REST"风格开发,我们就叫"RESTful"......
  • openresty package path
    openrestylua_package_path是整个openresty最基础的功能,不理解path就无法做项目,更无法写框架。先看下文档lua_package_pathhttps://github.com/openresty/lua-nginx-m......