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);
    }
}
相关推荐
悟能不能悟5 分钟前
Gson bean getxxx,怎么才能返回给前端
java·前端
Apex Predator12 分钟前
本地库导入到nexus
java·服务器·前端
仍然.16 分钟前
Java---反射、枚举、lambda表达式 和 泛型进阶
java·开发语言
我爱娃哈哈28 分钟前
SpringBoot + MinIO + 阿里云 OSS:文件上传下载、分片断点续传全链路方案
spring boot·后端·阿里云
小北方城市网30 分钟前
JVM 调优实战指南:从问题排查到参数优化
java·spring boot·python·rabbitmq·java-rabbitmq·数据库架构
Elieal31 分钟前
Java项目密码加密实现详解
java·开发语言
RunsenLIu33 分钟前
基于Spring Boot + Vue的图书馆座位预约管理系统
vue.js·spring boot·后端
shhpeng33 分钟前
go mod vendor命令详解
开发语言·后端·golang
Java程序员威哥33 分钟前
用Java玩转机器学习:协同过滤算法实战(比Python快3倍的工程实现)
java·开发语言·后端·python·算法·spring·机器学习
牧小七38 分钟前
java StampedLock 的使用
java