Using Fluentvalidator to validate plug-ins in Springboot

Source: Internet
Author: User
Tags manual writing

Objective

When we write a project, it is often necessary to verify some data in the controller. This is not to say that for these data services above the checksum, but the data are null or the length of the calibration.

Sometimes the checksum can be omitted, according to the needs of the business adjustment, and in most cases, the server has been using the client is not a trust policy, so many parameters need to be verified.

The purpose of verification is to execute the business logic after the execution in a safe and orderly manner. In order to reduce the pressure of the database to a certain extent, do not let the database become your tool to verify the data.

But this kind of verification is always very complex and very bloated, reusability is not high, so I am ready to use fluentvalidator this plugin to help me complete this task.

has been added to my GitHub template: https://github.com/LinkinStars/springBootTemplate

Fluentvalidator Introduction

Fluentvalidator Baidu is an open source authentication plug-in, I used to call it, simply said, it is to verify the data is simple and reusable and exist.

With this plugin, you can connect to the received data for the verification you need, and return the corresponding error message, and it also provides some good display error function.

In fact, I mainly use it because of its high reusability and code simplicity.

Fluentvalidator use

Importing dependencies using Gradle

Compile (group: ' Com.baidu.unbiz ', Name: ' fluent-validator-jsr303 ', version: ' 1.0.9 ') {
Exclude module: ' Slf4j-log4j12 '
you need to be aware of the need to prevent jar collisions, so you need to exclude some dependencies that you can use according to your needs. or if you use maven or download the jar yourself, you can

New Notnullstringvalidator.java

PackageCom.linkinstars.springBootTemplate.validator;ImportCom.baidu.unbiz.fluentvalidator.ValidationError;ImportCom.baidu.unbiz.fluentvalidator.Validator;ImportCom.baidu.unbiz.fluentvalidator.ValidatorContext;ImportCom.baidu.unbiz.fluentvalidator.ValidatorHandler;/*** String non-null check *@authorLinkinstar*/PublicClass NotnullstringvalidatorExtends validatorhandler<string>Implements Validator<string>{//The field name of the string that needs to be validatedPrivateString FieldName;PublicNotnullstringvalidator (String fieldName) {This.fieldname =FieldName; }/*** Check method *@paramcheckedstring needs to be checked string * /@Override public  boolean Validate (Validatorcontext context, string checkedstring) { if (null = = Checkedstring | | "" = = checkedstring) {context.adderror (Validationerror.create (String.Format ("%s" cannot be empty!) ", FieldName)). Seterrorcode ( -1). SetField (FieldName). Setinvalidvalue (checkedstring)); return false;} return true;}}                

Use this in places where verification is required

test the checksum string parameter string        checkedstring = "";        Result Validatorresult = fluentvalidator.checkall ()                new Notnullstringvalidator ("Test name"if (!  Validatorresult.issuccess ()) {System.out.println (validatorresult.geterrors ());}     

If you need to validate multiple fields, proceed directly after.

. On (CheckedString1, New Notnullstringvalidator ("Test Name")) . On (CheckedString2, New Notnullstringvalidator ("Test Account")) If you need to verify a different situation, such as verifying the size of the number, verifying the phone number, etc., just write the new validator class . This is just an example of a simple validation of a string.

Summarize

using this method, you can reuse a lot of validation rules, and return the error message you need, and it provides the error code can define the number itself, the checksum will become very clear. Use the time to pay attention to the following points, the first is to clear the rules, not blind verification, there are some times need special check the manual writing does not necessarily have to use the plug-in check. This plugin also has a lot of features, I just put forward the functions and methods I need, more specific usage can refer to:

http://ju.outofmemory.cn/entry/241915

Use Fluentvalidator validation plug-in in Springboot

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.