SpringBoot之RESTful风格
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; //以下注解在前面我的博客已经做出了详细说明 |
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风格的更多相关文章
- java框架之SpringBoot(6)-Restful风格的CRUD示例
准备 环境 IDE:Idea SpringBoot版本:1.5.19 UI:BootStrap 4 模板引擎:thymeleaf 3 效果:Restful 风格 CRUD 功能的 Demo 依赖 &l ...
- 使用SpringBoot编写Restful风格接口
一.简介 Restful是一种对url进行规范的编码风格,通常一个网址对应一个资源,访问形式类似http://xxx.com/xx/{id}/{id}. 举个栗子,当我们在某购物网站上买手机时会 ...
- Eclipse下利用Maven创建SpringBoot的Restful风格程序
参考文章:https://spring.io/guides/gs/rest-service/ 中文翻译:https://blog.dubby.cn/detail.html?id=9040 1.目标是什 ...
- SpringBoot之RESTFul风格的接口调用(jQuery-Ajax)
一.Get $.ajax({ type: "get", url: "url地址", async: true, dataType:"json" ...
- springboot的restful风格获取请求中携带的参数
http://localhost:8080/emp/1 有以上请求,我们controller要怎么获取请求中传递的参数1呢? 通过PathVariable注解,如下: @DeleteMapping(& ...
- SpringBoot实战(一)之构建RestFul风格
RestFul风格是一种非常流行的架构风格,相关实战可以参考我的这篇博客:SSM框架之RestFul示例 论文可参考:https://www.ics.uci.edu/~fielding/pubs/di ...
- SpringBoot整合Redis使用Restful风格实现CRUD功能
前言 本篇文章主要介绍的是SpringBoot整合Redis,使用Restful风格实现的CRUD功能. Redis 介绍 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-valu ...
- SpringBoot RestFul风格API接口开发
本文介绍在使用springBoot如何进行Restful Api接口的开发及相关注解已经参数传递如何处理. 一.概念: REST全称是Representational State Transfer,中 ...
- 使用RESTful风格整合springboot+mybatis
说明: 本文是springboot和mybatis的整合,Controller层使用的是RESTful风格,数据连接池使用的是c3p0,通过postman进行测试 项目结构如下: 1.引入pom.xm ...
随机推荐
- 面试官都叫好的Synchronized底层实现,这工资开多少一个月?
本文为死磕Synchronized底层实现第三篇文章,内容为重量级锁实现. 本系列文章将对HotSpot的synchronized锁实现进行全面分析,内容包括偏向锁.轻量级锁.重量级锁的加锁.解锁.锁 ...
- .NET开发框架(六)-架构设计之IIS负载均衡(视频)
前面有关注我们公众号文章的朋友应该都知道,我们的分布式应用服务可以通过Ocelot网关进行负载均衡,这种方式属于应用级别的实现. 而今天我们给大家介绍的是平台级别的实现,并且我们首次使用视频方式进行讲 ...
- php CI如何实现全站静态生成html,动态创建目录
php CI如何实现全站静态生成html,动态创建目录CodeIgniter框架生成HTML的方法 public function out_html($code) { $data['articles' ...
- 常用的linux命令大全
之前做过两年的运维,用过很多命令,深切体会到某些linux命令熟练掌握后对效率提升有多大.举个简单的例子,在做了研发后经常会有跑一些数据,对于结果数据的处理,我们的产品同学一般都习惯于用excel做统 ...
- Linux kernel workqueue机制分析【转】
转自:http://www.linuxsir.org/linuxjcjs/15346.html 在内核编程中,workqueue机制是最常用的异步处理方式.本文主要基于linux kernel 3.1 ...
- Python元组与字符串操作(8)——三数排序多种实现
练习 依次接收用户输入的3个数,排序后打印 1.转换int后,判断大小排序,使用分支结构完成 num1 = [] for i in range(3): num1.append(int(input('& ...
- Ubuntu 16.04/18.04 安装Shutter截图工具
在安装Shutter软件之前,先安装依赖包,下载地址: 1.libgoocanvas-common_1.0.0-1_all.deb http://launchpadli ...
- infinity新标签页失效
安装infinity新标签页后,无法生效,或者infinity新标签页突然失效了. 驱动精灵的问题 驱动精灵安装后,还会安装其它的软件,在卸载后,安装的软件还存在. 解决办法 光卸载驱动精灵还不够,还 ...
- AssetBundleMaster_ReadMe_CN
在开始使用之前, 建议先导入到一个空的工程里, 通过ReadMe的一步步引导使你对整个框架以及文件结构进行熟悉, 之后再考虑导入到现有工程中使用, 完整看完教程大概需要2个小时左右. 先看看文件夹结构 ...
- gulp中常用的模块
gulp-cssmin: css压缩 gulp-uglify: js压缩混淆 gulp-imagemin: 图片压缩 gulp-htmlmin: html压缩 gulp-concat: 文件合并 g ...