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. EF 一个实体对象不能由多个 IEntityChangeTracker 实例引用 解决办法

    在DAL层中,建立工厂类 namespace DAL { public static class SysDbContextFactory { /// <summary> /// 从Http ...

  2. jQuery API的特点

    jQuery API 的特点 版权声明:未经博主授权,严禁转载分享 jQuery API 的三大特点 1. jQuery 对象是一个类数组对象,API自带遍历效果 - 对 jQuery 对象调用一次A ...

  3. 解决Vue循环中子组件不实时更新的问题

    问题描述 使用Element-UI中的table组件时会遇到一个常见的问题.当在el-table中调用子组件的时候会出现数据更新后,子组件没有重新渲染的问题. eg:资源列表中的健康度组件. 代码如下 ...

  4. word2vec 中的数学原理详解(一)目录和前言【转】

    本文转载自:https://blog.csdn.net/itplus/article/details/37969519 word2vec 是 Google 于 2013 年开源推出的一个用于获取 wo ...

  5. 51nod 1043 幸运号码(数位dp

    1043 幸运号码     1个长度为2N的数,如果左边N个数的和 = 右边N个数的和,那么就是一个幸运号码. 例如:99.1230.123312是幸运号码. 给出一个N,求长度为2N的幸运号码的数量 ...

  6. Unity3D学习笔记(六):三角函数和点乘

    三角函数: 概念:用来描述三角形中某个角和对应的三条边的比例关系. 正弦:sin<θ>(sin<theta>)=对边/斜边 余弦:cos<θ>(cos<the ...

  7. 【eclipse】阿里巴巴代码检测插件在线安装

    https://p3c.alibaba.com/plugin/eclipse/update

  8. 【Coursera】Fourth Week(1)

    1994: year of the web (1)网景(Netscape)成立. (2)www conference 在CERN 举办. (3)www conference 在芝加哥举办. (4)十月 ...

  9. C# 实现简单的 Heap 堆(二叉堆)

    如题,C#  实现简单的二叉堆的 Push() 和 Pop(), 如有不足欢迎指正. 另外,在C#中使用 Heap 的相似功能可以考虑使用:Priority Queues,SortedDictiona ...

  10. mysql创建utf8数据库

    1.创建 CREATE DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 2.修改 ALTER DATABASE ...