In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to verify the custom comments of the input request in SpringBoot, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
In our daily programming, we use many Spring Boot default annotations that can be used for validation, such as @ NotNull, @ Size, @ NotBlank, @ Digits, and so on, which is a cool way to validate any incoming requirements.
Consider a scenario where some fields are optional by default and must be mandatory if others are populated with specific values.
Spring does not have predefined comments for this kind of validation.
Let's give some examples of how we can simplify the validation process, make its code reusable, and introduce abstraction at the annotation level.
In a typical sales platform, there will be sales operations and invalid sales operations. This amount is mandatory in the sales operation, and if the sales operation is invalid, the write-off type will be mandatory.
Our dto class is as follows:
Public class IncomingRequestDto {public TransactionType transactionType; public ReversalType reversalType; public String reversalId; public AmountDto amountDto;}
IncomingRequestDto has several properties, such as transactionType and reversalType as ENUMS.
Public enum TransactionType {SALE {public String toString () {return "Sale";}}, VOIDSALE {public String toString () {return "VoidSale";}},} public enum ReversalType {TIMEDOUT {public final String toString () {return "Timedout" }, CANCELLED {public final String toString () {return "Cancelled";}
And amountDto are:
Public class AmountDto {public String value;}
Scenario 1: amountDto.value is conditional. When we receive a request with transactionType= "SALE", amountDto.value should be mandatory.
Scenario 2: reversalType is conditional. When we receive a request with transactionType= "VOIDSALE", reversalType should be mandatory.
Let's first define a comment with the attributes required for the validation process:
@ Target ({TYPE, ANNOTATION_TYPE}) @ Retention (RUNTIME) @ Documentedpublic @ interface NotNullIfAnotherFieldHasValue {String fieldName (); String fieldValue (); String dependFieldName (); String message (); Class [] groups () default {}; Class
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.