基本介绍
Rest 风格支持(使用 HTTP 请求方式动词来表示对资源的操作)
应用实例
创建 src/main/java/com/lzw/springboot/controller/MonsterController.java
package com.lzw.springboot.controller;
import org.springframework.web.bind.annotation.*;
/**
* @author LiAng
*/
@RestController
public class MonsterController {
//等价的写法
//@RequestMapping(value = "/monster",method = RequestMethod.GET)
@GetMapping("/monster")
public String getMonster() {
return "GET-查询妖怪";
}
//等价写法
//@RequestMapping(value = "/monster", method = RequestMethod.POST)
@PostMapping("/monster")
public String saveMonster() {
return "POST-添加妖怪";
}
//等价写法
//@RequestMapping(value = "/monster",method = RequestMethod.PUT)
@PutMapping("/monster")
public String putMonster() {
return "PUT-修改妖怪";
}
//等价写法
//@RequestMapping(value = "/monster", method = RequestMethod.DELETE)
@DeleteMapping("/monster")
public String delMonster() {
return "DELETE-删除妖怪";
}
}
注意事项和细节
-
客户端是 PostMan 可以直接发送 Put、delete 等方式请求,可不设置 Filter
-
如果要 SpringBoot 支持 页面表单的 Rest 功能,则需要注意如下细节
(1)Rest 风格请求核心Filter ; HiddenHttpMethodFilter,表单请求会被 HiddenHttpMethodFilter 拦截,获取到表单 _method 的值, 再判断是 PUT/DELETE/PATCH。(PATCH 方法是新引入的,是对 PUT 方法的补充,用来对已知资源进行局部更新。https://segmentfault.com/q/1010000005685904)
(2)如果要 SpringBoot 支持 页面表单的 Rest 功能,需要在 application.yml 启用 filter 功能,否则无效。
(3)修改 application.yml 启用 filter 功能
spring:
mvc:
static-path-pattern: /lzwres/**
hiddenmethod:
filter:
enabled: true #启用了 HiddenHttpMethodFilter,开启页面表单的 Rest 功能
web:
resources:
static-locations: ["classpath:/lzwimg/","classpath:/META-INF/resources/",
"classpath:/public/", "classpath:/static/","classpath:/resources/"]
(4)创建 src/main/resources/public/rest.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>测试 rest 风格的 url, 来完成请求.</h1>
<form action="/monster" method="post">
u: <input type="text" name="name"><br/>
<input type="hidden" name="_method" value="delete">
<input type="submit" value="点击提交"></form>
</body>
</html>
思考:为什么这里 return “GET-查询妖怪”,返回的是字符串,而不是转发到对应的资源文件?
修改 MonsterController
package com.lzw.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/**
* @author LiAng
* @ResController 是一个复合注解, 含有@ResponseBody, 所以 springboot 底层(springmvc)处理
* return "xxx" 时, 会以@ResponseBody 注解进行解析处理, 即返回字符串 "xxx", 而不会使用视图解析器来处理
*/
@Controller
public class MonsterController {
@RequestMapping("/go")
public String go() {
return "hello"; // 注意: 1 看controller有没有 /hello[没有配置视图解析器] 2. 如果配置了视图解析器, 就按照视图解析器来定位页面
}
}
创建 src/main/resources/public/hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
修改 application.yml
spring:
mvc:
# static-path-pattern: /lzwres/**
hiddenmethod:
filter:
enabled: true #启用了 HiddenHttpMethodFilter,开启页面表单的 Rest 功能
view: # 配置视图解析器
suffix: .html
prefix: / #这里是需要注意 prefix 需要和当前的 static-path-pattern 一致
原文地址:http://www.cnblogs.com/liangnice/p/16859363.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性