How the springmvc @ Validated annotation turns on validation in service

problem description

  1. the server receives the json from the client in controller
  2. controller generates different request objects based on the type field in json, and calls the corresponding service to process it.
  3. before service processes request, the parameters should be determined first, and then the subsequent processing should be carried out.

normally, the request field should be verified directly in the controller, but my controller can only generate a specific request object based on the type field received in the json, so it can only be verified in the service. But the @ Validated object in the service method doesn"t seem to work. The field in
clipboard.png
request has @ NotBlank (message= "xxx") annotations, but it doesn"t work.

< hr >

what is the problem? Or the @ Validated annotation can only be used in controller?

Sep.16,2021

Spring validation validation framework for nested validation of input entities must add @ Valid instead of @ Validated to the corresponding attribute (field)

Menu