1.说明

基于已经创建好的Spring Boot工程,
开发Restful风格的接口,
并且对外提供HTTP服务。

Spring Boot工程创建方式有两种:
Maven向导方式:SpringBoot集成Maven工程
Spring Boot向导方式:SpringBoot集成MyBatis-Plus框架详细方法

2.示例说明

有一个用户对象需要操作,
用户有ID,名称,生日,电子邮箱等属性,
需要对外提供用户的基本增删改查操作。

3.新建实体类

用户实体类User.java:

package com.yuwen.spring.demo.entity;import java.util.Date;public class User {private Long id;private String name;private Date birthday;private String email;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + ", birthday=" + birthday + ", email=" + email + "]";}
}

4.新建接口类

用户接口类UserController.java

package com.yuwen.spring.demo.controller;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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.yuwen.spring.demo.entity.User;/*** 用户的增删改查相关接口*/
@RequestMapping("user")
public interface UserController {/*** 创建用户*/@PostMappingvoid createUser(@RequestBody User user);/*** 更新用户*/@PutMapping("{id}")void upadteUser(@PathVariable Long id, @RequestBody User user);/*** 删除用户*/@DeleteMapping("{id}")void deleteUser(@PathVariable("id") Long id);/*** 查询指定用户*/@GetMapping("one/{id}")User getOneUser(@PathVariable Long id);/*** 查询所有用户,支持分页*/@GetMapping("all")List<User> getAllUser(@RequestParam(required = false) Integer pageNum,@RequestParam(required = false) Integer pageSize);}

5.新建接口实现类

用户接口实现类UserControllerImpl.java,
为了方便演示,简化了后台逻辑:

package com.yuwen.spring.demo.controller.impl;import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.springframework.web.bind.annotation.RestController;
import com.yuwen.spring.demo.controller.UserController;
import com.yuwen.spring.demo.entity.User;@RestController
public class UserControllerImpl implements UserController {@Overridepublic void createUser(User user) {System.out.println("createUser, user=" + user);}@Overridepublic void upadteUser(Long id, User user) {System.out.println("upadteUser, id=" + id + ", user=" + user);}@Overridepublic void deleteUser(Long id) {System.out.println("deleteUser, id=" + id);}@Overridepublic User getOneUser(Long id) {System.out.println("getOneUser, id=" + id);User user = new User();user.setId(id);return user;}@Overridepublic List<User> getAllUser(Integer pageNum, Integer pageSize) {System.out.println("getAllUser, pageNum=" + pageNum + ", pageSize=" + pageSize);User user = new User();user.setId(999L);user.setName("all");user.setBirthday(new Date());return Collections.singletonList(user);}
}

注意在实现类上面增加@RestController注解,
表示对外提供Restful服务的类,
而且该注解必须配置在实现类上面,
加到接口上面是不生效的。

6.启动Spring Boot服务

右键主启动类DemoApplication.java,
Run As ... -> Java Application
成功启动日志中没有报错,
对外提供的服务端口是8088。

7.创建用户

使用HTTP工具调用创建用户接口:

HTTP Method: POST
HTTP URL   : http://localhost:8088/user
HTTP RequestBody:
{"id" : 1,"name" : "tom","birthday" : "2021-03-25T09:21:45.061+00:00","email" : "tom@ai.com"
}

控制台输出:

createUser, user=User [id=1, name=tom, birthday=Thu Mar 25 17:21:45 GMT+08:00 2021, email=tom@ai.com]

8.更新用户

使用HTTP工具调用更新用户接口:

HTTP Method: PUT
HTTP URL   : http://localhost:8088/user/1
HTTP RequestBody:
{"id" : 1,"name" : "tom2","birthday" : "2021-03-25T09:21:45.061+00:00","email" : "tom2@ai.com"
}

控制台输出:

upadteUser, id=1, user=User [id=1, name=tom2, birthday=Thu Mar 25 17:21:45 GMT+08:00 2021, email=tom2@ai.com]

