mocxe2vwww
V2EX  ›  Java

Springboot 如何忽略 空的 json?

  •  
  •   mocxe2vwww · Oct 28, 2020 · 3423 views
    This topic created in 2203 days ago, the information mentioned may be changed or developed.

    例如:

    pubic String (@RequestBody UserForm userForm)

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

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

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

    有什么简单的方法么?

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

    @NotNull
    private Integer age;
    fengpan567
    fengpan567
    Oct 28, 2020
    11
    楼上正解
    BBCCBB
    BBCCBB
    Oct 28, 2020
    12
    这种一般就老老实实自己写 if else 校验了
    chendy
    chendy
    Oct 28, 2020
    13
    @mocxe2vwww #5 那就老老实实写个校验逻辑喽
    xuanbg
    xuanbg
    Oct 29, 2020❤️1
    14
    这个参数验证怎么能优雅得起来呢?你这个接口接收的参数被设计成绝对的灵活,没有丝毫的规则可言。这种设计本身就不优雅,你让参数校验怎么去优雅!
    zifangsky
    zifangsky
    Oct 29, 2020
    15
    一个简单的办法是在 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
    smilekung
    Oct 30, 2020
    16
    我们之前会加一层拦截器,对请求参数做 JSONSchemaValidate,绝对灵活
    THESDZ
    THESDZ
    Oct 30, 2020
    17
    仔细思考了下
    1.一个接口所处理的情况一定是有限的
    2.每种情况的条件一定是确定的
    3.那么最后一个 else 的时候报错就行了
    mocxe2vwww
    mocxe2vwww
    OP
    Oct 31, 2020
    18
    @smilekung 只能搞拦截器了,我本想着框架有没有现成的方式。
    mocxe2vwww
    mocxe2vwww
    OP
    Oct 31, 2020
    19
    @zifangsky 不错 不错 谢谢
    Joker123456789
    Joker123456789
    Nov 12, 2020
    20
    把 userForm 转成 json 字符串,String userFormStr = JSON.toJSONString(userForm);

    然后判断 userFormStr 是否等于{}
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2575 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 11:01 · PVG 19:01 · LAX 03:01 · JFK 06:01
    ♥ Do have faith in what you're doing.