Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use nested objects @ NotBlank @ NotEmpty @ NotBlank

Shulou Source: shulou.com Published: 2022-06-02 02:59:50 10月05日 Update

This article mainly explains "how to use nested objects @ NotBlank @ NotEmpty @ NotBlank". The explanation in this article is simple, clear and easy to learn and understand. Please follow the editor's ideas to study and learn "how to use nested objects @ NotBlank @ NotEmpty @ NotBlank".

@ NotEmpty: cannot be null, and length must be greater than 0

@ NotBlank: used only on String, indicating that the value passed in cannot be null, and the length must be greater than 0 after calling trim ()

@ NotNull: cannot be null, but can be empty

If there are nested objects, just add @ Valid to the defined object

@ Valid

The way to use it is as follows, add @ Valid where the parameter is received

You can also add a global exception handling, which can also avoid writing a bunch of if to determine whether the data is empty. Of course, this also has some limitations. If you want to verify the validity of other data, you need to handle it separately, or custom annotations can be done.

@ ControllerAdvice@RestControllerpublic class GlobalExceptionHandler {@ ExceptionHandler (MethodArgumentNotValidException.class) @ ResponseBody public ResultMsgModel handleValidException (MethodArgumentNotValidException e) {Log.logger.error ("Parameter check failure" + e.getParameter () .getMethod () + "+ e.getBindingResult () .getAllErrors () .get (0) .getDefaultMessage ()); return new ResultMsgModel (ResponseCode.RETURN_VERIFICATION_FAIL,e.getBindingResult () .getAllErrors () .get (0) .getDefaultMessage () }} Thank you for your reading. The above is the content of "how to use nested objects @ NotBlank @ NotEmpty @ NotBlank". After the study of this article, I believe you have a deeper understanding of how to use nested objects @ NotBlank @ NotEmpty @ NotBlank. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Objects learning content parameters data length processing verification legality global legitimacy that is limitations ideas situations articles methods more notes knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL Huawei Shulou Tech Info Xiaomi