文章目录:

1.一些新的注解

1.1 @RestController

1.2 @RequestMapping(常用)

1.3 @GetMapping

1.4 @PostMapping

1.5 @PutMapping

1.6 @DeleteMapping

1.7 @PathVariable

2.SpringBoot实现RESTful风格

2.1 实例代码

2.2 GET请求方式

2.3 DELETE请求方式

2.4 POST请求方式

2.5 PUT请求方式

3.RESTful风格中请求冲突的问题


1.一些新的注解

1.1 @RestController

@RestController:Spring 4 后新增注解,是@Controller 注解功能的增强,是 @Controller 与 @ResponseBody 的组合注解。

如果一个 Controller 类添加了@RestController,那么该 Controller 类下的所有方法都相当于添加了@ResponseBody 注解,用于返回字符串或 json 数据。

1.2 @RequestMapping(常用)

支持 Get 请求,也支持 Post 请求

1.3 @GetMapping

@RequestMapping 和 Get 请求方法的组合,只支持 Get 请求,Get 请求主要用于查询操作。

1.4 @PostMapping

@RequestMapping 和 Post 请求方法的组合,只支持 Post 请求,Post 请求主要用户新增数据。

1.5 @PutMapping

@RequestMapping 和 Put 请求方法的组合,只支持 Put 请求,Put 通常用于修改数据。

1.6 @DeleteMapping

@RequestMapping 和 Delete 请求方法的组合,只支持 Delete 请求,通常用于删除数据。

1.7 @PathVariable

获取 url 中的数据,该注解是实现 RESTful 最主要的一个注解。


2.SpringBoot实现RESTful风格

REST (英文:Representational State Transfer ,简称 REST )

一种互联网软件架构设计的风格,但它并不是标准,它只是提出了一组客户端和服务器交互时的架构理念和设计原则,基于这种理念和原则设计的接口可以更简洁,更有层次,REST这个词,是 Roy Thomas Fielding 在他 2000 年的博士论文中提出的。

任何的技术都可以实现这种理念,如果一个架构符合 REST 原则,就称它为 RESTFul 架构。

比如我们要访问一个 http 接口:http://localhost:8080/springboot/order?id=1021&status=1

采用 RESTFul 风格则 http 地址为:http://localhost:8080/springboot/order/1021/1

2.1 实例代码

首先是一个实体类

package com.songzihao.springboot.entity;public class Student {private Integer id;private String name;private Integer age;//getter and setter
}

然后是我们的控制层

package com.songzihao.springboot.controller;import org.springframework.web.bind.annotation.*;import java.util.HashMap;
import java.util.Map;/****/
@RestController //相当于是 @Controller 与 @ResponseBody 的组合注解,意味着当前控制层类中的所有方法返回的都是Json对象
public class StudentController {@GetMapping(value = "/student/{id}/{age}")public Object student(@PathVariable("id") Integer id,@PathVariable("age") Integer age) {Map<String,Object> map=new HashMap<>();map.put("id",id);map.put("age",age);return map;}@DeleteMapping(value = "/student/detail/{id}/{status}")public Object student2(@PathVariable("id") Integer id,@PathVariable("status") Integer status) {Map<String,Object> map=new HashMap<>();map.put("id",id);map.put("status",status);return map;}@DeleteMapping(value = "/student/{id}/detail/{phone}")public Object student3(@PathVariable("id") Integer id,@PathVariable("phone") Integer phone) {Map<String,Object> map=new HashMap<>();map.put("id",id);map.put("phone",phone);return map;}@PostMapping(value = "/student/{id}")public String addStudent(@PathVariable("id") Integer id) {return "add student ID: " + id;}@PutMapping(value = "/student/{id}")public String updateStudent(@PathVariable("id") Integer id) {return "update student ID: " + id;}
}

接下来是springboot的核心配置文件

server.port=8082server.servlet.context-path=/springboot

最后通过springboot项目入口类启动

package com.songzihao.springboot;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

由于请求方式的不同(有GET、POST、PUT、DELETE),这里我使用Postman工具来模拟发送请求,查看测试结果。

2.2 GET请求方式

2.3 DELETE请求方式

2.4 POST请求方式

2.5 PUT请求方式


3.RESTful风格中请求冲突的问题

对于下面这两段代码,就会出现请求冲突的问题。

    @DeleteMapping(value = "/student/detail/{id}/{status}")public Object student2(@PathVariable("id") Integer id,@PathVariable("status") Integer status) {Map<String,Object> map=new HashMap<>();map.put("id",id);map.put("status",status);return map;}
    @DeleteMapping(value = "/student/detail/{id}/{phone}")public Object student2(@PathVariable("id") Integer id,@PathVariable("phone") Integer phone) {Map<String,Object> map=new HashMap<>();map.put("id",id);map.put("phone",phone);return map;}

因为当我们发起请求之后,服务器并不知道这个地址 http://localhost:8082/springboot/student/1001/666 对应的是那个方法

