日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

springboot 忽略接收請求中的參數

作者:Rewloc 更新時間: 2022-05-11 編程語言

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切面結合可以做很多事情。

原文鏈接:https://blog.csdn.net/lhp3000/article/details/124676153

欄目分類
最近更新