Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Custom Validations in Jersey

I want to Implement a validation in a jersey such that if I send a duplicate value of UserName or Email which already exists in DataBase then it should throw an Error saying UserName/Email already exists.

How can I acheive this?

I gone through this jersey documentation

https://jersey.java.net/documentation/latest/bean-validation.html

https://github.com/jersey/jersey/tree/2.6/examples/bean-validation-webapp/src

But I couldn't understood what exactly I have to follow to make my custom Jersey validations.

Suppose I send a Json in Body while Creating a User like:

 {  
     "name":"Krdd",
     "userName":"khnfknf",
     "password":"sfastet",
     "email":"[email protected]",
     "createdBy":"xyz",
     "modifiedBy":"xyz",
     "createdAt":"",
     "modifiedAt":"",

  }

Thanks in Advance for your helping hands.

like image 283
CandleCoder Avatar asked Aug 31 '15 05:08

CandleCoder


People also ask

Which type of Bean Validation method provided in Jersey?

Validation is the process of verifying that some data obeys one or more pre-defined constraints. It is, of course, a very common use case in most applications. The Java Bean Validation framework (JSR-380) has become the de-facto standard for handling this kind of operations in Java.

What is custom validation in Spring MVC?

It is necessary to validate user input in any web application to ensure the processing of valid data. The Spring MVC framework supports the use of validation API. The validation API puts constraints on the user input using annotations and can validate both client-side and server-side.

What is Jakarta validation API?

Jakarta Bean Validation is a Java specification which. lets you express constraints on object models via annotations. lets you write custom constraints in an extensible way. provides the APIs to validate objects and object graphs. provides the APIs to validate parameters and return values of methods and constructors.


1 Answers

Assuming you have a request instance of class:

public class UserRequest {

    // --> NOTICE THE ANNOTATION HERE <--
    @UniqueEmail(message = "email already registered")
    private final String email;

    public UserRequest(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }
}

You have to add a new annotation (and link it to your validator class using @Constraint):

@Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = { UniqueEmailValidator.class })
@Documented
public @interface UniqueEmail {
    String message();

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default { };

}

then you also have to implement the validation itself:

public class UniqueEmailValidator implements ConstraintValidator<UniqueEmail, UserRequest> {
    @Override
    public void initialize(UniqueEmail constraintAnnotation) {

    }

    @Override
    public boolean isValid(UserRequest value, ConstraintValidatorContext context) {
        // call to the DB and verify that value.getEmail() is unique
        return false;
    }
}

and you're done. Remember that Jersey is using HK2 internally so binding some sort of a DAO to your Validator instance can be tricky if you use Spring or other DI.

like image 153
Rafal G. Avatar answered Sep 19 '22 14:09

Rafal G.