spring boot获取request
1. Controller中
1.1 通过静态方法获取
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
但我在使用过程中发现遇到了一个警告
Method invocation 'getRequest' may produce 'java.lang.NullPointerException' less... (Ctrl+F1)
Inspection info: This inspection analyzes method control and data flow to report possible conditions that are always true or false, expressions whose value is statically proven to be constant, and situations that can lead to nullability contract violations.
Variables, method parameters and return values marked as @Nullable or @NotNull are treated as nullable (or not-null, respectively) and used during the analysis to check nullability contracts, e.g. report NullPointerException (NPE) errors that might be produced.
More complex contracts can be defined using @Contract annotation, for example:
@Contract(", null -> null") — method returns null if its second argument is null @Contract(", null -> null; _, !null -> !null") — method returns null if its second argument is null and not-null otherwise @Contract("true -> fail") — a typical assertFalse method which throws an exception if true is passed to it
The inspection can be configured to use custom @Nullable
@NotNull annotations (by default the ones from annotations.jar will be used)
如此使用可能会造成空指针异常,所以建议添加Objects.requireNonNull,如果为空,抛出异常。
HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
附Objects.requireNonNull源码
public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
1.2 通过参数直接获取
在参数上添加后,springboot会帮你绑定,之后可以直接使用
@GetMapping(value = "")
public String center(HttpServletRequest request,HttpServletResponse response) {
//...
}
1.3 自动注入
通过@Autowired自动注入,这样就不用每个方法都写了
@Autowired
private HttpServletRequest request;
@Autowired
private HttpServletResponse response;
@GetMapping(value = "")
public String center() {
//...
}
2.controller以外部分
见1.1
spring boot获取request的更多相关文章
- Spring Boot获取前端页面参数的几种方式总结
Spring Boot的一个好处就是通过注解可以轻松获取前端页面的参数,之后可以将参数经过一系列处理传送到后台数据库. 获得的方式有很多种,这里稍微总结一下,大致分为以下几种: 1.指定前端url请求 ...
- Spring Boot 获取 java resources 下文件
Spring Boot 获取 java resources 下文件 Spring Boot 获取 resources 目录下的目录(例:获取 resources 目录下的 template 目录): ...
- Spring Boot 获取ApplicationContext
package com.demo; import org.springframework.beans.BeansException; import org.springframework.contex ...
- Spring中获取request的几种方法,及其线程安全性分析
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...
- Spring Boot - 获取所有的Bean信息
前言 Spring Boot启动的时候需要加载许多Bean实现最小化配置,本文将尝试找出Spring启动后加载的所有Bean信息: 通过ApplicationContext 去获取所有的Bean 通过 ...
- [No000016E]Spring 中获取 request 的几种方法,及其线程安全性分析
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...
- Spring中获取request的几种方法,及其线程安全性分析(山东数漫江湖)
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...
- Spring Boot 获取Bean对象实体
一.实现 ApplicationContextAware 接口 package com.zxguan; import org.springframework.beans.BeansException; ...
- 【spring Boot】spring boot获取资源文件的三种方式【两种情况下】
首先声明一点,springboot获取资源文件,需要看是 1>从spring boot默认的application.properties资源文件中获取 2>还是从自定义的资源文件中获取 带 ...
随机推荐
- arya-sites模块的主要类
Site类,生成路由, - 方法:url,get_urls, register, login,logout - 字段:_registry = {} Config,基础配置类,主要用 ...
- 前端入门html(表单)
day48 配置Django项目:https://blog.csdn.net/zV3e189oS5c0tSknrBCL/article/details/79606994 <!DOCTYPE ht ...
- 使用dev-tool定位页面性能瓶颈
这是部门同事的一次内部分享,听完后受益颇多,趁着记忆还算新鲜,赶紧记录一波. 从 dev-tool 看页面 parse 过程 时间都去哪儿了 当浏览器发送一个请求到接受所有响应数据截止,这个过程发生了 ...
- php判断是否使用手机访问
直接上代码 /** * 检测是否使用手机访问 * @access public * @return bool */ public function isMobile() { if (isset($_S ...
- 【hdu6035】 Colorful Tree dfs序
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题目大意:给你一棵树,树上每个节点都有一个颜色. 现在定义两点间的距离为两点最短路径上颜色集合 ...
- POJ 1146
#include <iostream> #include <algorithm> #define MAXN 55 using namespace std; char _m[MA ...
- Win7下无法提交MapReduce Job到集群环境(转)
一. 对hadoop eclipse plugin认识不足 http://zy19982004.iteye.com/blog/2024467曾经说到我最hadoop eclipse plugin作用的 ...
- SpringMVC访问WEB-INF下的jsp的方法
当输入localhost:8080/项目名 浏览器弹出不知道神马错误 The absolute uri: http://java.sun.com/jsp/jstl/core cannot be res ...
- dynamic解析Http xml格式响应数据
继续上一篇 构建RESTful风格的WCF服务 ,咱已经把服务端的数据和服务准备好了,客户端调用 wcf rest接口后如何解析xml?下面使用dynamic关键字解析来至于WCF REST XML响 ...
- elasticsearch(三) 之 elasticsearch目录介绍和配置文件详解
目录 elasticsearch 配置 目录详情 (config) 配置文件 elasticsearch.yml 配置集群名称(cluster.name) 配置 network.host 更改数据和储 ...