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注解记录及测试的更多相关文章

  1. spring boot 学习三:OAuth2 认证

    1:  代码地址: https://github.com/liufeiSAP/uaa-zuul 2:     安装: postgres 下载 https://www.openscg.com/bigsq ...

  2. 我的Spring Boot学习记录(二):Tomcat Server以及Spring MVC的上下文问题

    Spring Boot版本: 2.0.0.RELEASE 这里需要引入依赖 spring-boot-starter-web 这里有可能有个人的误解,请抱着怀疑态度看. 建议: 感觉自己也会被绕晕,所以 ...

  3. spring boot / cloud (三) 集成springfox-swagger2构建在线API文档

    spring boot / cloud (三) 集成springfox-swagger2构建在线API文档 前言 不能同步更新API文档会有什么问题? 理想情况下,为所开发的服务编写接口文档,能提高与 ...

  4. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  5. 2019-04-05 Spring Boot学习记录

    1. 使用步骤 ① 在pom.xml 增加父级依赖(spring-boot-starter-parent) ② 增加项目起步依赖,如spring-boot-starter-web ③ 配置JDK版本插 ...

  6. Spring Boot 实战 —— MyBatis(注解版)使用方法

    原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...

  7. Spring Boot学习路线

    Spring Boot 学习路线,本文计划根据作者近几年的工作.学习经验,来分析和制定一个学习使用 Spring Boot技术的步骤路线图. SpringBoot是伴随着Spring4.0诞生的: S ...

  8. Spring Boot学习大全(入门)

    Spring Boot学习(入门) 1.了解Spring boot Spring boot的官网(https://spring.io),我们需要的一些jar包,配置文件都可以在下载.添置书签后,我自己 ...

  9. Spring boot学习1 构建微服务:Spring boot 入门篇

    Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...

随机推荐

  1. 六角填数|2014年蓝桥杯B组题解析第七题-fishers

    六角填数 如图所示六角形中,填入1~12的数字. 使得每条直线上的数字之和都相同. 图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少? 请通过浏览器提交答案,不要填写多余的内容. 思路 ...

  2. 第八章 对称加密算法--AES

    注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第7章“初等加密算法--对称加密算法” 8.1.AES 特点: 密钥建立时间短.灵敏性好.内存需求低(不管怎样,反正就是好) ...

  3. BZOJ3300: [USACO2011 Feb]Best Parenthesis 模拟

    Description Recently, the cows have been competing with strings of balanced  parentheses and compari ...

  4. [BZOJ3613][Heoi2014]南园满地堆轻絮 二分答案

    Description 小 Z 是 ZRP(Zombies’ Republic of Poetry,僵尸诗歌共和国)的一名诗歌爱好者,最近 他研究起了诗词音律的问题.   在过去,诗词是需要编成曲子唱 ...

  5. xshell5 Linux 上传下载文件

    1,先登录身份验证和文件传输ZMODEM 选择自动激活. 2,rpm -qa | grep lrzsz 利用此命令查看是否安装了lrzsz . 如果没有任何反应则是没有安装 若没有安装 yum ins ...

  6. Java LinkedList源码剖析

    LinkedList 本文github地址 总体介绍 LinkedList同时实现了List接口和Deque接口,也就是说它既可以看作一个顺序容器,又可以看作一个队列(Queue),同时又可以看作一个 ...

  7. Nexus Repository 搭建及使用

    Nexus Repository 是搭建maven的镜像的工具之一,在全球范围内使用挺广的. 一.Nexus 搭建过程 Nexus 镜像的搭建还是相对简单的,将下载的文件解压到相应的目录下,然后进入. ...

  8. CSAPP学习笔记 第一章 计算机系统漫游

    Ch 1.0 1.计算机系统是由硬件和系统软件组成的 2.本书阐述了计算机组件是如何工作的以及执行组件是如何影响程序正确性和性能的. 3.通过跟踪hello程序的生命周期来开始对系统的学习. #inc ...

  9. Qt5_qtconfig

    1.http://tieba.baidu.com/p/3225596765 QtConfig was removed in Qt5. If you want to force Qt5 to use a ...

  10. Linux环境下 RabbitMQ 的下载与安装

    0 环境 CentOS7 RabbitMQ 3.6.5 erlang 18.3 socat rabbitmq是使用erlang语言编写的,所以需要先安装erlang,其次rabbitmq安装依赖于so ...