【动态拦截@PostMapping注解,并且修改或新增value属性】

不仅可以拦截PostMapping 别的注解也可以



import java.lang.reflect.Field;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author:pys
 * @时间:2023年03月22日 14:14
 * @说明:动态修改RequestMapping注解
 */

@Slf4j
@Component
public class RequestMappingPrefixBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

  private static final String OLD_PREFIX = "/rest/rum";
  private static final String NEW_PREFIX = "/api/rum";
  private static final String VALUE = "value";
  private static final String MEMBER_VALUES = "memberValues";

  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
      throws BeansException {
    //获取RestController注解标注的bean
    String[] beanNames = beanFactory.getBeanNamesForAnnotation(RestController.class);
    for (String beanName : beanNames) {
      //获取Class
      Class<?> beanType = beanFactory.getType(beanName);
      //如果被RequestMapping标注
      if (beanType.isAnnotationPresent(RequestMapping.class)) {
        //获取注解
        RequestMapping annotation = beanType.getAnnotation(RequestMapping.class);
        //获取RequestMapping的所有路径
        String[] paths = annotation.value();
        List<String> newPathList = new ArrayList<>();
        //是否为 /rest/rum 开头的flag
        boolean addedNewPrefixFlag = false;
        String newPath = null;
        //遍历url路径
        for (String path : paths) {
          if (path.startsWith(OLD_PREFIX)) {
            log.info("old Annotation Value " + annotation.value()[0]);
            newPath = path.replace(OLD_PREFIX, NEW_PREFIX);
            //添加新的路径
            newPathList.add(newPath);
            addedNewPrefixFlag = true;
          }
          //添加老的路径
          newPathList.add(path);
          break;
        }
        //如果添加成功
        if (addedNewPrefixFlag && newPath != null) {
          //调转顺序 framework识别的是 path[0]的路径 所以要把/api/rum放前边 /rest/rum放后边
          newPathList.add(0, newPath);
          String[] newPaths = newPathList.toArray(new String[0]);
          Field f;
          try {
            //获取代理handler
            Object handler = Proxy.getInvocationHandler(annotation);
            //获取RequestMapping真正属性存值的地方
            f = handler.getClass().getDeclaredField(MEMBER_VALUES);
            f.setAccessible(true);
            Map<String, Object> memberValues;
            memberValues = (Map<String, Object>) f.get(handler);
            //替换value
            memberValues.put(VALUE, newPaths);
            log.info("New Annotation Value " + annotation.value()[0]);
          } catch (Exception e) {
            log.error("Failed to set annotation value: " + e.getMessage(), e);
          }
        }
      }
    }

  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用Spring Boot提供的AOP(面向切面编程)功能来实现该需求。具体来说,你可以定义一个切面类,使用@Around注解拦截所有被@RestController注解的类中的@PostMapping方法,并在方法执行前后打印请求值和返回值。 以下是示例代码: ```java @Aspect @Component public class LoggingAspect { private static final Logger LOGGER = LoggerFactory.getLogger(LoggingAspect.class); @Around("@annotation(org.springframework.web.bind.annotation.PostMapping) && within(@org.springframework.web.bind.annotation.RestController *)") public Object logRequestAndResponse(ProceedingJoinPoint joinPoint) throws Throwable { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); String requestUrl = request.getRequestURL().toString(); String httpMethod = request.getMethod(); String requestHeaders = Collections.list(request.getHeaderNames()).stream().map(headerName -> headerName + ":" + request.getHeader(headerName)).collect(Collectors.joining(",")); String requestParams = Collections.list(request.getParameterNames()).stream().map(paramName -> paramName + ":" + request.getParameter(paramName)).collect(Collectors.joining(",")); String requestBody = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8); LOGGER.info("Received {} request for {} with headers [{}] and request params [{}] with body [{}]", httpMethod, requestUrl, requestHeaders, requestParams, requestBody); Object response = joinPoint.proceed(); String responseBody = ""; if (response instanceof ResponseEntity) { ResponseEntity responseEntity = (ResponseEntity) response; responseBody = responseEntity.getBody() != null ? responseEntity.getBody().toString() : ""; } else if (response != null) { responseBody = response.toString(); } LOGGER.info("Returning response [{}] for {} request to {}", responseBody, httpMethod, requestUrl); return response; } } ``` 这段代码中定义了一个名为LoggingAspect的切面类,使用@Around注解拦截所有被@PostMapping注解的方法,并且这些方法所在的类必须被@RestController注解标记。在方法执行前,我们可以获取到HTTP请求的URL、请求方法、请求头、请求参数和请求体,并打印到日志中。在方法执行后,我们可以获取到方法的返回值,并打印到日志中。 需要注意的是,我们使用了HttpServletRequest.getInputStream()方法来获取请求体,因此需要注意请求体的大小不能太大,否则可能会导致性能问题。如果需要处理大文件等场景,建议使用其他方式获取请求体,比如使用MultipartFile等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值