NutzCN Logo
问答 validation插件怎么集成
发布于 2222天前 作者 tanoak10 1751 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk
<dependency>
            <groupId>org.nutz</groupId>
            <artifactId>nutz-plugins-validation</artifactId>
            <version>1.r.63</version>
        </dependency>

导入jar包后怎么让注解生效呢,我看github上没有文档说明
https://github.com/nutzam/nutzmore/tree/master/nutz-plugins-validation

17 回复

就已经生效了,可以为入口方法的各参数添加注解了

来自炫酷的 NutzCN

@wendal
我直接再controller中方法这样使用没用额
@At
@Ok("json")
@Post

public Object get(@Param("id") @Validations(required = true,errorMsg = "id不为空") String id){
        try{
            return ApiResult.success(dictCodeService.fetch(id));
        }catch (Exception e){
            return ApiResult.error(e.getMessage());
        }
    }

当id的值为空时,还是会进入到方法内,不是直接抛异常

是空还是空字符串呢? 设置一下最小长度

设置如下

 @At
    @Ok("json")
    public Object te(@Param("id")@Validations(required = true,qq = true,errorMsg = "id为null") String id, Errors errors) {
        try {
            return ApiResult.success("ok");
        } catch (Exception e) {
            
            return ApiResult.error("fail");
        }
    }

浏览器还是能正常访问,没有报错
image.png

是不是自定义动作链了?MainModule贴来看看

module如下

@Modules(scanPackage = true, packages = "cn.wizzer")
@Ok("json:full")
@Fail("http:500")
@IocBy(type = ComboIocProvider.class, args = {
        "*json", "config/ioc/", "*anno", "cn.wizzer", "*jedis",
        "*tx", "*quartz", "*async", "*rabbitmq", "*wkcache","*weixin","*js","*slog"})
@Localization(value = "locales/", defaultLocalizationKey = "zh_CN")
@Encoding(input = "UTF-8", output = "UTF-8")
@SetupBy(value = Setup.class)
@ChainBy(args = "config/chain/nutzwk-mvc-chain.json")
@SessionBy(ShiroSessionProvider.class)
public class Module {
}

哦,nutzwk,那需要改一下config/chain/nutzwk-mvc-chain.json,把插件里面的处理器加进去,放在AdaptorProcessor之后

nutzwk-mvc-chain.json也没做任何改动

var chain={
  "default": {
    "ps": [
      "cn.wizzer.app.web.commons.processor.LogTimeProcessor",
      "cn.wizzer.app.web.commons.processor.GlobalsSettingProcessor",
      "org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor",
      "org.nutz.mvc.impl.processor.EncodingProcessor",
      "org.nutz.mvc.impl.processor.ModuleProcessor",
      "cn.wizzer.app.web.commons.processor.NutShiroProcessor",
      "cn.wizzer.app.web.commons.processor.XssSqlFilterProcessor",
      "org.nutz.mvc.impl.processor.ActionFiltersProcessor",
      "org.nutz.mvc.impl.processor.AdaptorProcessor",
      "org.nutz.mvc.impl.processor.MethodInvokeProcessor",
      "org.nutz.mvc.impl.processor.ViewProcessor"
    ],
    "error": 'org.nutz.mvc.impl.processor.FailProcessor'
  }
};

加了,发现还是没用

var chain={
  "default": {
    "ps": [
      "cn.wizzer.app.web.commons.processor.LogTimeProcessor",
      "cn.wizzer.app.web.commons.processor.GlobalsSettingProcessor",
      "org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor",
      "org.nutz.mvc.impl.processor.EncodingProcessor",
      "org.nutz.mvc.impl.processor.ModuleProcessor",
      "cn.wizzer.app.web.commons.processor.NutShiroProcessor",
      "cn.wizzer.app.web.commons.processor.XssSqlFilterProcessor",
      "org.nutz.mvc.impl.processor.ActionFiltersProcessor",
      "org.nutz.mvc.impl.processor.AdaptorProcessor",
      "org.nutz.plugins.validation.ValidationProcessor",
      "org.nutz.mvc.impl.processor.MethodInvokeProcessor",
      "org.nutz.mvc.impl.processor.ViewProcessor"
    ],
    "error": 'org.nutz.mvc.impl.processor.FailProcessor'
  }
};

@wendal debug进处理器,发现获取不到方法的参数
image.png

那个参数刚好是null吧

@wendal
不是的你看debuger
image.png
没有验证的参数,然后就直接跳过了

恩,重新翻了翻代码,只能对pojo里面的字段校验,没有对方法形参进行检查,来个pull request?或者报个issue了?

嗯嗯,好的

还没解决 ,我提个issue

要不要尝试改一下,来个pull request呢?

添加回复
请先登陆
回到顶部