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实体类:
1 2 3 4 5 6 7 8 | public class User { private long id; private String name; private Integer age; //省略了 Getter 与 Setter 方法。 } |
具体的 Controller 类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | @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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具