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 is the difference between @ Validated and @ Valid in Spring

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what is the difference between @ Validated and @ Valid in Spring". In daily operation, I believe many people have doubts about the difference between @ Validated and @ Valid in Spring. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "what is the difference between @ Validated and @ Valid in Spring"! Next, please follow the editor to study!

Basic concept

The Spring Validation verification framework provides @ Validated (Spring's JSR-303 specification, a variation of standard JSR-303) for parameter verification, and javax provides @ Valid (standard JSR-303 specification). With BindingResult, you can directly provide parameter verification results. Specific validation annotations for fields, such as @ NotNull.

@ Valid and @ Validated annotations

In Spring, we use the @ Valid annotation of JSR-303 for method-level validation. In addition, we use it to mark member attributes for validation. However, this comment does not support group validation.

Group validation helps to limit the constraints applied during validation. The UI wizard is a special use case. Here, as a first step, we may have a specific field subgroup. In the next steps, there may be another group that belongs to the same bean. Therefore, we need to apply constraints to these limited fields at each step, but @ Valid does not support this constraint.

In this case, at the group level, we must use @ Validated of Spring, which is a variation of @ Valid of this JSR-303. Used at the method level. To mark up the member attributes, we continue to use the @ Valid annotation.

Main differences

When checking whether the input parameters of Controller conform to the specification, there is not much difference in basic validation functionality by using @ Validated or @ Valid. However, there are two differences in grouping, annotation location, nested validation, and so on:

@ Validated@Valid grouping provides grouping function, and different authentication mechanisms can be adopted according to different groupings when entering parameter verification. No grouping function

Annotable position

Can be used on types, methods, and method parameters. However, it can not be used on member properties, it can be used on methods, constructors, method parameters and member properties (whether they can be used on member properties that directly affect the ability to provide nested validation) nested validation

The nested validation function cannot be provided alone on the method input parameter.

Cannot be used on member properties.

Nor can it provide a framework for nested validation.

Can be used with the nested validation annotation @ Valid for nested validation.

The nested validation function cannot be provided alone on the method input parameter.

Can be used on member properties to prompt the validation framework for nested validation.

Can be used with the nested validation annotation @ Valid for nested validation.

What is nested validation? Public class Item {@ NotNull (message = "id cannot be empty") @ Min (value = 1, message = "id must be a positive integer") private Long id; @ NotNull (message = "props cannot be empty") @ Size (min = 1, message = "at least one attribute") private List props;}

Item comes with many attributes, including attribute id, attribute value id,props attribute name and attribute value, as shown below:

Public class Prop {@ NotNull (message = "pid cannot be empty") @ Min (value = 1, message = "pid must be a positive integer") private Long pid; @ NotNull (message = "vid cannot be empty") @ Min (value = 1, message = "vid must be a positive integer") private Long vid; @ NotBlank (message = "pidName cannot be empty") private String pidName; @ NotBlank (message = "vidName cannot be empty") private String vidName;}

The props property also has its own validation mechanism, such as the property value cannot be empty.

Now test, receive the input parameters of Item, and want to verify the Item, as follows:

RestControllerpublic class ItemController {@ RequestMapping ("/ item/add") public void addItem (@ Validated Item item, BindingResult bindingResult) {doSomething ();}}

In the figure above, if the props attribute of the Item entity is not annotated, there are only NotNull and @ Size. Regardless of whether the input parameter is verified by @ Validated or @ Valid, the Spring Validation framework will do non-null and quantity verification on the id and props of Item, and will not perform field verification on the Prop entity in the props field. That is, @ Validated and @ Valid will not perform nested verification of the parameters before the method parameters.

In order to be able to perform nested validation, you must manually make it clear on the props field of the Item entity that the entities in this field are also validated. Since @ Validated cannot be used on member attributes, but @ Valid is added to member attributes, and the @ Valid class annotation also indicates that it supports nested validation, we can infer that @ Valid does not automatically perform nested validation when added to method parameters, but is used on the corresponding fields of the nested validation class to match @ Validated or @ Valid on method parameters.

The modified Item class:

Public class Item {@ NotNull (message = "id cannot be empty") @ Min (value = 1, message = "id must be a positive integer") private Long id; @ Valid / / nested validation must use @ Valid @ NotNull (message = "props cannot be empty") @ Size (min = 1, message = "props must have at least one custom attribute") private List props } at this point, the study on "what is the difference between @ Validated and @ Valid in Spring" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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