9.删除用户

使用HTTP工具调用删除用户接口:

HTTP Method: DELETE
HTTP URL   : http://localhost:8088/user/1

控制台输出:

deleteUser, id=1

10.查询指定用户

使用HTTP工具调用查询指定用户接口:

HTTP Method: GET
HTTP URL   : http://localhost:8088/user/one/1

工具返回HTTP Response:

{"id" : 1,"name" : null,"birthday" : null,"email" : null
}

控制台输出:

getOneUser, id=1

11.查询所有用户,支持分页

使用HTTP工具调用查询所有用户接口:

HTTP Method: GET
HTTP URL   : http://localhost:8088/user/all?pageNum=1&pageSize=10

工具返回响应:

HTTP Response:
[{"id" : 999,"name" : "all","birthday" : "2021-03-25T09:38:38.093+00:00","email" : null}
]

控制台输出:

getAllUser, pageNum=1, pageSize=10

12.HTTP Method

通过上面的例子,
可以看出HTTP Method与接口中注解的对应关系:
POST <-> @PostMapping
PUT <-> @PutMapping
DELETE <-> @DeleteMapping
GET <-> @GetMapping

13.HTTP URL

主要有如下三种URL格式:http://localhost:8088/userhttp://localhost:8088/user/one/1http://localhost:8088/user/all?pageNum=1&pageSize=10

URL中的固定前缀http://localhost:8088/
http表示传输协议,
localhost表示IP地址,
8088表示服务端口;

13.1

URL中的user对应接口的注解@RequestMapping("user");

13.2

URL中的one/1中:
one/1对应方法的注解@GetMapping("one/{id}");
1对应方法参数的注解@PathVariable Long id,
或者@PathVariable("id") Long id,
注解的参数id不是必填的;

13.3

URL中的?pageNum=1&pageSize=10对应方法参数的注解:
@RequestParam(required = false) Integer pageNum,
@RequestParam(required = false) Integer pageSize;
注解的required属性为false,
表示该参数不是必填的,
注意默认情况下true。

14.HTTP RequestBody

HTTP RequestBody请求参数很简单,
对应应方法参数的注解@RequestBody User user;


http://www.taodudu.cc/news/show-1250962.html

相关文章:

  • Notepad++便签模式
  • SpringBoot集成Cache缓存(Ehcache缓存框架,注解方式)
  • PowerDesigner生成数据库刷库脚本
  • PowerDesigner生成数据库设计文档
  • Eclipse配置国内镜像源
  • PingInfoView批量PING工具
  • Git合并两个不同的仓库
  • Guava事件处理组件Eventbus使用入门
  • Junit4集成到Maven工程
  • Redis集成到Maven工程(Jedis客户端)
  • SpringBoot集成Cache缓存(Redis缓存,RedisTemplate方式)
  • Junit5集成到Maven工程
  • Junit5集成到SpringBoot工程
  • 语言代码表
  • Protobuf生成Java代码(Maven)
  • Protobuf生成Java代码(命令行)
  • Maven查看插件信息
  • SpringBoot脚手架工程快速搭建
  • SpringBoot集成MyBatis-Plus分页插件
  • SNMP客户端工具MIB Browser
  • PowerDesigner运行自定义VBS脚本,复制Name到Comment
  • BitMap-BitSet(JDK1.8)基本使用入门
  • IDEA查看Java类的UML关系图
  • 30. 包含min函数的栈
  • 35. 复杂链表的复制
  • 58 - II. 左旋转字符串
  • 03. 数组中重复的数字
  • 53 - II. 0~n-1中缺失的数字
  • 04. 二维数组中的查找
  • 11. 旋转数组的最小数字

