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

How to use custom annotations to verify restful request parameters

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

Share

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

This article is about how to use custom annotations to validate restful request parameters. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Custom annotations for verification of restful request parameters

When developing with springmvc, we usually add annotations such as @ NotNull () to the method parameter entity class in controller, and then add

@ Vilad annotation, so that when there is a request, it will automatically check whether the parameter is legal according to our annotation, avoiding our manual verification.

However, sometimes the self-contained verification annotations can not meet our business verification needs, so it is necessary for us to customize the verification annotations and customize us according to the business requirements.

Your own verification notes.

Let's look at an example:

1. First, we use @ interface to define an annotation @ Target ({METHOD, FIELD, ANNOTATION_TYPE}) @ Retention (RUNTIME) @ Constraint (validatedBy = ByteLengthValidator.class) / / specify the annotation verification implementation class using @ Constraint. This is a restricted annotation and can only use the specified implementation class @ Documentedpublic @ interface ByteLength {int min () default 0; int max () default 2147483647; String charsetName () default "GBK". String message () default "can only be between {min} and {max}"; 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report