您现在的位置是:首页 > 编程 > 

restful风格快速入门

2025-07-21 11:11:57
restful风格快速入门 restful风格入门概述简介:REST(Representational State Transfer),表现形式状态转换。 传统风格 http://localhost/users/getById?id=1 http://localhost/users/saveUser REST风格 http://localhost/users/1 http://loca

restful风格快速入门

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=".0.0" xmlns:xsi=";
         xsi:schemaLocation=".0.0 .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></groupId>
    <artifactId>SpringBootRestful</artifactId>
    <version>0.0.1-SAPSHOT</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 .springbootrestful.bean;

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

import .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.RespeBody;

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

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

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

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

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

运行演示

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

import .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)
    @RespeBody
    public String save(@RequestBody User user){
        println("user save..." + user);
        return "{'module':'user save'}";
    }

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

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

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

    @RequestMapping(value = "/users",method = RequestMethod.GET)
    @RespeBody
    public String getAll(){
        println("user getAll...");
        return "{'module':'user getAll'}";
    }
}
完善
代码语言:javascript代码运行次数:0运行复制
package .;

import .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){
        println("user save..." + user);
        return "{'module':'user save'}";
    }

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

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

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

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

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1149804.html

相关标签:无
上传时间: 2025-07-19 20:33:13
留言与评论(共有 10 条评论)
本站网友 溢价发行
30分钟前 发表
但是了太长了写起来不方便
本站网友 不动产统一登记制度
27分钟前 发表
//localhost/users 删除用户信息 DELETE(删除)规范 REST风格更多的是一种规范
本站网友 烟台婚纱摄影
6分钟前 发表
//localhost/users/1 http
本站网友 北京4号地铁
17分钟前 发表
分享自作者个人站点/博客
本站网友 小米手环4
10分钟前 发表
'user getById'}"; } @GetMapping public String getAll(){ println("user getAll..."); return "{'module'
本站网友 文件夹权限
4分钟前 发表
如有侵权请联系 cloudcommunity@tencent 删除前往查看注解publicrestful函数入门
本站网友 横岗
4分钟前 发表
'user save'}"; } @DeleteMapping public String delete(@PathVariable Integer id){ println("user delete..." + id); return "{'module'
本站网友 一刀切
13分钟前 发表
//localhost/users/getById?id=1 http
本站网友 花样年花港
1分钟前 发表
method = RequestMethod.POST) @RespeBody public String save(@RequestBody User user){ println("user save..." + user); return "{'module'