> 文章列表 > 手搭手SpringBoot之REST接口风格

手搭手SpringBoot之REST接口风格

手搭手SpringBoot之REST接口风格

REST一种软件架构风格

REST即表述性状态传递(英文:Representational State Transfer,简称REST,中文:表示层状态转移)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格。它是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。

在三种主流的Web服务实现方案中,因为REST模式的Web服务与复杂的SOAP和XML-RPC对比来讲明显的更加简洁,越来越多的web服务开始采用REST风格设计和实现。例如,Amazon.com提供接近REST风格的Web服务进行图书查找;雅虎提供的Web服务也是REST风格的。

REST中的要素:用REST表示资源和对资源的操作。在互联网中,表示一个资源或者一个操作。

资源用URL表示。

资源:查询资源、创建资源、更新资源、删除资源

表示层(视图层)状态转移:显示资源,通过视图页面,jsp等。

状态:资源变化。 转移:资源变化。

API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件的以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节。API除了有应用“应用程序接口”的意思外,还特指 API的说明文档,也称为帮助文档。

——————————————————————————————————————————

RESTful的注解

@PathVariable注解

    /
     * @PathVariable:获取url中的数据
     *  value :路径变量名
     *  位置: 放在控制器方法的形参前面
     *  {id}定义路径变量
     */

获取url中的数据

@GetMapping注解

接收和处理get请求。等同于RequestMapping(method=RequestMethod.GET)

@PostMapping注解

接收和处理Post请求。等同于RequestMapping(method=RequestMethod.POST)

@PutMapping注解, Request method 'POST' and 'GET' not supported

支持put请求方式。等同于RequestMapping(method=RequestMethod.PUT)

@DeleteMapping注解 Request method 'POST' and 'GET' not supported

接收delete方式的请求,等同于RequestMapping(method=RequestMethod.DELETE)

@RestController注解

@Controller与@ResponseBody的组合

——————————————————————————————————————————

用例

@RestControllerpublic class RestControllerDemo {@Resourceprivate StaffService service;/* @PathVariable:获取url中的数据*  value :路径变量名*  位置: 放在控制器方法的形参前面*  {id}定义路径变量*/@GetMapping("/Info/{id}")public String getInfo(@PathVariable("id") int id){//根据id查询信息Staff staff = service.selectById(id);return staff.getId()+staff.getName();}@PostMapping("/create/Staff/{id}/{name}")public String createStaff(@PathVariable("id") int id,@PathVariable String name){//执行sql----return "获得到数据:"+id+" : "+name;}@PutMapping("/modifyStaff/{id}/{name}")public String modifyStaff(@PathVariable("id") int id,@PathVariable String name){//执行sql语句 UPDATE staff SET name=#{name}  WHERE  id=#{id}return "更新了:"+id+" : "+name;}@DeleteMapping("/Delete/{id}")public String DelStaff(@PathVariable("id") int id){//执行sql语句 UPDATE staff SET name=#{name}  WHERE  id=#{id}return "id为"+id+"的用户被删除了";}}

建议使用Postman测试get,post,put,delete

配置html支持put和delect请求方式。

HiddenHttpMethodFilter支持将post请求转为put、delete请求

Springboot框架启用HiddenHttpMethodFilter

application.properties配置

#启用HiddenHttpMethodFilter过滤器spring.mvc.hiddenmethod.filter.enabled=true

使用方式

Html页面

Put

<form action="/boot/modifyStaff/003/张三" method="post">

    <input type="hidden" name="_method" value="put">

    <input type="submit" value="提交">

</form>
Delete

<form action="/boot/Delete/001" method="post">

    <input type="hidden" name="_method" value="delete">

    <input type="submit" value="提交">

</form>

Controller

RUL:地址必须唯一

@PutMapping("/modifyStaff/{id}/{name}")public String modifyStaff(@PathVariable("id") int id,@PathVariable String name){//执行sql语句 UPDATE staff SET name=#{name}  WHERE  id=#{id}return "更新了:"+id+" : "+name;}
@DeleteMapping("/Delete/{id}")public String DelStaff(@PathVariable("id") int id){//执行sql语句 DELETEreturn "id为"+id+"的用户被删除了";}

SpringMVC启用HiddenHttpMethodFilter

需在web.xml中配置

<filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>