JSR-303兼容豆验证为Android [英] JSR-303 compatible bean validator for Android

查看:208
本文介绍了JSR-303兼容豆验证为Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

难道真的有可能有针对Android没有JSR-303兼容验证身边?

Is it really possible that there is no JSR-303 compatible validator for Android around?

Hibernate验证似乎与Android(ClassNotFound的异常),然后不兼容......我找不到别的了。

Hibernate validator seems not compatible with Android (ClassNotFound Exception) and then... I could not find anything else.

我发现Metawidget - 但在他们的网站,他们说Metawidget自带UI组件,原产于现有的前端。 - 我并不需要额外的UI组件

I found Metawidget - but on their Website they say "Metawidget comes with a UI component native to your existing front-end" - I don't need additional UI components.

下面是我在后端:

    final String wrongJson = "{\"name\":\"notavalidemail\"}";
    final Gson gson = new Gson();
    final Project projectFromJson = gson.fromJson(wrongJson, Project.class);

    final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    final Validator validator = factory.getValidator();

    final Set<ConstraintViolation<Project>> constraintViolations = validator.validate(projectFromJson);
    assertNotNull(constraintViolations);

    for (final ConstraintViolation<Project> violation : constraintViolations) {
        logger.debug("Errormessage: {}",violation.getMessage());
    }

要避免一堆IFS的我想在Android上是相同的 - ?任何提示

To avoid a bunch of ifs I want the same on Android - any hints?

[更新]
对不起 - 这不是ClassNotFound的是

[Update] Sorry - it's not ClassNotFound it is

javax.validation.ValidationException:无法创建一个配置,因为没有Bean验证提供者可能被发现。像Hibernate验证器(RI)提供商加入到类路径中。
在javax.validation.Validation $ GenericBootstrapImpl.configure(Validation.java:271)
在javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:110)
在at.mikemitterer.mobiad.android.util.ProjectTest.testWithWrongJsonString(ProjectTest.java:90)
在java.lang.reflect.Method.invokeNative(本机方法)
在android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
在android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
在android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
在android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1661)

javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath. at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:271) at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:110) at at.mikemitterer.mobiad.android.util.ProjectTest.testWithWrongJsonString(ProjectTest.java:90) at java.lang.reflect.Method.invokeNative(Native Method) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

[更新II ]
是 - 休眠 - 验证 - 5.1.0.Alpha1.jar在我的lib文件夹

[Update II] Yes - hibernate-validator-5.1.0.Alpha1.jar is in my lib folder

推荐答案

我来到了这个问题,下面我追求一些集成JSR 343和Angular.js的,这应该是比你的情况更麻烦,还有,与Android,你使用Java开发。

I came to this question, following my quest for some integration of JSR 343 and Angular.js , which should be more trickier than in your case , as well, with Android you develop with Java.

如果你看一下 valdr 你会看到模​​型为中心的方法框架在angular.js验证和你会看到有一个插件来支持JSR 303。
该插件是由几个部分组成,你可能要考虑模仿在Android中,对于客户端 - 服务器应用程序,并有一个中心位置定义的验证规则。
为此,您可能会需要以下内容:

If you look at valdr you will see a framework with model centric approach to validation in angular.js and you will see there a plugin to support JSR 303. The plugin is composed of several components, you might want to consider "imitating" in Android, for client-server applications and have one "central place" that defines the rules of validation. For this you will probably need the following:


  1. 定义的类在服务器端扫描JSR 303注解

  2. 哪些类,你的Andr​​oid应用映射到这些Java豆类

  3. 办法让扫描结果和dynmically构建一套规则

  4. 当被定位在对象上的setter被激活使用每个规则。

如果这是没有服务器的Andr​​oid应用程序,那么我想你应该尝试端口JSR 303的源与Android使用

If this is an android app with no server, then I guess you should try to port the source for JSR 303 to be use with Android

这篇关于JSR-303兼容豆验证为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