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

What to do with the big pit encountered by Java @ Validated

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

Share

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

This article mainly explains how to deal with the big pit encountered by Java @ Validated. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to deal with the big pit encountered by Java @ Validated.

The big pit encountered by Java @ Validated

I use @ Validated in two methods in one Controller, which is the method entered by the two POST methods. The entity classes of these two methods cannot be named the same (the content of the red box below), which will result in the second one not displaying the error message on the page (the first one can be displayed normally). The names of 1 and 3 in the following figure cannot be the same, and the names of 2 and 4 cannot be the same. When the same situation occurs, the page will not display an error message.

Use of Validated annotations

If you want to perform a validation operation, you must annotate the class with @ Validated

On the parameters that need to be verified, you can use the following to verify:

Each of the following comments has a message attribute that specifies the cause of the error

For example:

@ NotNull (message = "cannot be empty!!") If the private String firstName; is empty, the error is as follows

Property:person.firstName

Value:null

Reason: cannot be empty!

Null check

@ Null: verify whether the object is null

@ NotNull: verify whether the object is not null, unable to check a string of length 0

@ NotBlank: check whether the constraint string is Null and whether the length of the Trim is greater than 0, only for the string, and the spaces before and after are removed.

NotEmpty: check whether the constraint element is NULL or EMPTY.

Booelan check

@ AssertTrue: verify that the Boolean object is true

@ AssertFalse: verify that the Boolean object is false

Length check

@ Size (min=, max=): verify that the length of the object (Array,Collection,Map,String) is within the given range

Length (min=, max=): Validates that the annotated string is between min and max included.

Date check

@ Past: verify that the Date and Calendar objects are before the current time

@ Future: verify that the Date and Calendar objects are after the current time

@ Pattern: verify that String objects conform to the rules of regular expressions

Numerical check

It is recommended to use the Stirng,Integer type, but not the int type, because the form value "" cannot be converted to int, but it can be converted to "Stirng" and Integer to null.

@ Min: verify that the Number and String objects are larger than the specified value

@ Max: verify that the Number and String objects are smaller than the specified value

@ DecimalMax: the dimensioned value must not be greater than the maximum value specified in the constraint. The parameter to this constraint is a string representation of the maximum value defined by BigDecimal. Decimal precision exists

DecimalMin: the value to be dimensioned must not be less than the minimum value specified in the constraint. The parameter to this constraint is a string representation of the minimum value defined by BigDecimal. Decimal precision exists

@ Digits: verify whether the composition of Number and String is legal

@ Digits (integer=,fraction=): verifies whether the string is a number in the specified format. Interger specifies the integer precision and fraction specifies the decimal precision.

Range (min=, max=): checks whether the band value is between (including) the specified minimum and maximum values.

@ Valid: recursively check the associated object. If the associated object is a collection or array, then check the elements in it recursively, and if it is a map, check the value part of it. (whether to perform recursive verification)

@ CreditCardNumber: credit card verification

@ Email: verify whether it is an email address. If it is null, if it is not verified, it will pass the verification.

@ ScriptAssert (lang=, script=, alias=)

@ URL (protocol=,host=, port=,regexp=, flags=)

At this point, I believe you have a deeper understanding of "how to deal with the big pit encountered by Java @ Validated". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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: 0

*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