解决的方法有两个:

  1. 修改路径。(见下面的代码)
  2. 修改请求方式。(这个意思是说,将请求方式由DELETE改为其他三种,所以就不再举例了)
    @DeleteMapping(value = "/student/detail/{id}/{status}")public Object student2(@PathVariable("id") Integer id,@PathVariable("status") Integer status) {Map<String,Object> map=new HashMap<>();map.put("id",id);map.put("status",status);return map;}

    @DeleteMapping(value = "/student/{phone}/detail/{id}")public Object student3(@PathVariable("id") Integer id,@PathVariable("phone") Integer phone) {Map<String,Object> map=new HashMap<>();map.put("id",id);map.put("phone",phone);return map;}

SpringBoot——SpringBoot中使用RESTful风格相关推荐

  1. java中REST_Java——Restful风格

    REST与RESTful: REST:表现层状态转移,资源在网络中以某种形式进行状态转移. RESTful是基于REST理念的一套开发风格,是具体的开发规则. 服务器端只返回数据,以json或者xml ...

  2. SpringBoot与Restful风格

    一.什么是REST?         REST(英文:Representational State Transfer,简称REST,意思:表述性状态转换,描述了一个架构样式的网络系统,比如web应用) ...

  3. SpringBoot RESTful 风格 API 多语言国际化i18n解决方案

    文章目录 1 摘要 2 核心代码 2.1 多语言枚举类 2.2 多语言处理工具类 2.3 多语言的API返回状态码枚举类 2.4 多语言 API 接口返回结果封装 2.5 i18n 国际化多语言配置文 ...

  4. Spring MVC开发RESTful风格的URI

    一.写在前面 RESTful结构可参考博文:https://blog.csdn.net/codejas/article/details/79799386 我们知道在HTTP 协议中,有四种操作方式的动 ...

  5. SpringMVC基础学习之Restful风格的简单使用

    前言: 小伙伴们,大家好,我是狂奔の蜗牛rz,当然你们可以叫我蜗牛君,我是一个学习Java半年多时间的小菜鸟,同时还有一个伟大的梦想,那就是有朝一日,成为一个优秀的Java架构师. 这个SpringM ...

  6. 在原有的SSH项目上开发RESTful风格的HTTP接口

    最近做一个需求,其中有个节点涉及到需要在传统的SSH项目中开发RESTful风格的HTTP接口,供请求方访问,获取结果,响应处理结果,其开发风格与spring cloud中控制器(controller ...

  7. springboot 集成jpa_基于Spring Boot+JPA Restful 风格的数据

    第一章 Restful简介 Restful是一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服 务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次, ...

  8. SpringBoot——JPA的使用、构建restful风格的JPA

    1.JPA概述 JPA:Java持久化规范.JPA(Java Persistence API)是Sun官方提出的Java持久化规范.为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的 ...

  9. Springboot 之 RESTFul风格

    3.4 Spring Boot 实现 RESTful 风格 3.4.1 关于RESTFul REST(英文:Representational State Transfer,简称 REST) RESTF ...

最新文章

  1. Oracle 字符转date类型问题,未解决
  2. 《爬虫与网络编程基础》学习
  3. c#生成随机位数的汉字字符串
  4. 继承复习-发均分红包案例
  5. python中的ix是啥_python pandas (ix iloc loc) 的区别
  6. this.get_element .style为空或不是对象
  7. 2008安装完了找不到_7206BEP.进口轴承_玉溪SKF轴承安装指南
  8. Android AndroidNSSP的简单说明
  9. java true false 异或_三元运算符21?true:false;
  10. vue组件通讯:父传子、子传父、事件发射详解
  11. 扫宽、分辨率和扫描时间
  12. So easy!非技术人员也能学会的土狗防骗技巧!
  13. 中职计算机应用专业(云计算方向)建设实践
  14. 1000瓶毒药,小白鼠测毒问题
  15. gr-osmosdr的安装
  16. 使用scikit-image feature计算图像特征与常见特征示例
  17. 招聘海外博士计算机视觉国际,丹麦奥尔堡大学计算机视觉博士后职位
  18. 不服丨月薪10k程序员vs月薪40K的程序员
  19. Acrel-3200远程预付费电能管理系统 在福州万宝产业园的应用
  20. 论文阅读笔记--Deep Visual Saliency on Stereoscopic Images

热门文章

  1. vsphere 导入虚拟机_Vmware虚拟机教程之vmware vsphere 打开本地的虚拟机资源,workstation中的虚拟机导入到vsphere...
  2. 【matlab测试与修正】考虑源荷两侧不确定性的含风电电力系统低碳调度
  3. NIMDA娜妲(尼姆达)病毒部分反汇编代码
  4. X86_64 CR3详解
  5. 矩阵分析之 实矩阵分解(5)矩阵分解法总结
  6. 麦肯锡:看好中国数字经济
  7. C++线程间共享数据
  8. 阿里云ECS研发2021春招(校招/实习)
  9. 有源器件与无源器件的区别
  10. axios 前端请求接口 跨域问题 Vue实现跨域请求