I have normal Java POJO class, which is not an entity class, named Student. I am using spring boot validator. within my REST controller when, for a request just a student object will be returned. I have set different validation in the Student class. But the validation is not working. I have given the age limit 10 to 30. But it is creating Object with 35 years old. Is there any way to make this validation work? Note : i am doing this as test, if this validation work, i will use it within my main project.
Student
package com.mkyong;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
@Validated
public class Student {
@NotEmpty
private String id;
@NotEmpty
private String name;
@Min(10)
@Max(30)
private int age;
public Student() {
}
public Student(String id, String name, @Min(10) @Max(30) int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge( @Min(10) @Max(30) int age) {
this.age = age;
}
}
**Rest Controller: **
public class BookController {
// Find
@GetMapping("/student")
Student studentCreate() {
Student student = new Student("","",35);
student.setAge(36);
return student;
}
}