class="hide-preCode-box">

3.4 注册拦截器和过滤器

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean<RequestWrapperFilter> loggingFilter() {
        FilterRegistrationBean<RequestWrapperFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new RequestWrapperFilter());
        registrationBean.addUrlPatterns("/api/*");  // 这里根据需要配置拦截的 URL
        return registrationBean;
    }
}
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private RequestBodyInterceptor requestBodyInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(requestBodyInterceptor).addPathPatterns("/api/*");  // 根据需要配置路径
    }
}
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">

4. 总结

通过以上步骤,你可以实现一个能够多次读取 HttpServletRequest.getInputStream() 数据的机制。基本思路是:

这样,无论在拦截器还是后续的参数解析过程中,都会能够多次访问请求体数据。

方式二:拦截器中使用包装类ContentCachingRequestWrapper

在 HTTP 请求中,HttpServletRequest 的请求体(POST 请求中的 JSON 数据)是一次性的流,读取完一次之后,如果没有特殊处理,就不能再次读取它。

ContentCachingRequestWrapper 是 Spring 框架提供的一个包装类,它的作用是“包装”原始的 HttpServletRequest 对象,使得请求体内容可以被多次读取。

使用ContentCachingRequestWrapper ,省去方法一中创建的 HttpServletRequestWrapper和RequestWrapperFilter。

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RequestBodyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws IOException {
		if (HttpMethod.POST.matches(request.getMethod())) {
 			// 包装请求,使其可以多次读取请求体
            ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request);

			// 读取请求体,明确指定字符编码为 UTF-8
			String requestBody = new String(wrappedRequest.getContentAsByteArray(), "UTF-8");

            // 打印或处理请求体内容
            System.out.println("Request Body: " + requestBody);

            // 将请求体转换成 Java 对象
            MyRequestObject myRequestObject = new ObjectMapper().readValue(requestBody, MyRequestObject.class);
            System.out.println("Parsed Java Object: " + myRequestObject);
        }

        return true; // 返回 true,表示继续处理请求
    }
}
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">

总结

ContentCachingRequestWrapper 是一种非常有用的工具,允许缓存并多次读取请求体内容,尤其需要在拦截器中处理请求体数据时,它非常有效。

data-report-view="{"mod":"1585297308_001","spm":"1001.2101.3001.6548","dest":"https://blog.csdn.net/m0_69893292/article/details/144973960","extend1":"pc","ab":"new"}">>
注:本文转载自blog.csdn.net的PlutoZuo的文章"https://blog.csdn.net/PlutoZuo/article/details/134259279"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接

评论记录:

未查询到任何数据!