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);
    }
}
相关推荐
Hazenix24 分钟前
Go 指南:一篇文章速通 Golang
开发语言·后端·golang
灯澜忆梦27 分钟前
GO_复合类型---指针
开发语言·后端·golang
IT_陈寒32 分钟前
SpringBoot自动装配坑了我一天,原来问题出在这
前端·人工智能·后端
wuqingshun31415941 分钟前
MYSQL的乐观锁和悲观锁是什么?
java
星栈1 小时前
深度复盘:比 EventLoop 更隐蔽!Node 微任务堆积引发的线上接口雪崩事故
后端·node.js
唐青枫1 小时前
Java SLF4J 实战指南:从日志门面到 Logback、MDC 和链路追踪
java
名字还没想好☜2 小时前
Go slice 的 append 陷阱:共享底层数组导致的数据串改
开发语言·后端·golang·go·slice
诸神缄默不语2 小时前
FastAPI后端配置CORS中间件支持浏览器跨域访问
后端·fastapi
Fanta丶2 小时前
14.Activiti7 网关 排他网关ExclusiveGateway、并行网关ParallelGateway
后端
明月_清风2 小时前
robots.txt 完全指南:从入门到精通
后端·爬虫