RequestContextHolder获取request和response

首先需要在web.xml中配置监听器,这个监听器源码如下,实现了ServletRequestListener,则每个请求进来都会被监听

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.web.context.request;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.i18n.LocaleContextHolder;

public class RequestContextListener implements ServletRequestListener {
    private static final String REQUEST_ATTRIBUTES_ATTRIBUTE = RequestContextListener.class.getName() + ".REQUEST_ATTRIBUTES";

    public RequestContextListener() {
    }

    public void requestInitialized(ServletRequestEvent requestEvent) {
        if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
            throw new IllegalArgumentException("Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
        } else {
            HttpServletRequest request = (HttpServletRequest)requestEvent.getServletRequest();
            ServletRequestAttributes attributes = new ServletRequestAttributes(request);
            request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
            LocaleContextHolder.setLocale(request.getLocale());  
            RequestContextHolder.setRequestAttributes(attributes);//将request包装的ServletRequestAttributes放入RequestContextHolder
        }
    }

    public void requestDestroyed(ServletRequestEvent requestEvent) {
        ServletRequestAttributes attributes = (ServletRequestAttributes)requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE);
        ServletRequestAttributes threadAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        if (threadAttributes != null) {
            if (attributes == null) {
                attributes = threadAttributes;
            }

            LocaleContextHolder.resetLocaleContext();
            RequestContextHolder.resetRequestAttributes();  //将ServletRequestAttributes等资源清除掉
        }

        if (attributes != null) {
            attributes.requestCompleted();
        }

    }
}

web.xml中配置如下

<listener>
      <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

spring中获取request 和 response代码


  HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

  HttpServletResponse resp = ((ServletWebRequest)RequestContextHolder.getRequestAttributes()).getResponse();
在Spring Boot中,如果你想要在处理HTTP请求前或后对`RequestBody`进行自定义操作,你可以使用`@ModelAttribute注解`结合`RequestBodyAdvice`。`RequestBodyAdvice`是一个接口,允许你在请求体被处理之前或之后添加、修改或删除数据。 首先,你需要创建一个实现了`RequestBodyAdvice接口`的类: ```java import org.springframework.http.HttpRequest; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.method.support.HandlerMethodArgumentResolver; @RestControllerAdvice public class CustomRequestBodyAdvice implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter parameter) { // 检查是否支持处理RequestBody类型的参数 return parameter.getParameterType().equals(RequestBody.class); } @Override public Object resolveArgument(HttpRequest request, MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { // 这里可以获取原始的RequestBody内容 Object requestBody = request.getBody(); // 对请求体做你想要的操作,比如日志记录、验证等 // 然后返回处理后的请求体 // 返回处理后的请求体给框架继续处理 return requestBody; } } ``` 在这个例子中,`resolveArgument`方法会在每个控制器方法处理`RequestBody`参数之前被调用。你可以在这里获取原始请求体的内容,并对其进行相应的修改。注意,`RequestContextHolder`通常用于获取当前HTTP请求的相关信息,但在`RequestBodyAdvice`上下文中,你直接操作`HttpRequest`即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值