• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constraint validation on RESTful resource method in Jersey project  RSS feed

Vani Kumar
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
return userMap().get(Integer.valueOf(fUserId));
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 })
@Constraint(validatedBy = UserIdValidator.class)
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) {
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!