網站首頁 編程語言 正文
springboot 忽略接收請求中的參數
一、場景說明
在一些開發場景中,特別是前后端分開開發的場景中,由于后端接口采用的VO接收前端請求傳遞的參數,但是前端開發小伙伴可能會把vo中所有屬性不進行過濾就直接調用接口,這樣會導致后端api由于不需要某些字段而導致api運行失敗,比如:id字段等。
二、開發環境
開發工具: IDEA
開發語言: JAVA 1.8
開發環境: Springboot 2.4.13
三、實現思路
- 使用Java的注解技術,定義一個ReceiveIgnoreParam注解,作用在Controller的需要忽略接收屬性的方法上。
- 通過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
相關推薦
- 2023-06-16 GO語言中通道和sync包的使用教程分享_Golang
- 2023-07-13 websocket的使用及nginx通信的ws代理配置
- 2022-08-07 gRPC超時攔截器實現示例_Golang
- 2023-06-16 GO語言中Chan實現原理的示例詳解_Golang
- 2023-01-09 python數據分析之如何刪除value=0的行_python
- 2023-08-15 :prop父組件給子組件傳遞函數 子組件接收 并default子組件自己的方法 問題
- 2023-01-02 如何用C#找出數組中只出現了一次的數字_C#教程
- 2022-06-14 教你使用docker查看運行中的容器_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支