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);
    }
}
相关推荐
砍材农夫37 分钟前
物联网实战|Spring Boot MQTT平台 |emqx broker实战
spring boot·spring·spring cloud·mybatis
似璟如你1 小时前
Java 开发者的 Go 语法基础:从 0 开始快速上手 Go
java·开发语言·后端·golang·go·编程语言
zzz_23681 小时前
TencentDB-Agent-Memory 深度解析:让多个 Agent 共享项目经验的记忆中枢
java·开发语言·jvm·人工智能·agent·memory·tencent db
vx-程序开发2 小时前
django医院预约挂号系统---附源码23353
java·javascript·spring boot·python·eclipse·django·php
金斗潼关2 小时前
ysoserial的使用
java
ruleslol2 小时前
volatile 关键字
java
Codelinghu2 小时前
AI 写代码越强,程序员越不能只懂代码
后端
卡卡敲码2 小时前
Skills 撞车了,Agent 怎么选
后端
羑悻2 小时前
周一早上三件事砸过来,我以为要延期,结果 AI 替我扛了一半!
后端
文艺理科生2 小时前
3 年,8 种方案,1 次重构:LangChain 记忆方案如何从混乱走向清晰
前端·后端·架构