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>还是从自定义的资源文件中获取 带 ...
随机推荐
- 几条shell命令
windows: route add 158.0.0.0 mask 255.0.0.0 158.137.38.1 metric 3 linux: netstat -apn 查看所有端口使用,可查看端 ...
- Day 49 CSS样式.
外部样式 1.元素选择器 /*1. 元素选择器*/p{ color :red} <p>1.我是一个p标签</p> 2.ID选择器 /*2 ID 选择器*/#p{ color : ...
- vhosetuser 和 vhostuservlient 差异
Open vSwitch支持的vHost-user类型 在Open vSwitch中vHost User通过socket进行通信,模式为client-server,其中server端负责创建/管理/销 ...
- Linux下安装python虚拟环境(virtualenv,vritulaenvwrapper)
一.virtualenv virtualenv是如何创建"独立"的Python运行环境的呢? 原理很简单,就是把系统Python复制一份到virtualenv的环境,用命令sour ...
- Brainteaser-292. Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- about BFC
https://www.cnblogs.com/lhb25/p/inside-block-formatting-ontext.html Box.Formatting Context(BFC)
- springboot入门之简单demo
项目构建 我们采用maven构建SpringBoot工程,首先创建一个maven工程,对应的pom文件如下: <properties> <java.version>1.8< ...
- Android studio 安装的安装若干问题
1.在国内如何更新android sdk? 由于众所周知的某些原因,我们无法直接连接android sdk的更新服务更新sdk,所以可以通过国内的ftp站点把常用的sdk组件如android plat ...
- Python基础3:字符编码
http://www.jb51.net/article/64917.htm Python 编码为什么那么蛋疼? https://i.cnblogs.com/EditPosts.aspx?postid= ...
- Mac下使用zsh不执行/etc/profile文件
Mac下使用了zsh会不执行/etc/profile文件,当然,如果用原始的是会执行. 转而执行的是这两个文件,每次登陆都会执行: ~/.zshrc与/etc/zshenv与/etc/zshrc 所以 ...