目录

springboot-忽略接收请求中的参数

springboot 忽略接收请求中的参数

springboot 忽略接收请求中的参数

一、场景说明

在一些开发场景中,特别是前后端分开开发的场景中,由于后端接口采用的VO接收前端请求传递的参数,但是前端开发小伙伴可能会把vo中所有属性不进行过滤就直接调用接口,这样会导致后端api由于不需要某些字段而导致api运行失败,比如:id字段等。

二、开发环境

开发工具: IDEA

开发语言: JAVA 1.8

开发环境: Springboot 2.4.13

三、实现思路
  1. 使用Java的注解技术,定义一个ReceiveIgnoreParam注解,作用在Controller的需要忽略接收属性的方法上。
  2. 通过spring切面编程技术来实现对方法注解的操作,其判断方法参数中是否包含需要忽略的属性,如果存在直接设置为null或者空串
四、具体实现
  • 注解类代码
package com.rewloc.annotation;

import java.lang.annotation.*;

/**

- 类描述忽略接收参数 注解
-
- @date 2022/5/9 10:54
- @slogan 设计就是代码代码就是设计
- @since JDK 8
  _/
  @Retention(RetentionPolicy.RUNTIME)
  @Documented
  @Target(ElementType.METHOD)
  public @interface ReceiveIgnoreParam {
  /** 字段名数组 _/
  String[] fieldName();
  }
  
  • 切面类代码
package com.rewloc.aop;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.BooleanUtil;
import com.rewloc.annotation.ReceiveIgnoreParam;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Arrays;

/**

- 类描述:忽略接收参数 切面类
-
- @slogan 设计就是代码,代码就是设计
- @since JDK 8
  */
  @Aspect
  @Component
  public class ReceiveIgnoreParamAop {
  private static final String ID = "id";

      /**
       * 方法描述: 切面拦截器
       *
       * @slogan 设计就是代码,代码就是设计
       */
      @Pointcut("@annotation(com.rewloc.ReceiveIgnoreParam)")
      private void ignoreReceiveParam() {
      }

      /**
       * 方法描述: 忽略属性值拦截处理
       *
       * @param point 环绕通知处理对象
       * @return {@link Object}
       * @slogan 设计就是代码,代码就是设计
       */
      @Around(value = "ignoreReceiveParam()")
      public Object doAround(ProceedingJoinPoint point) {
          try {
              // 获取注解信息
              MethodSignature methodSignature = (MethodSignature) point.getSignature();
              ReceiveIgnoreParam ignoreReceiveParam = methodSignature.getMethod().getAnnotation(ReceiveIgnoreParam.class);
              // 获取方法的参数
              Object[] args = point.getArgs();
              // 循环返回参数,
              for (Object arg : args) {
                  // 对需要忽略的属性进行null赋值 
                  Arrays.stream(ignoreReceiveParam.fieldName()).forEach(field -> {
                      try {
                          // 清空忽略属性的值
                          cleanValue(arg, field);
                      } catch (Exception e) {
                      }
                  });
              }
              return point.proceed(args);
          } catch (Throwable e) {
              throw new RuntimeException("系统繁忙,请稍后再试!");
          }
      }

      /**
       * 方法描述: 情况属性的值
       *
       * @param obj       参数对象
       * @param fieldName 字段名称
       * @return {@link Object}
       * @slogan 设计就是代码,代码就是设计
       */
      private void cleanValue(Object obj, String fieldName) throws IllegalAccessException {
          // 获取参数对象
          Class<?> aClass = obj.getClass();
          // 获取class中的所有字段
          Field[] fields = aClass.getDeclaredFields();
          for (Field field : fields) {
              // 如果是需要忽略的字段,赋null值
              if (fieldName.equals(field.getName())) {
                  field.setAccessible(true);
                  field.set(obj, null);
              }
          }
      }

  }
  
  • vo 类
package com.rewloc.vo

import lombok.Data;

@Data
public class ReceiveIgnoreParamVO {
private String id;
private String name;
private String sex;
private int age;
private String birthdate;
}
  • 注解使用
package com.rewloc.web;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**

- 类描述:忽略接收参数 controller 类
-
- @slogan 设计就是代码,代码就是设计
- @since JDK 8
_/
@RestController
@RequestMapping("/receive/ignoreParam")
public class ReceiveIgnoreParamController {
/**
_ 分页获取查询列表 *
_ @param vo 接收查询参数的 VO 对象
_ @return Result
*/
@PostMapping("/hello")
@ReceiveIgnoreParam(fieldName = {"id", "name"})
public String selectPage(ReceiveIgnoreParamVO vo) {
// 将当前 vo 对象返回
return JSONObject.toJSONString(vo);
}
}
五、总结

只要注解和 spring 切面结合可以做很多事情。