In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to implement the Java object data verification tool class VerifyUtils". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Background introduction
Excel data import is a very common function, but in many cases, the data imported by customers may not be stored in the database, such as long fields and wrong types. Wait. When the product manager proposes to do data verification, do you want a field to check? And there are dozens of tables in the system that need similar functions, not to mention thousands of pieces of data imported by customers, it must be unfriendly to prompt a field error every time. The request is to remind excel of what went wrong in some fields of a line.
1. Annotation class import java.lang.annotation.*;/** * @ author Gaoyang * @ date 2020-9-2 * @ jdk.version 1.8 * @ desc javaBean check note * / @ Target ({ElementType.FIELD}) @ Retention (RetentionPolicy.RUNTIME) @ Inherited@Documentedpublic @ interface Verify {/ * field name: * @ return * / String name () default "" / * * the maximum length of 0 will not take effect until the check is greater than 0 * @ return * / int maxLength () default 0; / * required * @ return * / boolean required () default false; / * whether it contains the special character * @ return * / boolean specialCharacters () default false / * Digital verification * @ return * / boolean number () default false; / * regular * @ return * / Regexp regexp () default @ Regexp ();} / * * @ author Gaoyang * @ date 2020-9-2 * @ jdk.version 1.8 * @ desc for verifying the regular annotation class * / public @ interface Regexp {String regexp () default " String message () default "";} 2. Check tool class import cn.hutool.core.util.NumberUtil;import cn.hutool.core.util.ReUtil;import cn.hutool.core.util.ReflectUtil;import org.apache.commons.lang3.StringUtils;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.List / * * @ author Gaoyang * @ Type VerifyUtils.java * @ date 2020-9-2 * @ jdk.version 1.8 * @ desc javaBean Verification tools * / @ Verify (maxLength = 10, required = true, number = true) / / private String f32 rampant Universe verify (regexp = @ Regexp (regexp = "\\ w + [\ u4E00 -\ u9FFF] +\\ d +", message = "regular mismatch"), specialCharacters = true) / / private String f33 Public class VerifyUtils {/ * check object * * @ param obj object * @ return error message * / public static List verify (Object obj) {List stringList = new ArrayList (); Field [] fields = obj.getClass () .getDeclaredFields () For (Field f: fields) {if (f.isAnnotationPresent (Verify.class)) {StringBuilder stringBuilder = new StringBuilder (); Verify verify = f.getAnnotation (Verify.class); Object object = ReflectUtil.getFieldValue (obj, f.getName ()); String value = null; if (object! = null) value = object.toString () If (verify.required ()) {if (StringUtils.isBlank (value)) {stringBuilder.append ("Field cannot be empty |") }} if (StringUtils.isNotBlank (value)) {if (verify.specialCharacters ()) {if (value.contains ("|) | | value.contains ("'") | | value.indexOf ('") >-1) {stringBuilder .append ("contains illegal characters |") } if (value.length ()! = value.trim () .length ()) {stringBuilder.append ("cannot contain spaces before and after |") }} if (verify.maxLength () > 0) {if (value.length () > verify.maxLength ()) {stringBuilder.append ("content length is too long |") }} if (verify.number ()) {if (! NumberUtil.isNumber (value)) {stringBuilder.append ("not a numeric type |") } Regexp regexp = verify.regexp (); if (StringUtils.isNotBlank (regexp.regexp () {boolean isMatch = ReUtil.isMatch (regexp.regexp (), value) If (! isMatch) {stringBuilder.append (regexp.message ()) .append ("|");} if (StringUtils.isNotBlank (stringBuilder.toString () {String name = verify.name () If (StringUtils.isBlank (name)) {name = f.getName ();} String message = name + ":" + stringBuilder.substring (0, stringBuilder.toString (). Length ()-1); stringList.add (message) } return stringList;}} 3. test
At present, only non-empty, type, length and special characters have been done, and the current implementation is only to meet the business needs of the company, which can be modified and improved if necessary.
This is the end of the content of "how to implement the Java object data validation tool class VerifyUtils". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.