Day06_13_SpringBoot教程之实现Restful风格API

Day06_13_SpringBoot教程之实现Restful风格API

一.Restful简介

1. Restful概述

Restful是一种软件架构风格,它是一种设计风格而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类型的软件开发中,基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制.

在服务器端,应用程序状态和功能可以分为各种资源.资源是一个有趣的概念实体,它向客户端公开.资源的例子有:应用程序对象、数据库记录、算法等等.每个资源都可以使用 URI (Universal Resource Identifier) 得到一个唯一的地址,所有资源都共享统一的接口,以便在客户端和服务器之间传输状态.使用的是标准的 HTTP 方法,比如 GET、PUT、POST 和 DELETE

2. Rest常用请求方式

在基于Rest的设计中,使用一组通用的动词来操作资源:

  • 创建资源: 应使用HTTP POST;
  • 检索资源: 应该使用HTTP GET;
  • 更新资源: 应使用HTTP PUT;
  • 删除资源: 应该使用HTTP DELETE.

二. SpringBoot搭建 RESTful风格的API接口实现步骤

1. 创建一个新的模块--boot_restful

2. pom.xml中添加依赖包

<dependencies>
    <!--web启动器是与SpringMVC相关的依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--thymeleaf的依赖包-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <!--为了解决thymeleaf模板中,对html标签要求太严格的问题!-->
    <dependency>
        <groupId>net.sourceforge.nekohtml</groupId>
        <artifactId>nekohtml</artifactId>
        <version>1.9.22</version>
    </dependency>

    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.20</version>
    </dependency>

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

3. 创建一个pojo类--User

package com.syc.boot.domain;
import lombok.Data;
import java.io.Serializable;

@Data
public class User implements Serializable {

    private Long id;
    private String name;
    private Integer age;

}

4. 创建Controller接口方法

package com.syc.boot.web;
import com.syc.boot.domain.User;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
// 通过这里配置使下面的映射都在/users下
@RequestMapping(value="/users")
public class UserController {

    // 创建线程安全的Map
    static Map<Long, User> users = Collections.synchronizedMap(new HashMap<>());

    // 处理GET请求,用来获取用户列表.
    // 可以通过@RequestParam获取从页面中传递进来的查询条件或者翻页信息等参数.
    @RequestMapping(value="/", method=RequestMethod.GET)
    public List<User> getUserList() {
        return new ArrayList<>(users.values());
    }

    // 处理POST请求,用来创建User.
    // 除了@ModelAttribute绑定参数之外,还可以通过@RequestParam从页面中传递参数.
    @RequestMapping(value="/", method=RequestMethod.POST)
    public String addUser(@RequestBody User user) {
        users.put(user.getId(), user);
        System.out.println("user==="+users.get(user.getId()));
        return "success";
    }

    // 处理GET请求,用来获取url中id值的User信息;
    // url中的id可通过@PathVariable绑定到函数的参数中.
    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public User getUser(@PathVariable Long id) {
        return users.get(id);
    }

    // 处理PUT请求,用来更新User信息.
    @RequestMapping(value="/{id}", method=RequestMethod.PUT)
    public String updateUser(@PathVariable Long id, @RequestBody User user) {
        User u = users.get(id);
        u.setName(user.getName());
        u.setAge(user.getAge());
        users.put(id, u);
        return "success";
    }

    // 处理DELETE请求,用来删除User
    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public String deleteUser(@PathVariable Long id) {
        users.remove(id);
        return "success";
    }

}

5. 创建应用程序入口类

package com.syc.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 *
 */
@SpringBootApplication
public class RestWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(RestWebApplication.class, args);
    }
}

三. 运行测试

1. 添加一个新用户

执行post请求,传递json格式的参数.

2. 查询全部用户

执行get请求,查询刚才添加的用户,不需要传递任何参数.

3. 更新用户信息

执行put请求,传递id作为参数.

查询更新后的结果

4. 删除用户

执行delete请求,删除id为1的用户

查询删除后的结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一一哥Sun

您的鼓励是我继续创作的动力哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值