JSR-303 compatible bean validator for Android

孤街浪徒 提交于 2019-12-24 01:27:32

问题


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

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

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.

Here is what I have on the backend:

    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());
    }

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

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

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)

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


回答1:


Whiche CNF did you exactly get? If it is about java.beans.Introspector, this is fixed as of HV 5.1.0.Alpha1 which doesn't import this type anymore and thus should run on Android. If it is about other APIs it would be very interesting to know which ones.




回答2:


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.

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. Define the classes at server side to scan for JSR 303 annotations
  2. Which classes that your android app are mapped to these java beans
  3. A way to get the scanning result and dynmically construct a set of rules
  4. Use each rule when a setter on an object that was mapped is being activated.

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



来源:https://stackoverflow.com/questions/19551882/jsr-303-compatible-bean-validator-for-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!