restful风格快速入门

简介: 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

<?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

package com.example.springbootrestful.bean;
public class User {
    private Integer id;
}

Controller

传统代码
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风格代码
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'}";
    }
}
完善
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'}";
    }
}


相关文章
|
JSON 网络协议 API
|
7天前
|
JSON Java API
如何设计可扩展的RESTful API?
如何设计可扩展的RESTful API?
|
15天前
|
JavaScript API 开发者
GraphQL API开发入门:比RESTful更高效的数据查询方式
**GraphQL API开发入门摘要** GraphQL是一种更高效的数据查询方式,解决RESTful API的过度或不足获取数据问题。它允许客户端按需获取数据,减少网络传输,支持一次请求获取多资源。强类型和自描述特性方便了开发。文章通过一个简单的Node.js示例,展示如何使用`apollo-server-express`搭建GraphQL服务器,包括定义Schema、实现Resolver和创建服务器。通过测试,显示了GraphQL如何提供精确数据和优化查询效率。对于复杂数据需求,GraphQL是现代API设计的有效选择。
23 0
|
1天前
|
安全 前端开发 API
深入理解RESTful API设计原则与实践
【7月更文挑战第14天】在数字化时代的浪潮中,后端开发扮演着至关重要的角色,而RESTful API作为现代Web服务通信的基石,其设计质量直接影响到应用的性能与可维护性。本文将通过剖析RESTful API的核心设计原则,结合实际案例,探讨如何构建高效、易于理解和扩展的API。我们将从资源定位、请求方法、状态码使用等方面入手,揭示良好API设计的秘诀,并分享一些最佳实践和常见陷阱,旨在指导开发者打造更加健壮和友好的后端服务接口。
|
8天前
|
JSON Java API
如何设计可扩展的RESTful API?
如何设计可扩展的RESTful API?
|
15天前
|
Java API 开发者
RESTful API设计与实现:Java开发者指南
RESTful API设计与实现:Java开发者指南
|
12天前
|
XML Java API
使用Java构建RESTful API的最佳实践
使用Java构建RESTful API的最佳实践
|
3天前
|
XML 缓存 API
深入理解RESTful API设计原则与最佳实践
【7月更文挑战第12天】本文将探索RESTful API设计的核心原则和实用的最佳实践。我们将从REST的基本概念入手,逐步深入到API设计的高级话题,如版本控制、状态码的正确使用以及如何提高API的安全性。此外,我们还将探讨一些常见的设计挑战和解决方案,以帮助开发者构建更加健壮、可维护和用户友好的后端服务。
|
7天前
|
Java API 数据库
使用Spring Boot构建RESTful API
使用Spring Boot构建RESTful API
|
8天前
|
开发框架 Java API
使用Spring Boot构建RESTful API的最佳实践
使用Spring Boot构建RESTful API的最佳实践