简体   繁体   中英

JSR 303 Bean Validation in java SE or inside jaxb

I receive xml and convert it to bean via jaxb. I need to validate xml and I don't want to use xsd or other xml validation methods. I like to use annotations and bean validation methods. How can I validate bean in java se environment? But the best solution is implement JSR 303 validation in jaxb framework

Update

I initially interpreted the question as "how do I make JAXB schema-derived classes JSR 303-validatable". After the discussion in appeared that the OP had a different question. Still, I'll leave my answer as I think it may be helpful for other people who find this question via keywords.

But it is not the answer to OP's question, sorry for this.


Please see the krasa-jaxb-tools Jsr303Annotations plugin.

Generates:

  • @Valid annotation for all complex types, can be further restricted to generate only for types from defined schema: -XJsr303Annotations:targetNamespace= http://www.foo.com/bar
  • @NotNull annotation for objects that has a MinOccur value >= 1 or for attributes with required use
  • @Size for lists that have minOccurs > 1
  • @Size if there is a maxLength or minLength or length restriction
  • @DecimalMax for maxInclusive restriction
  • @DecimalMin for minInclusive restriction
  • @DecimalMax for maxExclusive restriction, enable new parameter (inclusive=false) with: -XJsr303Annotations:JSR_349=true
  • @DecimalMin for minExclusive restriction, enable new parameter (inclusive=false) with: -XJsr303Annotations:JSR_349=true
  • @Digits if there is a totalDigits or fractionDigits restriction.
  • @Pattern if there is a Pattern restriction

I cant understand how can I run krasa-jaxb-tools, I found another solution. It is hibernate-validator dependencies:

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>2.2.4</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>javax.el</artifactId>
        <version>2.2.4</version>
    </dependency>

and java code:

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
x = validator.validate(bean).size(); // x=0 good; x!=0 bad

it is not beautiful but it is works

I have a similar situation. I developp REST services with Java 7, Jax-RS (cxf), Spring (core), within a servlet container (Tomcat 7). I don't have a persistance layer (it's a search backend based on Lucene/Solr), I don't use any managed environment (except Spring for DI).

I had a pojo class (MyPojo) and I created a constraint (MyPojoConstraint) and its underlying validator (MyPojoValidator).

Note: the pojo instances are not created via Spring Note: the constraint is a class level constraint

So, MyPojo is someting like:

@MyPojoConstraint
public class MyPojo {

// members

// getters/setters
}

At the beginning, I thought putting the @MyPojoConstraint annotation would be enough/magic (ie that when creating an instance of the class, the validation will be done).

But nothing happened. I tried using the @Valid annotation, using it on the constructor. But it did not work.

I ended up writting this kind of code:

MyPojo p = new MyPojo(....);

// Proceed to validation
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<SuggestMRRequestParameters>> constraintViolations = validator.validate(p);
if(constraintViolations.size() > 0){
            throw new ConstraintViolationException(constraintViolations);
}

My understanding now is:

  • if you have "managed beans" (via Spring or an entity manager) whose lifecycle is managed by a container, then this container will take care of executing the validation
  • if you have only pojo / unmanaged beans, you have to programatically execute the validator: in some context, I guess AOP could be usefull to do this and avoid writing too much boilerplate code

I will greatly appreciate if someone can confirm my understanding or correct me if I'm wrong

Thanks

Philippe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM