栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 忽略接收请求中的参数

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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.*;


@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;


@Aspect
@Component
public class ReceiveIgnoreParamAop {
    private static final String ID = "id";

    
    @Pointcut("@annotation(com.rewloc.ReceiveIgnoreParam)")
    private void ignoreReceiveParam() {
    }

    
    @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("系统繁忙,请稍后再试!");
        }
    }

    
    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;


@RestController
@RequestMapping("/receive/ignoreParam")
public class ReceiveIgnoreParamController {
    
    @PostMapping("/hello")
    @ReceiveIgnoreParam(fieldName = {"id", "name"})
    public String selectPage(ReceiveIgnoreParamVO vo) {
        // 将当前vo对象返回
        return JSONObject.toJSONString(vo);
    }
}
五、总结

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/871172.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号