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的更多相关文章

  1. Spring Boot获取前端页面参数的几种方式总结

    Spring Boot的一个好处就是通过注解可以轻松获取前端页面的参数,之后可以将参数经过一系列处理传送到后台数据库. 获得的方式有很多种,这里稍微总结一下,大致分为以下几种: 1.指定前端url请求 ...

  2. Spring Boot 获取 java resources 下文件

    Spring Boot 获取 java resources 下文件 Spring Boot 获取 resources 目录下的目录(例:获取 resources 目录下的 template 目录): ...

  3. Spring Boot 获取ApplicationContext

    package com.demo; import org.springframework.beans.BeansException; import org.springframework.contex ...

  4. Spring中获取request的几种方法,及其线程安全性分析

    前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...

  5. Spring Boot - 获取所有的Bean信息

    前言 Spring Boot启动的时候需要加载许多Bean实现最小化配置,本文将尝试找出Spring启动后加载的所有Bean信息: 通过ApplicationContext 去获取所有的Bean 通过 ...

  6. [No000016E]Spring 中获取 request 的几种方法,及其线程安全性分析

    前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...

  7. Spring中获取request的几种方法,及其线程安全性分析(山东数漫江湖)

    前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...

  8. Spring Boot 获取Bean对象实体

    一.实现 ApplicationContextAware 接口 package com.zxguan; import org.springframework.beans.BeansException; ...

  9. 【spring Boot】spring boot获取资源文件的三种方式【两种情况下】

    首先声明一点,springboot获取资源文件,需要看是 1>从spring boot默认的application.properties资源文件中获取 2>还是从自定义的资源文件中获取 带 ...

随机推荐

  1. Zookeeper数据存储总结

    Zookeeper快照文件和事物操作文件以文件的形式存储在硬盘上,以快照文件为主,日志文件为辅.因为当对内存数据进行变更的时候,会保证将事务操作记入log日志,而snapshot只是内存某一个时刻影像 ...

  2. Java原子操作类汇总

    当程序更新一个变量时,如果是多线程同时更新这个变量,可能得到的结果与期望值不同.比如:有一个变量i,A线程执行i+1,B线程也执行i+1,经过两个线程的操作后,变量i的值可能不是期望的3,而是2.这是 ...

  3. 基于mongoose 的增删改查操作

    无论是基于robomongo 的可视化工具,亦或是基于 mongoose 的函数工具,只要是对 mongodb 的操作,第一步都是开启数据库. 开启mongodb 数据库 进入mongod所在目录 执 ...

  4. [Swift实际操作]七、常见概念-(1).范围Range、ClosedRange和NSRange的使用实际操作

    本文将为你解析区间的概念.在对字符串进行处理时,经常会使用到区间.例如获得字符串中从某个位置开始到另一个位置之间的内容. 首先导入需要使用到的框架 import Foundation 区间分为闭区间和 ...

  5. 下载Centos7 64位镜像

    下载Centos7 64位镜像 1.打开Centos官网 打开Centos官方网站地址:https://www.centos.org/,点击Get CentOS Now 2.点击Minimal ISO ...

  6. To be learned

    1.web性能测试工具:LoadRunner:2.web自动化测试工具:selenium QTP:3.安全性测试工具:AppScan4.缺陷管理工具:TestLink+Mantisbt5..抓包工具: ...

  7. RDLC_部署到不同的浏览器

    首先我用的是vs2015 的reportview插件 在数据库中应该配置报表的服务器地址,在项目中添加ReportViewer 插件,单独用一个页面显示接收报表 <form id="f ...

  8. shell脚本实现无密码交互的SSH自动登陆

    ssh连接远程主机时候询问密码,跟su.sudo命令的默认行为一样,是不从stdin读入数据的,据称是为安全考虑,但是有时候在脚本当中确实需要无人守值的登陆. 搜索一下不难找到类似的例子,使用expe ...

  9. (转)python的paramiko模块

    python的paramiko模块  原文:http://www.cnblogs.com/breezey/p/6663546.html     paramiko是用python语言写的一个模块,遵循S ...

  10. C# 点击打开浏览器

    System.Diagnostics.Process.Start("网址");//默认浏览器打开网页System.Diagnostics.Process.Start(@" ...