在SpringMVC的源代码中也提供了一个封装过的ThreadLocal,其中保存了每次请求的HttpServletRequest对象,(详细请看org.springframework.web.context.request.ServletRequestAttributes的源代码) 。

这样我们就可以进行简单封装一下写一个工具进行使用:

*当然必不可少的一点一定记得,在web.xml里必须要有相应的配置来支持 

<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener> 工具类代码: package samples.utils; import java.util.Arrays;
import java.util.Collection;
import java.util.Locale; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.MessageSource;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.ui.context.Theme;
import org.springframework.util.ClassUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ThemeResolver;
import org.springframework.web.servlet.support.RequestContextUtils; /**
* SpringMVC通用工具
* @author
*/
public final class WebContextHolder { private static final Logger LOGGER = LoggerFactory.getLogger(WebContextHolder.class);
private static final WebContextHolder INSTANCE = new WebContextHolder(); public WebContextHolder get() {
return INSTANCE;
} private WebContextHolder() {
super();
} // -------------------------------------------------------------------------------------------------------------- public HttpServletRequest getRequest() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
return attributes.getRequest();
} public HttpSession getSession() {
return getSession(true);
} public HttpSession getSession(boolean create) {
return getRequest().getSession(create);
} public String getSessionId() {
return getSession().getId();
} public ServletContext getServletContext() {
return getSession().getServletContext(); // servlet2.3
} public Locale getLocale() {
return RequestContextUtils.getLocale(getRequest());
} public Theme getTheme() {
return RequestContextUtils.getTheme(getRequest());
} public ApplicationContext getApplicationContext() {
return WebApplicationContextUtils.getWebApplicationContext(getServletContext());
} public ApplicationEventPublisher getApplicationEventPublisher() {
return (ApplicationEventPublisher) getApplicationContext();
} public LocaleResolver getLocaleResolver() {
return RequestContextUtils.getLocaleResolver(getRequest());
} public ThemeResolver getThemeResolver() {
return RequestContextUtils.getThemeResolver(getRequest());
} public ResourceLoader getResourceLoader() {
return (ResourceLoader) getApplicationContext();
} public ResourcePatternResolver getResourcePatternResolver() {
return (ResourcePatternResolver) getApplicationContext();
} public MessageSource getMessageSource() {
return (MessageSource) getApplicationContext();
} public ConversionService getConversionService() {
return getBeanFromApplicationContext(ConversionService.class);
} public DataSource getDataSource() {
return getBeanFromApplicationContext(DataSource.class);
} public Collection<String> getActiveProfiles() {
return Arrays.asList(getApplicationContext().getEnvironment().getActiveProfiles());
} public ClassLoader getBeanClassLoader() {
return ClassUtils.getDefaultClassLoader();
} private <T> T getBeanFromApplicationContext(Class<T> requiredType) {
try {
return getApplicationContext().getBean(requiredType);
} catch (NoUniqueBeanDefinitionException e) {
LOGGER.error(e.getMessage(), e);
throw e;
} catch (NoSuchBeanDefinitionException e) {
LOGGER.warn(e.getMessage());
return null;
}
} }

SpringMVC Http请求工具代码类的更多相关文章

  1. Android开发之强大的网络判断工具,判断是否联网,判断是wifi还是3g网络等java工具代码类

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985, 转载请说明出处. 给大家分享一个Android开发者常用的工具类.主要针对网络判断的 功能强大.下面 ...

  2. WebUtils-网络请求工具类

    网络请求工具类,大幅代码借鉴aplipay. using System; using System.Collections.Generic; using System.IO; using System ...

  3. 微信https请求工具类

    工作中用到的微信https请求工具类. package com.gxgrh.wechat.tools; import com.gxgrh.wechat.wechatapi.service.System ...

  4. HTTP请求工具类

    HTTP请求工具类,适用于微信服务器请求,可以自测 代码; /// <summary> /// HTTP请求工具类 /// </summary> public class Ht ...

  5. springMVC的一些工具类

    springMVC的一些工具类,主要有转换器,读取器 读取文件: package cn.edu.hbcf.common.springmvc; import java.util.HashMap; imp ...

  6. C#实现的UDP收发请求工具类实例

    本文实例讲述了C#实现的UDP收发请求工具类.分享给大家供大家参考,具体如下: 初始化: ListeningPort = int.Parse(ConfigurationManager.AppSetti ...

  7. 我的Android进阶之旅------>Android关于HttpsURLConnection一个忽略Https证书是否正确的Https请求工具类

    下面是一个Android HttpsURLConnection忽略Https证书是否正确的Https请求工具类,不需要验证服务器端证书是否正确,也不需要验证服务器证书中的域名是否有效. (PS:建议下 ...

  8. Java开发微信公众号(三)---微信服务器请求消息,响应消息,事件消息以及工具处理类的封装

    在前面几篇文章我们讲了微信公众号环境的配置 和微信公众号服务的接入,接下来我们来说一下微信服务器请求消息,响应消息以及事件消息的相关内容,首先我们来分析一下消息类型和返回xml格式及实体类的封装. ( ...

  9. java模板模式项目中使用--封装一个http请求工具类

    需要调用http接口的代码继承FundHttpTemplate类,重写getParamData方法,在getParamDate里写调用逻辑. 模板: package com.crb.ocms.fund ...

随机推荐

  1. python制作模块

    自己写的函数,为了下一次方便用,做成模块 主要有这几个步骤: 1:准备发布 2:构建发布 3:导入模块并使用 1:准备发布 首先,我自己写的一个打印出列表(含嵌套列表),打印出列表中的每个数据项,文件 ...

  2. [JAVA]JAVA多线程实现方法之——实现Runnable接口

    public class MultiThread { public static void main(String[] args) { Thread t1 = new Thread(new Threa ...

  3. asp.net core 2.0 后台定时自动执行任务

    自己写一个类继承BackgroundService internal class RefreshService : BackgroundService { protected override asy ...

  4. 干掉hao123劫持浏览器主页

    原因可能是安装某个软件流氓捆绑了IE主页导致的,建议这样尝试: 一.如果安装有三方安全防护类软件,排查流氓软件,建议运行系统自带的Windows Defender或者MSE程序扫描系统. 二.如果有检 ...

  5. ArrayList add方法(转)

    由于 BrowerList 输出结果都是最后一条记录,后来网上查到了 if (dRead.HasRows) { List<Class_RejectQuery> BrowerList = n ...

  6. centos7 安装percona-toolkit工具包的安装和使用

    一.检查和安装与Perl相关的模块 PT工具是使用Perl语言编写和执行的,所以需要系统中有Perl环境. 依赖包检查命令为: rpm -qa perl-DBI perl-DBD-MySQL perl ...

  7. Vue-cli3.0开发笔记

    安装 npm install -g @vue/cli # OR yarn global add @vue/cli 版本为3.X vue --version 创建项目 vue create hello- ...

  8. MySQL视图-(视图创建,修改,删除,查看,更新数据)

    视图是一种虚拟存在的表,对于使用视图的用户来说基本上是透明的.视图并不在数据库中实际存在,行和列数据来自定义视图的查询总使用的表,并且是在使用视图时动态生成的. 视图相对于普通表的优势: 简单:使用视 ...

  9. base64图片

    常见的html中或css中图片的src赋值为data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAEAAAAkCAYAAABIdFAMAAAAGXRFWHR ...

  10. Maven私服(Nexus)启动创建Windows服务

    sc create lutao-maven-nexus binpath= D:\maven-server\nexus--\bin\nexus.exe type= own start= auto dis ...