• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constraint validation on RESTful resource method in Jersey project  RSS feed

 
Vani Kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am newbie to Jersey and Bean Validation and trying to do custom constraint validation on resource method form parameter.

I have the resource method as

public User findPostUser(@IdInterface @FormParam("userId") String fUserId) throws
IOException{
if(userMap.containsKey(Integer.valueOf(fUserId))){
return userMap().get(Integer.valueOf(fUserId));
Response.ok("200").entity(userMap().
get(Integer.valueOf(fUserId))).build();
return null;
}

Constraint Annotation as

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
import javax.validation.Constraint;
import javax.validation.Payload;

@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = UserIdValidator.class)
@Documented
public @interface IdInterface {
String message() default "{com.rest.validation.IdInterface.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

Constraint Validator as

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class IdValidator implements ConstraintValidator<IdInterface, String> {
public void initialize(IdInterface useridinterface) {

}
public boolean isValid(String value, ConstraintValidatorContext context) {
if(value!=null){
System.out.println("object is null");
return true;
}
return false;
}
}

I added Jersey-bean-validator jar file to lib folder of Tomcat web project.

I don't see the FormParam is being validated.

Is the above code sufficient to do bean validation with Jersey-bean-validatorl jar and Jersey with Tomcat.

Please let me know if i am missing something here.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!