V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
mocxe2vwww
V2EX  ›  Java

Springboot 如何忽略 空的 json?

  •  
  •   mocxe2vwww · 2020-10-28 18:53:46 +08:00 · 2190 次点击
    这是一个创建于 1248 天前的主题,其中的信息可能已经有所发展或是发生改变。

    例如:

    pubic String (@RequestBody UserForm userForm)

    userFrom 必须有一个属性不为 null, 且这个属性不是固定的。

    如果前端传一个{}会反序列化成一个所有属性都为空的 UserForm

    怎么做到检查 requestbody 不能为 {}

    有什么简单的方法么?

    第 1 条附言  ·  2020-10-28 19:53:41 +08:00
    问题是参数不是固定的,可以传{"name": "zsan"} 也可以传{"age" : 12, "gender": "男"} 只有传了某些字段才会进行验证
    20 条回复    2020-11-12 11:07:39 +08:00
    baylee12
        1
    baylee12  
       2020-10-28 18:59:42 +08:00
    hibernate @vliad 注解了解下
    zhady009
        2
    zhady009  
       2020-10-28 19:04:58 +08:00
    @RequestBody(required = false)
    zhady009
        3
    zhady009  
       2020-10-28 19:05:40 +08:00
    看错了 无视..
    chendy
        4
    chendy  
       2020-10-28 19:06:45 +08:00
    你需要参数校验,并不需要判断传入的是不是“{}”
    mocxe2vwww
        5
    mocxe2vwww  
    OP
       2020-10-28 19:20:14 +08:00
    @chendy 问题是参数不是固定的,可以传{"name": "zsan"} 也可以传{"age" : 12, "gender": "男"} 只有传了某些字段才会进行验证
    superBearL
        6
    superBearL  
       2020-10-28 19:26:15 +08:00
    写一个检测函数,用反射实现
    WangZiyue
        7
    WangZiyue  
       2020-10-28 19:31:23 +08:00
    我第一想到的是 filter 里对 httpServletRequest 包装处理判断一下 body.
    mocxe2vwww
        8
    mocxe2vwww  
    OP
       2020-10-28 19:44:46 +08:00
    @superBearL 这是最后的办法嘛,有没有啥优雅的处理方式
    mocxe2vwww
        9
    mocxe2vwww  
    OP
       2020-10-28 19:45:08 +08:00
    @WangZiyue 没办法的时候 就只能这么做了。
    hcx0
        10
    hcx0  
       2020-10-28 19:50:23 +08:00
    一楼就给出了最便捷的方式

    @NotNull
    private Integer age;
    fengpan567
        11
    fengpan567  
       2020-10-28 19:59:50 +08:00
    楼上正解
    BBCCBB
        12
    BBCCBB  
       2020-10-28 20:12:21 +08:00
    这种一般就老老实实自己写 if else 校验了
    chendy
        13
    chendy  
       2020-10-28 20:24:46 +08:00
    @mocxe2vwww #5 那就老老实实写个校验逻辑喽
    xuanbg
        14
    xuanbg  
       2020-10-29 09:17:03 +08:00   ❤️ 1
    这个参数验证怎么能优雅得起来呢?你这个接口接收的参数被设计成绝对的灵活,没有丝毫的规则可言。这种设计本身就不优雅,你让参数校验怎么去优雅!
    zifangsky
        15
    zifangsky  
       2020-10-29 11:14:02 +08:00
    一个简单的办法是在 controller 中用反射校验每一个属性是否都为空,比如:

    /**
    * 检查是否所有属性都为空
    * @author zifangsky
    * @date 2020/10/29 11:05
    * @since 1.0.0
    * @param element 待处理对象
    * @return 返回 true 表示对象的所有参数都为空
    */
    public static <K> boolean checkIfAllFieldsEmpty(K element){
    if(element == null){
    return true;
    }

    Class<?> clazz = element.getClass();

    //1. 获取当前类及父类的所有属性
    List<Field> fieldList = new ArrayList<>();
    while (clazz != null){
    Field[] declaredFields = clazz.getDeclaredFields();
    fieldList.addAll(Arrays.asList(declaredFields));

    clazz = clazz.getSuperclass();
    }

    //2. 统一处理所有符合条件的属性
    for (Field field : fieldList) {
    //更改权限
    field.setAccessible(true);

    try {
    //对于字符串类型的属性除了要判断是否为空,还要再判断一次是否为空字符串
    if(field.getType().isAssignableFrom(String.class)){
    String fValue = (String) field.get(element);
    if(fValue != null && !"".equals(fValue.trim())){
    return false;
    }
    }else{
    Object fValue = field.get(element);
    if(fValue != null){
    return false;
    }
    }
    }catch (Exception e){
    //ignore
    }
    }

    return true;
    }

    public static void main(String[] args) {
    UserForm form1 = new UserForm("张三", "password", 18);
    UserForm form2 = new UserForm("", null, null);

    System.out.println("检查结果是:" + checkIfAllFieldsEmpty(form1));
    System.out.println("检查结果是:" + checkIfAllFieldsEmpty(form2));
    }
    smilekung
        16
    smilekung  
       2020-10-30 15:29:11 +08:00
    我们之前会加一层拦截器,对请求参数做 JSONSchemaValidate,绝对灵活
    THESDZ
        17
    THESDZ  
       2020-10-30 16:37:07 +08:00
    仔细思考了下
    1.一个接口所处理的情况一定是有限的
    2.每种情况的条件一定是确定的
    3.那么最后一个 else 的时候报错就行了
    mocxe2vwww
        18
    mocxe2vwww  
    OP
       2020-10-31 16:36:50 +08:00
    @smilekung 只能搞拦截器了,我本想着框架有没有现成的方式。
    mocxe2vwww
        19
    mocxe2vwww  
    OP
       2020-10-31 16:37:25 +08:00
    @zifangsky 不错 不错 谢谢
    Joker123456789
        20
    Joker123456789  
       2020-11-12 11:07:39 +08:00
    把 userForm 转成 json 字符串,String userFormStr = JSON.toJSONString(userForm);

    然后判断 userFormStr 是否等于{}
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2790 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 14:44 · PVG 22:44 · LAX 07:44 · JFK 10:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.