spring boot 学习(三)API注解记录及测试
spring boot API注解记录及测试
部分注解解析
@Controller
: 修饰创建处理 http 处理对象,一般用于页面渲染时使用。@RestController
: Json数据交互; 相当于@Controller
中配置@ResponseBody
来返回 Json数据。@RequestMapping
: 配置映射URL。
关于 @Controller 与 @RestController 的区别:
官方文档:@RestController is a stereotype annotation that combines @ResponseBody and @Controller。
谷歌翻译:@RestController是一个构造型注释,结合了@ResponseBody和@Controller。
1) 使用@RestController注解Controller,配置的视图解析器不起作用,返回为Json数据。(JSON、XML或自定义mediaType内容)
2) 如果需要返回到指定页面,则需要用 @Controller配合视图解析器。
关于 @RequestMapping 的六个属性 :
value:指定请求的实际地址。(value的uri值为三类:具体值、含有某变量的一类值、含正则表达式的一类值)。
method:指定请求的method类型, GET、POST、PUT、DELETE等。
consumes:指定处理请求的提交内容类型(Content-Type),例如application/json, text/html。
produces:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回。
params: 指定request中必须包含某些参数值是,才让该方法处理。
headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。
创建一份简单的API
请求类型 | URL | 功能说明 |
---|---|---|
GET | /users | 查询用户列表 |
POST | /users | 创建一个用户 |
GET | /users/id | 根据id查询一个用户 |
PUT | /users/id | 根据id更新一个用户 |
DELETE | /users/id | 根据id删除一个用户 |
User实体类:
public class User { private long id;
private String name;
private Integer age; //省略了 Getter 与 Setter 方法。
}
具体的 Controller 类:
@RestController
@RequestMapping(value = "/users")
public class UserController { //创建线程安全的Map
static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>()); @RequestMapping(value="/", method=RequestMethod.GET)
public List<User> getUserList(){
// 处理"/users/"的GET请求,用来获取用户列表
List<User> list = new ArrayList<User>(users.values());
return list;
} @RequestMapping(value="/", method=RequestMethod.POST)
public String postUser(@ModelAttribute User user) {
// 处理"/users/"的POST请求,用来创建User
users.put(user.getId(), user);
return "创建用户 "+ id +" 成功!";
} @RequestMapping(value="/{id}", method=RequestMethod.GET)
public User getUser(@PathVariable Long id) {
// 处理"/users/{id}"的GET请求,用来获取url中id值的User信息
// url中的id可通过@PathVariable绑定到函数的参数中
return users.get(id);
} @RequestMapping(value="/{id}", method=RequestMethod.PUT)
public String putUser(@PathVariable Long id, @ModelAttribute User user) {
// 处理"/users/{id}"的PUT请求,用来更新User信息
User u = users.get(id);
u.setName(user.getName());
u.setAge(user.getAge());
users.put(id, u);
return "更新用户 "+id+" 信息成功!";
} @RequestMapping(value="/{id}", method= RequestMethod.DELETE)
public String deleteUser(@PathVariable Long id) {
// 处理"/users/{id}"的DELETE请求,用来删除User
users.remove(id);
return "删除用户 "+id+" 成功!";
}
}
利用 google chrome 的 Postman 来进行测试
1. 检查当前用户列表。
链接(GET方法):http://localhost:8080/users/
2. 创建一个新用户。
链接(POST方法):http://localhost:8080/users/
3. 更新用户信息。
链接(PUT方法):http://localhost:8080/users/123
4. 查询用户信息。
链接(GET方法):http://localhost:8080/users/123
5. 检查当前用户列表。
链接(GET方法):http://localhost:8080/users/
6. 删除用户信息。
链接(DELETE方法):http://localhost:8080/users/123
spring boot 学习(三)API注解记录及测试的更多相关文章
- spring boot 学习三:OAuth2 认证
1: 代码地址: https://github.com/liufeiSAP/uaa-zuul 2: 安装: postgres 下载 https://www.openscg.com/bigsq ...
- 我的Spring Boot学习记录(二):Tomcat Server以及Spring MVC的上下文问题
Spring Boot版本: 2.0.0.RELEASE 这里需要引入依赖 spring-boot-starter-web 这里有可能有个人的误解,请抱着怀疑态度看. 建议: 感觉自己也会被绕晕,所以 ...
- spring boot / cloud (三) 集成springfox-swagger2构建在线API文档
spring boot / cloud (三) 集成springfox-swagger2构建在线API文档 前言 不能同步更新API文档会有什么问题? 理想情况下,为所开发的服务编写接口文档,能提高与 ...
- Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客
==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...
- 2019-04-05 Spring Boot学习记录
1. 使用步骤 ① 在pom.xml 增加父级依赖(spring-boot-starter-parent) ② 增加项目起步依赖,如spring-boot-starter-web ③ 配置JDK版本插 ...
- Spring Boot 实战 —— MyBatis(注解版)使用方法
原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...
- Spring Boot学习路线
Spring Boot 学习路线,本文计划根据作者近几年的工作.学习经验,来分析和制定一个学习使用 Spring Boot技术的步骤路线图. SpringBoot是伴随着Spring4.0诞生的: S ...
- Spring Boot学习大全(入门)
Spring Boot学习(入门) 1.了解Spring boot Spring boot的官网(https://spring.io),我们需要的一些jar包,配置文件都可以在下载.添置书签后,我自己 ...
- Spring boot学习1 构建微服务:Spring boot 入门篇
Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...
随机推荐
- Android项目开发一
Android项目开发一 进度计划 1.第一周 开源中国注册账号:http://my.oschina.net/u/2511208,并上传Android HelloWorld程序代码 搭建Andro ...
- Android 实践项目开发 总结
Android 实践项目开发 总结 课程:移动平台应用开发实践 班级:201592 姓名:杨凤 学号:20159213 成绩:___________ 指导老师:娄嘉鹏 ...
- topcoder srm 620 div1
problem1 link 分别计算可以得到(a,b)的有哪些二元组,以及可以得到(c,d)的有哪些二元组.然后在公共的二元组中找到和最大的即可. problem2 link 设最后的排序为$r=[2 ...
- Entity Framework 6 和 MVC5
网站地址: Entity Framework 6 http://msdn.microsoft.com/en-us/data/ef.aspx MVC5 http://www.asp.net ...
- 筛选出sql 查询结果中 不包含某个字符
select * from table1 where patindex('%关键字%' , aa) = 0 select * from table1 where charindex('关键字' , a ...
- 【TCP/IP详解 卷一:协议】第十九章 TCP的交互数据流
19.1 引言 前一章我们介绍了TCP连接的建立与释放:三握四挥,以及状态转移图. TCP报文段分为:交互数据,以及成块数据(下一章介绍). 交互数据:例如telnet,ssh,这种类型的协议在大多数 ...
- 12_Python操作MySQL(basic)
""" Test connection to MySQL using mysql-client conn = MySQLdb.connect(host,port,user ...
- UVa 11300 分金币
https://vjudge.net/problem/UVA-11300 题意: 圆桌上有n个人,每个人都有一定的初始金币,每个人可以给他旁边的人一些金币,最终使每个人的金币数相等.计算最少需要转手的 ...
- pragma comment的使用(转)
#pragma 的使用 尽管 C 和 C++ 都已经有标准,但是几乎每个编译器 (广义,包含连接器等) 扩展一些 C/C++ 关键字. 合理地应用这些关键字,有时候能使我们的工作非常方便.下面随便说说 ...
- Codeforces Beta Round #94 div 2 B
B. Students and Shoelaces time limit per test 2 seconds memory limit per test 256 megabytes input st ...