I searched for a @NotNull java annotation and found the one from javax. I tried to use it but ran into the same issues described here. In short: I need to set up some stuff to get it working - but I actually don't need all that (I am in plain Java/Groovy context, no JavaEE). Are there alternatives to the mentioned annotation which work standalone, where to find those?
            Asked
            
        
        
            Active
            
        
            Viewed 1,950 times
        
    2
            
            
         
    
    
        Community
        
- 1
- 1
 
    
    
        valenterry
        
- 757
- 6
- 21
- 
                    You might find some answers here http://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use – Jan Chimiak Nov 27 '14 at 10:24
2 Answers
2
            
            
        oval can help you with this.
download the jar from http://mvnrepository.com/artifact/net.sf.oval/oval/1.31 and look at the documentation here http://oval.sourceforge.net/
for example:
import net.sf.oval.constraint.MaxLength;
import net.sf.oval.constraint.NotEmpty;
import net.sf.oval.constraint.NotNull;
public class Request {
@NotNull
@NotEmpty
@MaxLength(value = 30)
private String id;
//.....getters setters......
}
above will be your pojo
/**
* Method returns if valid request or not
*/
private boolean isValid(Request request) {
List<ConstraintViolation> violations = validator.validate(request);
if (violations.size() > 0) {
    return false;
} else {
    return true;
}
}
and will do validation like above.
you can also find many more examples online.
 
    
    
        sasuke
        
- 595
- 1
- 5
- 15
0
            
            
        You can use the validation quite fine just with groovy. There is the hibernate-validator implementation. e.g.
@Grapes([
        @Grab('javax.validation:validation-api:1.1.0.Final'),
        @Grab('org.glassfish:javax.el:3.0.0'),
        @Grab('org.hibernate:hibernate-validator:5.1.3.Final'),
        ])
import javax.validation.*
import javax.validation.constraints.*
class Pogo {
    @NotNull
    String name
    @NotNull
    @Min(1L)
    Long size
}
def validator = Validation.buildDefaultValidatorFactory().getValidator()
println validator.validate(new Pogo()).collect{ "Error on $it.propertyPath: $it.message" }
//; [Error on name: may not be null, Error on size: may not be null]
println validator.validate(new Pogo(name:"x", size:0)).collect{ "Error on $it.propertyPath: $it.message" }
//; [Error on size: must be greater than or equal to 1]
 
    
    
        cfrick
        
- 35,203
- 6
- 56
- 68