SpringBoot开发Restful接口相关推荐

  1. c#分页_使用Kotlin搭配Springboot开发RESTFul接口(二)自定义配置、跨域、分页

    前言 上一篇文章请看这里:使用Kotlin搭配Springboot开发RESTFul接口与服务部署 上一篇文章介绍了Kotlin搭配Springboot的开发流程,从搭建项目.ORM.Controll ...

  2. SpringBoot开发Restful风格的接口实现CRUD功能

    一.前言 我们都知道SpringBoot的出现使得在开发web项目的时候变得更加方便.快捷.之前写过一篇文章是如何快速搭建一个springboot项目:SpringBoot入门:使用IDEA和Ecli ...

  3. php开发流程 restful,PhpBoot 入门(一) 快速开发 RESTful 接口

    PhpBoot 是一款为快速开发 RESTful API 而设计的PHP框架(更多内容请前往 PbpBoot Github).本文为你演示如何使用 PhpBoot 快速开发一套 RESTful 风格的 ...

  4. springboot建立restful接口

    目录 restful简介 项目创建及运行 总结 restful简介 restful架构简介: RESTful架构是对MVC架构改进后所形成的一种架构,通过使用事先定义好的接口与不同的服务联系起来.在R ...

  5. 【SpringBoot教程】SpringBoot开发HTTP接口GET请求实战

    ⛪ 专栏地址 系列教程更新中

  6. php调用restful接口_如何使用PHP编写RESTful接口

    这是一个轻量级框架,专为快速开发RESTful接口而设计.如果你和我一样,厌倦了使用传统的MVC框架编写微服务或者前后端分离的API接口,受不了为了一个简单接口而做的很多多余的coding(和CTRL ...

  7. php调用restful接口_PHP restful 接口

    首先我们来认识下RESTful Restful是一种设计风格而不是标准,比如一个接口原本是这样的: http://www.test.com/user/view/id/1 表示获取id为1的用户信息,如 ...

  8. 基于SpringBoot开发一个Restful服务,实现增删改查功能

    点击上方"方志朋",选择"置顶公众号" 技术文章第一时间送达! 作者:虚无境 cnblogs.com/xuwujing/p/8260935.html 前言 在去 ...

  9. springboot增删改查案例_大神基于SpringBoot开发一个Restful服务,实现增删改查功能...

    前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...

最新文章

  1. 更大的工字型电感作为150kHz导航信号接收天线
  2. 图解 VS2015 如何打包winform 安装程序
  3. python绘制帕累托图
  4. java dataset类的方法,C#中DataSet转化为实体集合类的方法
  5. 一流设计师导航|16map,一款强大且智能的设计师导航网站
  6. 15 设置系统分词器
  7. python 相对导入_Python相对导入机制详解
  8. python语言中、外部模块先导入、再使用_python引入导入自定义模块和外部文件--转载Sumomo的博客...
  9. 时域有限差分法matlab程序,时域有限差分法的Matlab仿真
  10. 龙果学院Java并发编程原理与实战
  11. zookeeper和ZAB协议
  12. 链游的趋势和前景:团队开始专注于建设 进入 6-12 个月重新整合期
  13. 第三方百度地图-----展示所在位置显示小圆点
  14. 科学家正在尝试取用脂肪细胞3D打印人类心脏
  15. 微信公众号已认证如何修改名字?
  16. Windows10删除windows.edb文件的官方方法
  17. 塔石E18D mqtt连接onenet
  18. 7款可以实现 PDF 转换 Word 格式的免费在线工具
  19. MATLAB 函数求极限,定积分,一阶导,二阶导(经典例题)
  20. 利用标签打印软件批量制作工作证的详细步骤

热门文章

  1. 【BZOJ4008】【HNOI2015】亚瑟王 [期望DP]
  2. 【转载】C++知识库内容精选 尽览所有核心技术点
  3. 同TTX更可爱的层次分析法游戏破解
  4. 闲谈神经网络--写给初学者(三)
  5. Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)
  6. 奋战杭电ACM(DAY10)1015
  7. file标签样式修改
  8. u盘装xp/win7/ubuntu/fedora总结
  9. [MongoDB] MongoDB的安装以及问题
  10. HTMLCSS--使用CSS完成页面布局及排版(附案例代码)