Check-restcontroller advanced for client incoming data

Source: Internet
Author: User

using Hibernate validator for data validation

Bean validation annotations (need to join related dependencies, can be used directly in Springboot, Springboot will help us to join directly)

@Null Verify that the object is empty (the property must be empty and the client cannot pass in this property, otherwise it will be an error)

@NotNull Verify that the object is non-empty (the property is not empty, the customer must pass in this property, or it will be an error)

@Min Verify that number and string objects are greater than or equal to the specified value

@Max Verify that number and string objects are less than or equal to the specified value

@Size Verify that the length of the object (array,collection,map,string) is within a given range

@Past Verify that date and Calendar objects are before the current time

@Future Verify that date and Calendar objects are after the current time

@AssertTrue verifies whether a Boolean object is True

@AssertFalse Verify that the Boolean object is False

@Valid-Cascade validation annotations

Annotations can be used on member variables:

Package Com.example.demo;import Java.util.date;import java.util.list;import javax.validation.valid;import Javax.validation.constraints.decimalmin;import Javax.validation.constraints.notnull;import Javax.validation.constraints.null;import Javax.validation.constraints.past;import Javax.validation.constraints.size;import Com.fasterxml.jackson.annotation.JsonFormat; Public classtvseriesdto {@NullPrivate intID; @NotNullPrivateString name; @DecimalMin ("1")Private intSeasoncount; //the @Valid represents a cascade check, and @Size (min=2) indicates that the list must have at least 2 items, or it will not pass the checksum@Valid @NotNull @Size (min=2)PrivateList<tvcharacterdto>tvcharacters; //if you want to represent a date with a long timestamp, you can: @JsonFormat (shape = JsonFormat.Shape.NUMBER)@JsonFormat (timezone="gmt+8", pattern="YYYY-MM-DD")    //@Past indicates that only the past time is accepted, and is considered unqualified later than the current time@PastPrivateDate originrelease;  Publictvseriesdto () {} PublicTvseriesdto (intID, String name,intSeasoncount, Date originrelease) {         This. ID =ID;  This. Name =name;  This. Seasoncount =Seasoncount;  This. Originrelease =originrelease; }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetseasoncount () {returnSeasoncount; }     Public voidSetseasoncount (intSeasoncount) {         This. Seasoncount =Seasoncount; }     PublicDate getoriginrelease () {returnoriginrelease; }     Public voidsetoriginrelease (Date originrelease) { This. Originrelease =originrelease; } @Override PublicString toString () {return "Tvseriesdto [id="+ ID +", Name="+ name +", seasoncount="+ Seasoncount +", originrelease="+ Originrelease +"]"; }    }

Can be used on the method:

We found that if the incoming parameter does not conform to the verification requirement, it will be returned, and through Bindingresult, regardless of whether the incoming parameter meets the requirements,

Can be passed through the bindingresult in the procedure, to be judged by the program. As follows:

The client agrees to use postman to send the request ~ ~

Check-restcontroller advanced for client incoming data

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.