springboot如何获取控制层get和Post入参

一、在 Spring 配置中创建一个过滤器,将 HttpServletRequest 包装为 ContentCachingRequestWrapper

import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.util.ContentCachingRequestWrapper;
import java.io.IOException;

@Component
public class xxxxFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) 
            throws ServletException, IOException {
        ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request);
        filterChain.doFilter(wrappedRequest, response);
    }
}

二、添加注解

import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnotation {
  
    String XXXX() default "";
}

三、添加切面

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class MyAspect {

    @Around("logPointcut() && @annotation(logAnnotation)")
    public Object around(ProceedingJoinPoint joinPoint, MyAnotation  myAnotation ) throws Throwable {
        // 获取当前 HttpServletRequest
        HttpServletRequest request = getCurrentHttpRequest();

        if (request != null) {
            // 判断请求方法
            if ("GET".equalsIgnoreCase(request.getMethod())) {
                String paramName = request.getParameter("name");
                System.out.println("GET 请求参数 name: " + paramName);
            } else if ("POST".equalsIgnoreCase(request.getMethod())) {
                String body = getRequestBody(request);
                String paramName = getParamFromBody(body, "name");
                System.out.println("POST 请求 body 参数 name: " + paramName);
            }
        }

        return joinPoint.proceed();
    }

    // 获取当前 HttpServletRequest
    private HttpServletRequest getCurrentHttpRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes != null ? attributes.getRequest() : null;
    }

    // 读取 POST 请求体内容
    private String getRequestBody(HttpServletRequest request) {
        StringBuilder body = new StringBuilder();
        try (BufferedReader reader = request.getReader()) {
            String line;
            while ((line = reader.readLine()) != null) {
                body.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return body.toString();
    }

    // 从 JSON 请求体中解析指定参数
    private String getParamFromBody(String body, String paramName) {
        JSONObject jsonObject = JSON.parseObject(body);
        return jsonObject.getString(paramName);
    }
}
相关推荐
呜呼~225147 分钟前
前后端数据交互
java·vue.js·spring boot·前端框架·intellij-idea·交互·css3
飞的肖15 分钟前
从测试服务器手动热部署到生产环境的实现
java·服务器·系统架构
周伯通*20 分钟前
策略模式以及优化
java·前端·策略模式
两点王爷28 分钟前
Java读取csv文件内容,保存到sqlite数据库中
java·数据库·sqlite·csv
lvbu_2024war0139 分钟前
MATLAB语言的网络编程
开发语言·后端·golang
问道飞鱼40 分钟前
【Springboot知识】Springboot进阶-实现CAS完整流程
java·spring boot·后端·cas
抓哇小菜鸡1 小时前
WebSocket
java·websocket
single5941 小时前
【c++笔试强训】(第四十五篇)
java·开发语言·数据结构·c++·算法
Q_19284999061 小时前
基于Spring Boot的电影网站系统
java·spring boot·后端
豌豆花下猫1 小时前
Python 潮流周刊#83:uv 的使用技巧(摘要)
后端·python·ai