1

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;
    }
}
Black Swan
  • 813
  • 13
  • 35
  • How is the validation supposed to work if you are creating the Student with new? Try sending a Student with a POST request and annotate the parameter with `@Valid` – burm87 Jan 18 '21 at 11:51
  • @burm87 what is the problem with new? – Black Swan Jan 18 '21 at 11:52
  • That creating the object with new is not gonna trigger the validation. It's an annotation based validation – burm87 Jan 18 '21 at 11:55
  • Also, @BlackSwan, when do you want this validation to happen? I guess when your API is called? – burm87 Jan 18 '21 at 12:00
  • @burm87 actually i will not use the Student as request body. I want to validate generally. – Black Swan Jan 18 '21 at 12:06
  • What does "generally" mean? :) At which point in time? When you create, when you save, when you retrieve from your db? – burm87 Jan 18 '21 at 12:10
  • @burm87 i will not use this class for any DB operation. it is DTO- Data Transfer Object. – Black Swan Jan 18 '21 at 12:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227488/discussion-between-burm87-and-black-swan). – burm87 Jan 18 '21 at 12:25
  • What is the reason for validating the object that you create yourself with hardcoded values? Are you planning to pass it to your endpoint via request body? – Sergiy Dakhniy Jan 18 '21 at 12:34

1 Answers1

2
  • Your entity will be validated automatically in case that your request body argument will be annotated with @Valid annotation
  • You can validate your entities manually by using: javax.validation.Validator

Following your case:

  • Inject Validator in BookController.
  • call Validator#validate using your DTO as a parameter.
  • Check if there are viloations
public class BookController {

    @Autowired
    Validator validator;

    // Find
    @GetMapping("/student")
    Student studentCreate() {
        Student student = new Student("","",35);
        student.setAge(36);
        Set<ConstraintViolation<Student>> result = validator.validate(student);
        if (!result.isEmpty()) {
                //do here whatever you want with each validation violation.
        }
        return student;
    }
}

For more details check official docs: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#validation

Note: Depends on your version of spring boot, org.springframework.boot:spring-boot-starter-validation dependency might be missing from your dependencies, so double check if it exists.

Vladlen Gladis
  • 1,699
  • 5
  • 19
  • 41