Home > OS >  @NotEmpty and @notNull not working during deserialization
@NotEmpty and @notNull not working during deserialization

Time:01-20

I have a piece of code which deserialize json string to model object,However I have provided @NotNull and @NotEmpty Validation but its not working, Below is the code:-

   private Student getDecryptedRequestData(String studentRequest) throws JsonParseException, JsonMappingException, IOException {
    
    return  new ObjectMapper().readValue(studentRequest,Student.class);
}

Student.java

   import javax.validation.constraints.NotEmpty;
   import javax.validation.constraints.NotNull;

   import com.fasterxml.jackson.annotation.JsonProperty;

   public class Student{


    @NotEmpty
    private String studentName;

    @NotNull
    private Long roll;

   public String getStudentName() {
    return studentName;
   }

public void setStudentName(String studentName) {
    this.studentName= studentName;
}


public Long getRoll() {
    return roll;
}

public void setRoll(Long roll) {
    this.duration = roll;
}

}

CodePudding user response:

before you return Student you can use

Validation.buildDefaultValidatorFactory().getValidator().validate(Student);

CodePudding user response:

Check for the required dependency that you need to have in order to use those annotations.

  •  Tags:  
  • Related