1. Research javax.mail. Validation. Constraints. NotNull operation principle

1.1. Source code follow-up

  1. In order to findNotNullWhere exactly is it being processed, so I print an error message, and according to the keyword of the error message, the first thing I find is this place, you can see it’s webMVC package, understandable, after all, it’s requested through the interface and intercepted, it needs to go through WebMVC, right

  1. The next step is to explore how this statement is generated. Trace back to the source of the body value and find out where the exception was finally caughtDispatcherServletClass, feel ready to find, enter the handle method

  1. Keep looking inside and find the tossMethodArgumentNotValidExceptionWhere there’s an exception, you can see that the way to validate the parameters isvalidateIfApplicable

  1. If you go into this method, you can see your familiar friends,@ValidatedThis annotation needs to be added to any Controller interface that needs validation, and the core validation method after that isbinder.validateAnd then there are a lot of layers of branching, so I’m going to talk about a real problem I encountered finding the source path

Problem of 1.2.

  1. Only in thecontrollerLayer, write a@ValidatedNotes, after@NotNullAnnotations such as judgment are only available@ValidatedThe defined object is in effect, and now I want to implement itObjects within objects also implement validationI’m just going to jump to the conclusion here, so I wrote the following class, and I need the body object to be able to validate as well, so I add a@ValidAnnotation can be achieved, then look at the source code
@data public class Request<T> {/** * Request body */ @valid private T body; /** * private Integer requestCode; */ private Map<String,Object> extend; }Copy the code
  1. Due to the depth of too many layers, I will pick a few key screenshots, play a role in the role of the deep understanding must look at their own source code

  1. The core class is validated with the SpringValidatorAdapter

  2. I’m going to go through the annotations, and I’m going to skip a few layers

  1. Finally found the @VALID annotation that gets the object property

  2. Now that I have this cascading metadata, which I’ll use for subsequent judgments, valueContext, I’ve set the id attribute of the ExampleDeleteVo object to @notnull

  1. And you can see that it gets a value with ID null and puts it incurrentValue

1.3. Summarize

In the beginning, I looked at the @validated annotation to see if there was a way to validate the “Validated” object within an object. If not, I might have to write my own interceptor method. I don’t want to duplicate the “wheel” until I have to. If you have patience to finish reading this article, it is estimated that you have encountered the problem of @vaildated. I hope it can play a role in drawing inspiration from others