最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot如何忽略接收請求中的參數(shù)

 更新時間:2024年07月16日 09:58:08   作者:Rewloc  
這篇文章主要介紹了springboot如何忽略接收請求中的參數(shù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

一、場景說明

在一些開發(fā)場景中,特別是前后端分開開發(fā)的場景中,由于后端接口采用的VO接收前端請求傳遞的參數(shù),但是前端開發(fā)小伙伴可能會把vo中所有屬性不進行過濾就直接調(diào)用接口,這樣會導(dǎo)致后端api由于不需要某些字段而導(dǎo)致api運行失敗,比如:id字段等。

二、開發(fā)環(huán)境

  • 開發(fā)工具:IDEA
  • 開發(fā)語言:JAVA 1.8
  • 開發(fā)環(huán)境:Springboot 2.4.13

三、實現(xiàn)思路

  • 使用Java的注解技術(shù),定義一個ReceiveIgnoreParam注解,作用在Controller的需要忽略接收屬性的方法上。
  • 通過spring切面編程技術(shù)來實現(xiàn)對方法注解的操作,其判斷方法參數(shù)中是否包含需要忽略的屬性,如果存在直接設(shè)置為null或者空串

四、具體實現(xiàn)

  • 注解類代碼
package com.rewloc.annotation;

import java.lang.annotation.*;

/**
 * 類描述:忽略接收參數(shù) 注解
 *
 * @date 2022/5/9 10:54
 * @slogan 設(shè)計就是代碼,代碼就是設(shè)計
 * @since JDK 8
 */
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(ElementType.METHOD)
public @interface ReceiveIgnoreParam {
    /** 字段名數(shù)組 */
    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;

/**
 * 類描述:忽略接收參數(shù) 切面類
 *
 * @slogan 設(shè)計就是代碼,代碼就是設(shè)計
 * @since JDK 8
 */
@Aspect
@Component
public class ReceiveIgnoreParamAop {
    private static final String ID = "id";

    /**
     * 方法描述: 切面攔截器
     *
     * @slogan 設(shè)計就是代碼,代碼就是設(shè)計
     */
    @Pointcut("@annotation(com.rewloc.ReceiveIgnoreParam)")
    private void ignoreReceiveParam() {
    }

    /**
     * 方法描述: 忽略屬性值攔截處理
     *
     * @param point 環(huán)繞通知處理對象
     * @return {@link Object}
     * @slogan 設(shè)計就是代碼,代碼就是設(shè)計
     */
    @Around(value = "ignoreReceiveParam()")
    public Object doAround(ProceedingJoinPoint point) {
        try {
            // 獲取注解信息
            MethodSignature methodSignature = (MethodSignature) point.getSignature();
            ReceiveIgnoreParam ignoreReceiveParam = methodSignature.getMethod().getAnnotation(ReceiveIgnoreParam.class);
            // 獲取方法的參數(shù)
            Object[] args = point.getArgs();
            // 循環(huán)返回參數(shù),
            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("系統(tǒng)繁忙,請稍后再試!");
        }
    }

    /**
     * 方法描述: 情況屬性的值
     *
     * @param obj       參數(shù)對象
     * @param fieldName 字段名稱
     * @return {@link Object}
     * @slogan 設(shè)計就是代碼,代碼就是設(shè)計
     */
    private void cleanValue(Object obj, String fieldName) throws IllegalAccessException {
        // 獲取參數(shù)對象
        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;

/**
 * 類描述:忽略接收參數(shù) controller類
 *
 * @slogan 設(shè)計就是代碼,代碼就是設(shè)計
 * @since JDK 8
 */
@RestController
@RequestMapping("/receive/ignoreParam")
public class ReceiveIgnoreParamController {
    /**
     * 分頁獲取查詢列表
     *
     * @param vo    接收查詢參數(shù)的VO對象
     * @return Result
     */
    @PostMapping("/hello")
    @ReceiveIgnoreParam(fieldName = {"id", "name"})
    public String selectPage(ReceiveIgnoreParamVO vo) {
        // 將當(dāng)前vo對象返回
        return JSONObject.toJSONString(vo);
    }
}

五、總結(jié)

只要注解和spring切面結(jié)合可以做很多事情。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

诏安县| 枝江市| 平果县| 盘山县| 读书| 牡丹江市| 香港| 交城县| 巴林右旗| 嘉义市| 游戏| 景洪市| 开化县| 河东区| 娄烦县| 苍山县| 景洪市| 无极县| 米泉市| 东城区| 大石桥市| 榆树市| 福安市| 江油市| 西宁市| 苏尼特右旗| 夏河县| 武义县| 贵阳市| 海南省| 皮山县| 太湖县| 迭部县| 琼海市| 肥西县| 葫芦岛市| 寿光市| 绥芬河市| 嘉兴市| 通化市| 大荔县|