Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use notnull annotations

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use notnull annotations, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

NotNull annotations require that parameters or other specified variables cannot be NULL, but the annotation itself is just a tag that does not affect the logic of the code, so why do we need this annotation?

NotNull annotation exampl

For example, we already need to check whether the parameter is null at the entry of the following parameters, so why do we need to add NotNull annotations?

Publicvoidtest (Stringname) {

If (name==null) thrownewIllegalArgumentException ("nameisnull")

...

}

Notnull commonly used label

@ Null the annotated element must be null

@ NotNull the annotated element cannot be null

@ AssertTrue the annotated element must be true

@ AssertFalse the annotated element must be false

@ Min (value) the annotated element must be a number whose value must be greater than or equal to the specified minimum value

@ Max (value) the annotated element must be a number whose value must be less than or equal to the specified maximum value

@ DecimalMin (value) the annotated element must be a number whose value must be greater than or equal to the specified minimum value

@ DecimalMax (value) the annotated element must be a number whose value must be less than or equal to the specified maximum value

@ Size (max,min) the size of the annotated element must be within the specified range.

@ Digits (integer,fraction) the annotated element must be a number and its value must be within an acceptable range

@ Past the annotated element must be a past date

@ Future the annotated element must be a future date

@ Pattern (value) the annotated element must conform to the specified regular expression.

@ Email the annotated element must be an email address

@ Length the size of the annotated string must be within the specified range

@ NotEmpty the commented string must be non-empty

@ Range the annotated element must be within the appropriate scope

Example:

Check the data sent from the vo page

Inferface: just as a tag, a group can add multiple groups to a field validated by vo, so that groups that have not joined will not validate this field.

Controller: need to add @ Validated (GroupInterface1.class) / / GroupInterface1.class is a defined grouping GroupInterface2.class fields that need to be verified will not be verified

Thank you for reading this article carefully. I hope the article "how to use notnull Notes" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 255

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report