5

I want my RestController to return an error when the input of date is set(required = false) but with the wrong regex(f. e. 10-2019)

  @GetMapping
  @Timed
  public ResponseEntity<ResponseBodyWrapper<List<ListData>>> getList(
     @RequestParam(name = "date", required = false) @Pattern(regexp = "[0-9]{4}-[0-9]{1,2}") String date) {
    // Logic
  }

However, the validation isnt taking place. I was expecting an error but none was thrown, the error occured later, when i tried to build a new Object with the wrong input

2 Answers 2

6

Fix the validation with @Validated

When you want the validation to be triggered, the controller class must be annotated with @Validated:

@Validated
@RestController
public class MyController {
   ...
}

Quoting the documentation:

To be eligible for Spring-driven method validation, all target classes need to be annotated with Spring’s @Validated annotation.

Don't use @Timed

I find it weird the fact your controller method is annotated with @Timed. This is a test-specific annotation and it's not meant to be used in the actual code:

Test-specific annotation to indicate that a test method has to finish execution in a specified time period.

You may also want to review your dependencies and ensure they use the correct scope.

Consider YearMonth instead of String

As you seem to be receiving a year and month value in your controller method, it's better to use YearMonth instead of String:

A year-month in the ISO-8601 calendar system, such as 2007-12.

Your controller method can be like:

@GetMapping
public ResponseEntity<Foo> getList(@RequestParam(name = "date", required = false) YearMonth date) {
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Validated worked We are not using org.springframework.test.annotation@Timed for testing we are using io.micrometer.core.annotation@Timed for our Grafana Thank you very much, we are now using YearMonth instead of String, this way it is way more reliable and reusable. Only thing, because we don't use ISO-Standard we had to implement an Pattern Converter with Regex: ^(\\d{4})-(1[0-2]|[1-9]|0[1-9])$
Thank you, the @Validated works for me, saved me hours of searching other configs.
2

You need to add the @Validated annotation to the class:

@RestController
@Validated
public class myRestController{

@Timed
  public ResponseEntity<ResponseBodyWrapper<List<ListData>>> getList(
     @RequestParam(name = "date", required = false) @Pattern(regexp = "[0-9]{4}-[0-9]{1,2}") String date) {
    // Logic
  }
}

References: https://www.baeldung.com/javax-validation-method-constraints point 3.

3 Comments

Does not work for me unfortunatelly, still same behavior
Do you have your controller class annotated as @Validated ? Can you try with this change? As I edited on my Answer, you don't need @Valid annotation if you are using @Validated on the class
Yes thank you, that was the problem, forgot the @Validated annotation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.