前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >restful风格快速入门

restful风格快速入门

作者头像
GeekLiHua
发布2025-01-21 16:19:02
发布2025-01-21 16:19:02
10400
代码可运行
举报
文章被收录于专栏:JavaJava
运行总次数:0
代码可运行

restful风格入门

概述

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

  • 传统风格 http://localhost/users/getById?id=1 http://localhost/users/saveUser
  • REST风格 http://localhost/users/1 http://localhost/users

对比: 对比可以发现,虽然传统风格的更详细,知道调用哪个函数,但是了太长了写起来不方便,REST风格的话就很简介,而且还可以隐藏函数调用的函数的名字,无法通过地址得知对资源进行何种操作,更加安全。

如何区分资源访问形式 在后面的代码演示的时候就会知道,REST风格定义了很多种类的Mapping注解,通过这些注解进行识别。

  • http://localhost/users 查询全部用户信息 GET(查询)
  • http://localhost/users/1 查询指定用户信息 GET(查询)
  • http://localhost/users 添加用户信息 POST(添加/保存)
  • http://localhost/users 修改用户信息 PUT(修改/更新)
  • http://localhost/users 删除用户信息 DELETE(删除)

规范 REST风格更多的是一种规范,对于描述模块名称通常使用复数,比如users

快速入门

文件结构

pom.xml

代码语言:javascript
代码运行次数:0
运行
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>SpringBootRestful</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBootRestful</name>
    <description>SpringBootRestful</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
User
代码语言:javascript
代码运行次数:0
运行
复制
package com.example.springbootrestful.bean;

public class User {
    private Integer id;
}
Controller
传统代码
代码语言:javascript
代码运行次数:0
运行
复制
package com.example.springbootrestful.controller;

import com.example.springbootrestful.bean.User;
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 {
    // save与update需要的是post方法才可以访问
    @RequestMapping("/save")
    @ResponseBody
    public String save(@RequestBody User user){
        System.out.println("user save..." + user);
        return "{'module':'user save'}";
    }

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

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

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

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

运行演示

RESTful风格代码
代码语言:javascript
代码运行次数:0
运行
复制
package com.example.springbootrestful.controller;

import com.example.springbootrestful.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class UserController {
    // save与update需要的是post方法才可以访问
    @RequestMapping(value = "/users",method = RequestMethod.POST)
    @ResponseBody
    public String save(@RequestBody User user){
        System.out.println("user save..." + user);
        return "{'module':'user save'}";
    }

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

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

    @RequestMapping(value = "/users/{id}",method = RequestMethod.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'}";
    }
}
完善
代码语言:javascript
代码运行次数:0
运行
复制
package com.example.springbootrestful.controller;

import com.example.springbootrestful.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/Users")
public class UserController {
    // save与update需要的是post方法才可以访问
    @PostMapping
    public String save(@RequestBody User user){
        System.out.println("user save..." + user);
        return "{'module':'user save'}";
    }

    @DeleteMapping
    public String delete(@PathVariable Integer id){
        System.out.println("user delete..." + id);
        return "{'module':'user delete'}";
    }

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

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

    @GetMapping
    public String getAll(){
        System.out.println("user getAll...");
        return "{'module':'user getAll'}";
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-01-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • restful风格入门
    • 概述
    • 快速入门
      • 文件结构
      • User
      • Controller
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档