Use of @valid annotations for spring frames (validation of nested types)

Source: Internet
Author: User

@Valid Annotations Enable validation of data, you can define entities, add a validation rule on the attributes of an entity, and add the @valid keyword when the API receives data, and your entity will turn on a checksum function.

@Valid the use of annotation types:

@Null
Limit is null only

@NotNull
Limit must be non-null

@AssertFalse
The limit must be false

@AssertTrue
The limit must be true

@DecimalMax (value)
Limit must be a number that is less than the specified value

@DecimalMin (value)
The limit must be a number that is not less than the specified value

@Digits (integer,fraction)
The limit must be a decimal, and the number of digits in the integer part cannot exceed integer, and the number of fractional digits cannot exceed fraction

@Future
The limit must be a future date

@Max (value)
Limit must be a number that is less than the specified value

@Min (value)
The limit must be a number that is not less than the specified value

@Past
The limit must be a past date

@Pattern (value)
The restriction must conform to the specified regular expression

@Size (Max,min)
Limit character length must be between min to Max

@Past
Verify that the annotation's element value (date type) is earlier than the current time

@NotEmpty
Verifies that the element value of the annotation is not null and is not empty (string length is not 0, collection size is not 0)

@NotBlank
Verifies that the element value of the annotation is not null (NOT NULL, the first space is removed after the length is 0), differs from @notempty, @NotBlank is applied only to strings and the whitespace is stripped of the string when compared

@Email
Verify that the element value of the annotation is email, or you can specify a custom email format via regular expressions and flag

Note that the exception type is not used incorrectly, for example, not available on int @size

I. Common entity field validation and nested entity field validation differences

NOTE: Entity class validation is only validated against the properties of the first-level object within that entity, and if there is a member variable for the entity class that is being validated as another entity class, you need to add @valid annotation on that member variable (the entity class that requires validation)

Second, RESTful request interface (Action/controller layer/web Business layer), entity class data binding validation comparison

@1.

Import Javax.validation.Valid;
Import Org.springframework.beans.factory.annotation.Value;
Import Org.springframework.validation.BindingResult;

//Request parameters are simple data combination objects, single-layer entity field validation

@ApiOperation (value = "Get card content from business platform", notes = "card content acquisition")
@RequestMapping (value = "/cardinfodetailtest", method = Requestmethod.post)
Public Object getcardinfodetailtemp (@Valid cardinfodto info,bindingresult bindingresult) {
OUTPUTOBJECTCSF obj = new OUTPUTOBJECTCSF ();
if (Bindingresult.haserrors ()) {
Obj.setrtncode (ReturnInfoEnums.PROCESS_PARAMETER_ERROR.getCode ());
Obj.setrtnmsg (Bindingresult.getallerrors (). Get (0). Getdefaultmessage ());
return obj;
}
return obj;
}



//Request parameters are complex data combination objects, nested entity field validation

    @ApiOperation (value = "Get card content from the business platform", notes = "card content acquisition")
    @RequestMapping ( Value = "/cardinfodetailtest", method = Requestmethod.post)
    public Object getcardinfodetailtemp ( @Valid cardinfodetail Info,bindingresult bindingresult) {
        OUTPUTOBJECTCSF obj = new OUTPUTOBJECTCSF ();
          if (bindingresult.haserrors ()) {
             Obj.setrtncode (ReturnInfoEnums.PROCESS_PARAMETER_ERROR.getCode ());
            obj.setrtnmsg (bindingresult.getallerrors (). Get (0). Getdefaultmessage ());
            return obj;
       }
        return obj;
   }

@2.swagger-ui.html page Interface Data validation comparison

Third, the @pathvariable calibration

Spring-boot may not currently support validation of parameters:
Public result<?> Xobyid (@NotNull @NotBlank @Size (min=10,max=32) @PathVariable (value= "accountId") String ID) {}
However, there is no way to throw an exception, it may be resolved in the next version of Spring, or it will not be @pathvariable, but in service
Class xoservice{
Public Xomethon (@NotNull String ID) {
}
}
To solve

Use of @valid annotations for spring frames (validation of nested types)

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.