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应用的初始搭建以及开发过程.该框 ...
随机推荐
- 添加用户到 sudo
sudo 简介: 在 Linux 中系统管理员可以通过 sudo 实用程序让用户或组能够作为另一个用户运行命令.换句话说,可以分派命令特权,而不需要另一个用户的密码.root 用户通过在 /etc/s ...
- 20145333茹翔《网络对抗》Exp9 Web安全基础实践
20145333茹翔<网络对抗>Exp9 Web安全基础实践 基础问题回答 1.SQL注入原理,如何防御 SQL注入 就是通过把SQL命令插入到"Web表单递交"或&q ...
- 车载项目问题解(memset)
1memset函数解 1.void *memset(void *s,int c,size_t n) 总的作用:将已开辟内存空间 s 的首 n 个字节的值设为值 c.2.例子 #includevoid ...
- js实现ajax请求
练下手,好久没写ajax var xmlhttp=null;//创建XMLHttprequest function createXMLHttpRequest(){ if(window.ActiveXO ...
- Online Judge 2014 K-th Number -主席树
You are working for Macrohard company in data structures department. After failing your previous tas ...
- 处理文字基线 文字对不齐 font-size
这是我无意中发现的,先记录下 比如你在一个h1标签里面添加文字,或者在其他块级标签添加文字,想让他们间隙少一点,(基线影响)可把行高设置为line-height: 1;就达到消除基线的作用. 建议使用 ...
- OpenFlow protocol version 1.0 通信过程
参考 李呈:[原创]OpenFlow通信流程解读 OpenFlow protocol version 1.0 通信过程 通信双方: OpenFlow控制器,OpenFlow交换机. 通信模块: Sec ...
- C#删除和清空文件夹的程序
/// <summary> /// 清空指定的文件夹,但不删除文件夹 /// </summary> /// <param name="dir"> ...
- Linux(CentOS 6.5) 下安装MySql 5.7.18 二进制版本粗浅攻略
鉴于Linux和mysql因不同版本,安装方式也不同,所以在阅读本攻略前,请确保各位同学的版本和我的Linux.MySql 版本一致. 如果不一致,只能参考. 我的版本: Linux CentOS 6 ...
- python 可视化时间转时间元组(自定义)
>>> print(time.strptime('2018-9-30 11:32:23', '%Y-%m-%d %H:%M:%S')) time.struct_time(tm_yea ...