1

I am working on a Spring REST application.
In that I have a REST Controller like this

@RequestMapping(value = "/patient", method = RequestMethod.POST)
        public BloodPressureDTO store(@RequestBody BloodPressureDTO bloodPressureDto) {

      .........
}

The BloodPressureDTO object has a Date object inside it.
I need to validate the Date object.

If the JSON that is posted in the request has an invalid date format, then I would like to handle that situation.

Soumitri Pattnaik
  • 3,246
  • 4
  • 24
  • 42

1 Answers1

-1

Good news is that you are already using Spring framework which has the flexibility to be configured with both JSR-303, JSR-349 bean validation or by implementing spring Validator interface to meet your requirements.

You can add @Valid @RequestBody to controller. The @Valid annotation is part of java bean validation API and is not a spring-specific construct. By adding it to the input parameter within a method in @Controller we will trigger validation.

You can do @NotNull etc simply for but for range, format, you need to extend the Validator & create something like @InDateRange

You can check date validation specific code here.

Kalyan Pradhan
  • 1,415
  • 3
  • 19
  • 34
Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24