Spring Boot 获取 HttpServletRequest 的几种方式
Spring Boot 大约 789 字方法一
SpringMVC
会隐式注入HttpRequest
@RestController
public class TestController {
@GetMapping("/hello")
public String helloGetPathVariable(HttpServletRequest request) {
return "OK";
}
}
方法二
通过依赖注入。
@RestController
public class TestController {
@Autowired
private HttpServletRequest request;
@GetMapping("/hello")
public String helloGetPathVariable() {
return "OK";
}
}
方法三
RequestContextHolder
:Spring
封装的ThreadLocal
。
@RestController
public class TestController {
@GetMapping("/hello")
public String helloGetPathVariable() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
return "OK";
}
}
阅读 1350 · 发布于 2022-10-14
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
Spring 事务阅读 1121
-
Rust 标准库 API 输入 stdin 输出 stdout 错误输出 stderr阅读 331
-
Rust 标准库 API 字符串 String &str阅读 361
-
Spring Boot 集成 Flyway 数据库版本管理阅读 529
-
设计模式之状态设计模式阅读 1661
-
IDEA 根据 URL 快速定位 Controller 类快捷键阅读 1702
-
微信小程序修改 wxParse 支持代码块不换行/表格无法横向滚动等阅读 3911
-
Docker 部署 PostgreSQL阅读 1220
-
package.json 中的依赖包版本号阅读 548
-
PostgreSQL update from 根据 A 表更新 B 表阅读 3496