In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "@ valid can not trigger BindingResult how to solve". In daily operation, I believe many people have doubts about how @ valid can not trigger BindingResult to solve the problem. The editor consulted all kinds of information and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt that @ valid can't trigger BindingResult. Next, please follow the editor to study!
Method parameter public String listFireEvent (@ Valid FireSearch fireSearch, HttpServletRequest request, BindingResult bindingResult) problem
If validation fails, an exception will be thrown directly instead of being put into bindingResult
Reason
@ Valid and BindingResult cannot have other classes
Solution
Put HttpServletRequest at the end.
That is:
Difference between public String listFireEvent (@ Valid FireSearch fireSearch, BindingResult bindingResult,HttpServletRequest request) @ Validated and @ Valid
The Spring validation validation framework for nested validation of input entities must add @ Valid instead of @ Validated to the corresponding attribute (field).
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. Among them, specific verification annotations for fields, such as @ NotNull, can be found all over the Internet, which are not detailed here.
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:
1. Grouping
@ Validated: provides a grouping function that allows you to use different verification mechanisms according to different groups when entering the verification. There are also materials on this network, which will not be described in detail. Valid: as a standard JSR-303 specification, it does not have the ability to absorb packets.
two。 Annotation place
Validated: can be used on types, methods, and method parameters. But cannot be used on member properties (fields)
Valid: can be used on methods, constructors, method parameters, and member properties (fields)
Whether they can be used on member properties (fields) directly affects the ability to provide nested validation.
3. Nested verification
When comparing the two nested validations, first explain what nested validation is. For example, we now have an entity called Item:
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, 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;}
Attribute is an entity that has its own verification mechanism, such as attribute and attribute value id cannot be empty, attribute name and attribute value cannot be empty, and so on.
Now we have an ItemController that accepts an input parameter of Item and wants to verify the Item, as shown below:
RestControllerpublic class ItemController {@ RequestMapping ("/ item/add") public void addItem (@ Validated Item item, BindingResult bindingResult) {doSomething ();}}
As 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 only 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 automatically nest the parameters before the method parameters. In other words, if the pid of Prop in the passed List is empty or negative, the input parameter verification will not be detected.
In order to be able to perform nested validation, you must manually specify on the props field of the Item entity that the entities in this field also need to be validated. Since @ Validated cannot be used on member properties (fields), but @ Valid can be added to member properties (fields), and the annotation of the @ Valid class also shows 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 that need nested validation classes to match @ Validated or @ Valid on method parameters.
We modify the Item class as follows:
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;}
Then we can use @ Validated or @ Valid on the addItem function of ItemController to nest and verify the input parameters of Item. At this point, if the props in Item contains the corresponding field of Prop is empty, the Spring Validation framework will detect it, and bindingResult will record the corresponding error.
Summarize the differences between @ Validated and @ Valid in nested validation capabilities:
Validated: it is not possible to provide nested validation alone on method input parameters. Cannot be used on member properties (fields), nor can you prompt the framework for nested validation. Can be used with the nested validation annotation @ Valid for nested validation.
Valid: it is not possible to provide nested validation alone on method input parameters. Can be used on member properties (fields) to prompt the validation framework for nested validation. Can be used with the nested validation annotation @ Valid for nested validation.
At this point, the study on "@ valid can not trigger BindingResult how to solve" is over. I hope to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.