16

关于 spring boot 的参数校验的问题,如何不用注解以及具体对象来校验 Map 里的属性

 3 years ago
source link: https://www.v2ex.com/t/782726
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  Java

关于 spring boot 的参数校验的问题,如何不用注解以及具体对象来校验 Map 里的属性

  0576coder · 14 小时 38 分钟前 · 568 次点击

spring boot 里自带 validator 。使用方式基本上就是具体的类上加上注解比如我定一个 class A 然后在这个类里面加个属性 a1 加上注解 @NotNULL,这种。

目前有个需求是这样的,我有个结构不确定的 Map,但是我那边有个需求就是要做参数校验,比如我手动写是可以实现的。代码如下

        Map<String, Object> a = new HashMap<>();
        a.put("a1", 12);
        a.put("a2", "sad1231dsad");
        Object a2 = a.get("a2");
        if(a2 instanceof String) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }

我目前的期望是,我不想再实现一遍 validator 校验的功能,如何实现对一个动态的 map 参数校验 我的想法是入参是两个 map 一个 map 传参数校验的 rule 另一个 map 传需要被校验的 map

除了纯手写,有无更优雅的实现封装办法


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK