SpringBoot之RESTful风格

1、RESTful介绍

RESTful是一种软件架构风格,一种时尚!

RESTful架构风格规定,数据的元操作,即CRUD(create, read, update和delete,即数据的增删查改)操作,分别对应于HTTP方法:

GET用来获取资源(查询),

POST用来新建资源(添加),

PUT用来更新资源(修改),

DELETE用来删除资源(删除),

这样就统一了数据操作的接口,仅通过HTTP方法,就可以完成对数据的所有增删查改工作

2、RESTful接口设计

在此我们以用户数据的基本操作来进行接口设计

HTTP协议请求方法

SpringBoot注解

URL

功能说明

POST

@PostMapping

/users

创建一个用户

GET

@GetMapping

/users

查询用户列表

GET

@GetMapping

/users/id

根据id查询一个用户

PUT

@PutMapping

/users/id

根据id更新一个用户

DELETE

@DeleteMapping

/users/id

根据id删除一个用户

3、用户实体bean创建

package com.offcn.po;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

//以下注解在前面我的博客已经做出了详细说明
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Long id;
    private String name;
    private Integer age;  
}

4、创建Controller  UserController

package com.offcn.controllerold;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.PutMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.offcn.po.User;

@RestController

@RequestMapping("/users-test")

public class UserController {

//Collections.synchronizedList线程安全

private List<User> listUser=Collections.synchronizedList(new ArrayList<User>());

/***

* 获取全部用户信息

* @return

*/

@GetMapping("/")

public List<User> getUserList(){

return listUser;

}

/***

* 新增用户

* @param user

* @return

*/

@PostMapping("/")

public String createUser(User user) {

listUser.add(user);

return "success";

}

/***

* 获取指定id用户信息

* @param id

* @return

*/

@GetMapping("/{id}")

public User getUser(@PathVariable("id")Long id) {

for (User user : listUser) {

if(user.getId()==id) {

return user;

}

}

return null;

}

/**

* 更新指定id用户信息

* @param id

* @param user

* @return

*/

@PutMapping("/{id}")

public String updateUser(@PathVariable("id") Long id,User user) {

for (User user2 : listUser) {

if(user2.getId()==id) {

user2.setName(user.getName());

user2.setAge(user.getAge());

}

}

return "success";

}

/***

* 删除指定id用户

* @param id

* @return

*/

@DeleteMapping("/{id}")

public String deleteUser(@PathVariable("id") Long id) {

listUser.remove(getUser(id));

return "success";

}

}

5、Postman测试RESTful接口

(1)、新增用户

post      http://localhost:8080/users/

(2)、获取全部用户信息

get      http://localhost:8080/users/

(3)、获取指定id用户信息

get      http://localhost:8080/users/id

(4)、更新指定id用户信息

put      http://localhost:8080/users/id

(5)、删除指定id用户信息

delete      http://localhost:8080/users/id

SpringBoot之RESTful风格的更多相关文章

  1. java框架之SpringBoot(6)-Restful风格的CRUD示例

    准备 环境 IDE:Idea SpringBoot版本:1.5.19 UI:BootStrap 4 模板引擎:thymeleaf 3 效果:Restful 风格 CRUD 功能的 Demo 依赖 &l ...

  2. 使用SpringBoot编写Restful风格接口

    一.简介    Restful是一种对url进行规范的编码风格,通常一个网址对应一个资源,访问形式类似http://xxx.com/xx/{id}/{id}. 举个栗子,当我们在某购物网站上买手机时会 ...

  3. Eclipse下利用Maven创建SpringBoot的Restful风格程序

    参考文章:https://spring.io/guides/gs/rest-service/ 中文翻译:https://blog.dubby.cn/detail.html?id=9040 1.目标是什 ...

  4. SpringBoot之RESTFul风格的接口调用(jQuery-Ajax)

    一.Get $.ajax({ type: "get", url: "url地址", async: true, dataType:"json" ...

  5. springboot的restful风格获取请求中携带的参数

    http://localhost:8080/emp/1 有以上请求,我们controller要怎么获取请求中传递的参数1呢? 通过PathVariable注解,如下: @DeleteMapping(& ...

  6. SpringBoot实战(一)之构建RestFul风格

    RestFul风格是一种非常流行的架构风格,相关实战可以参考我的这篇博客:SSM框架之RestFul示例 论文可参考:https://www.ics.uci.edu/~fielding/pubs/di ...

  7. SpringBoot整合Redis使用Restful风格实现CRUD功能

    前言 本篇文章主要介绍的是SpringBoot整合Redis,使用Restful风格实现的CRUD功能. Redis 介绍 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-valu ...

  8. SpringBoot RestFul风格API接口开发

    本文介绍在使用springBoot如何进行Restful Api接口的开发及相关注解已经参数传递如何处理. 一.概念: REST全称是Representational State Transfer,中 ...

  9. 使用RESTful风格整合springboot+mybatis

    说明: 本文是springboot和mybatis的整合,Controller层使用的是RESTful风格,数据连接池使用的是c3p0,通过postman进行测试 项目结构如下: 1.引入pom.xm ...

随机推荐

  1. Python - 标准库概况 - 第二十一天

    Python 标准库概览 操作系统接口 os模块提供了不少与操作系统相关联的函数. 建议使用 "import os" 风格而非 "from os import *&quo ...

  2. java开发高校社团管理系统JSP

    运行环境JDK1.7编写Eclipse( Neon.3 Release (4.6.3)) windows下Tomcat v8.5Mysql 5.5使用技术Java servlet & jspM ...

  3. js中this关键字的作用

    this中的几种情况 1.普通函数中的this window 2.构造函数中的this 是当前构造函数创建的对象在new这个构造函数的时候会在内存中创建一个对象,此时会让this指向刚创建好的这个对象 ...

  4. 虚拟机中安装Kali遇到的问题及解决方法

    title: 虚拟机中安装Kali遇到的问题及解决方法 date: 2018-11-25 12:25:43 tags: 安全 --- 关于Kali版本选择 kail官方下载页面 虚拟机中当然就下载虚拟 ...

  5. shell编写for例子

    1.批量打包sh文件 #!/bin/bash -name "*.sh"` do tar -czvf $i.tgz $i done 2.批量解压文件 #!/bin/bash -nam ...

  6. Ubuntu安装MDK

    1 环境部署 [x] Ubuntu 18.04 [x] Wine 3.0.4 1.0 查看CPU信息 lscpu 序号 属性 描述 1 架构 x86_64 2 CPU 运行模式 32-bit, 64- ...

  7. 在linux下配置jupyter notebook,本地浏览器访问

    1.安装jupyter notebook pip install jupyter 2.生成配置文件 jupyter notebook --generate-config 3.设置登陆密码并生成秘钥 s ...

  8. google v8

    https://github.com/tongbai168/v8 https://iwebing.lofter.com/tag/chromium  编译动态库 gyp mylib.gyp --dept ...

  9. luoguP1198 [JSOI2008]最大数

    https://www.luogu.org/problem/P1198 update!!! 经过老师的讲解,惊人的发现这题有用更简单数据结构维护的解法,而越简单的数据结构(如果能够用的话),越好(实现 ...

  10. 《快活帮》第七次作业:团队项目设计完善&编码

    项目 内容 这个作业属于哪个课程 2016计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十一 团队作业7-团队项目设计完善&编码 团队名称 快活帮 作业学习目标 掌握 ...