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 are the SpringBoot group check and custom check annotations

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

Share

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

This article introduces what the SpringBoot group check and custom check notes are like, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Preface

In the daily development of   , parameter verification is a very important link. Strict parameter verification will reduce the probability of bug and increase the security of the interface. This article introduces some advanced checking methods. For example, in the process of writing an interface, you will definitely encounter that when the xxType value is A, the parama value must be passed. XxType value is B, paramB value must be passed. For such cases, the usual practice is to add various if judgments to controller. Obviously, such code is not elegant enough, and group checking and custom parameter checking are used to solve this problem.

PathVariable parameter check

The interface of    Restful should be quite common now. We all check the parameters of the address bar in common use.

/ * get phone number information * / @ GetMapping ("/ phoneInfo/ {phone}") public ResultVo phoneInfo (@ PathVariable ("phone") String phone) {/ / verify whether the phone number is valid or not String pattern = "^ [1] [3Ling 4Ling 5J 7J 8] [0-9] {9} $"; boolean isValid = Pattern.matches (pattern, phone) If (isValid) {/ / execute the corresponding logic return ResultVoUtil.success (phone);} else {/ / return error message return ResultVoUtil.error ("invalid mobile number");}}

Obviously the above code is not elegant enough, so we can validate it by adding the corresponding regular expression phone: regular expression after the parameter. This eliminates the need to write validation code in controller.

/ * get phone number information * / @ GetMapping ("/ phoneInfo/ {phone: ^ [1] [3Mae 4Ling 5Ling 7Ling 8] [0-9] {9} $}") public ResultVo phoneInfo (@ PathVariable ("phone") String phone) {return ResultVoUtil.success (phone);}

Although after this processing, the code is more concise. However, if the incoming mobile phone number does not comply with the rules, 404 will be returned directly. Instead of prompting for the wrong cell phone number. The error message is as follows:

Custom check comment

   We take checking mobile phone numbers as an example, although validation provides the annotation @ Pattern to use regular expressions for verification. If it is used in many places, once the regular expression changes, it needs to be modified one by one. Obviously, in order to avoid such futile work, custom check annotations are your good helper.

@ Datapublic class PhoneForm {/ * phone number * / @ Pattern (regexp = "^ [1] [3meme 4meme 5meme 7je 8] [0-9] {9} $", message = "incorrect phone number") private String phone;}

There are two main steps for    to implement a custom check annotation. One is the annotation itself, and the other is the verification logic implementation class.

PhoneVerify check Note @ Target ({ElementType.FIELD}) @ Retention (RetentionPolicy.RUNTIME) @ Constraint (validatedBy = PhoneValidator.class) public @ interface Phone {String message () default "incorrect Mobile number format"; Class [] groups () default {}; Class > getValidationGroups (CustomGroupForm form) {List

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