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);
    }
}
相关推荐
hstar952714 分钟前
二、即时通讯系统设计经验
java·架构
江梦寻28 分钟前
MacOS下Homebrew国内镜像加速指南(2025最新国内镜像加速)
开发语言·后端·python·macos·架构·策略模式
风象南35 分钟前
SpringBoot的4种死信队列处理方式
java·spring boot·后端
互联网全栈架构2 小时前
遨游Spring AI:第一盘菜Hello World
java·人工智能·后端·spring
优秀的颜3 小时前
计算机基础知识(第五篇)
java·开发语言·分布式
BillKu3 小时前
Java严格模式withResolverStyle解析日期错误及解决方案
java
网安INF3 小时前
ElGamal加密算法:离散对数难题的安全基石
java·网络安全·密码学
AWS官方合作商4 小时前
在CSDN发布AWS Proton解决方案:实现云原生应用的标准化部署
java·云原生·aws
gadiaola5 小时前
【JVM】Java虚拟机(二)——垃圾回收
java·jvm
coderSong25687 小时前
Java高级 |【实验八】springboot 使用Websocket
java·spring boot·后端·websocket