844 Star 3K Fork 927

GVPJbootProjects / jboot

 / 详情

使用Validator进行json格式的数据验证发生的问题

待办的
创建于  
2022-03-20 23:53

在jboot文档中有这样的描述

http://www.jboot.com.cn/docs/validator.html#notnull

如果是 ajax (或者 content-type 为 "application/json") 访问 /test 的时候,会返回如下的 json 信息:

{
    "throwable": "io.jboot.components.valid.ValidException: 不能为null",
    "errorMessage": "para is null at method: io.jboot.test.MyController.test(java.lang.String)",
    "errorCode": 400,
    "state": "fail",
    "message": "不能为null"
}

可实际上,最后一行的message却不是这样显示的,总是会把包名带出来,以至于自己写一个渲染器进行渲染的时候,要去对这个message的字段进行处理去掉包名。
以下是我这边真实的validErrorRender返回的内容:

{
    "throwable": "io.jboot.components.valid.ValidException: com.payhub.api.bean.PaymentCatalogueRequestBean.merchantID不能为空",
    "formName": "requestBean",
    "errorMessage": "requestBean is valid failed at method: com.payhub.api.controller.PaymentController.catalogue(com.payhub.api.bean.PaymentCatalogueRequestBean)",
    "errorCode": 400,
    "state": "fail",
    "message": "com.payhub.api.bean.PaymentCatalogueRequestBean.merchantID不能为空"
}

这个message并没有像文档一样显示。可能是存在一个bug的。

评论 (8)

Du Honghao 创建了任务

提供一下系统信息:
JDK 17 && osx && jboot latest.

我看到这两个数据返回的内容都差不多呀,你这边什么场景?需要什么样的数据呢?

海哥好,感谢您亲自答复:

message的信息不一样
"message": "不能为null"
"message": "com.payhub.api.bean.PaymentCatalogueRequestBean.merchantID不能为空"

就是,给客户端返回一个错误信息,总是暴露出包名很奇怪。我在bean里 @NotBlank(message = "不能为空")明明备注的是“不能为空”,结果却在message出现了包名不是很奇怪吗。

这个应该不是最新的版本吧 ,目前最新版本是 v3.14.2

Bean 中对于内容验证的设置:

    @NotBlank(message = "不能为空")
    private String merchantID;//获取商户id

Debug模式中,ValidException中拦截的message内容:
输入图片说明

好的,下个版本修复一下。

这个不是最新版本吧?

嗯嗯,我是用最新版本的。

        <jboot.version>3.14.2</jboot.version>

然后,感谢海哥采纳!

我刚刚看了一下

ValidUtil.java 中您对message进行了特殊处理


    public static void throwValidException(String fieldName, String message, Ret paras, String reason) {
        if (isParaMessage(message)) {
            message = fieldName + " " + Validation.buildDefaultValidatorFactory().getMessageInterpolator()
                    .interpolate(message, new SimpleContext(paras));
        }

        throw new ValidException(message, reason, fieldName);
    }

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(2)
181 duice 1578913742 61279 fuhai 1578915942
Java
1
https://gitee.com/JbootProjects/jboot.git
git@gitee.com:JbootProjects/jboot.git
JbootProjects
jboot
jboot

搜索帮助