Springboot---->springboot calibrator (i)

Source: Internet
Author: User

Here we simply learn about the use of data formatting in Springboot. winter flowers fail, spring blossoms, some people leave, some return.

Validators in the Springboot

Our test environment is springboot, and the requested person data is first converted and re-verified.

We define the entity Bean class person
 PackageCom.linux.huhx.learn.converter;Importjavax.validation.constraints.Min;ImportJavax.validation.constraints.NotNull;Importjavax.validation.constraints.Size;Importjava.io.Serializable;/*** @Author: HUHX * @Date: 2017-12-15 PM * @Desc: Entity class*/ Public classPersonImplementsSerializable {@NotNull (message= "User name cannot be empty") @Size (min= 3, message = "The length of the user name is less than 3 bits")    PrivateString username; PrivateString password; @Min (Value=, message = "The age of the user cannot be less than 18")    Private intAge ;  PublicPerson (string Username, string password,intAge ) {         This. Username =username;  This. Password =password;  This. Age =Age ; }  // .. Get Set}

Second, our test controller class
 PackageCom.linux.huhx.learn.converter;Importcom.linux.huhx.exception.MaxRunTimeException;ImportOrg.springframework.validation.BindingResult;ImportOrg.springframework.validation.ObjectError;Importorg.springframework.web.bind.annotation.PostMapping;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;ImportJavax.validation.Valid;/*** @Author: HUHX * @Date: 2017-12-15 pm 3:46 * @Desc: Test springboot in custom type converters*/@RestController @requestmapping ("/converter") Public classpersonconverteraction {@PostMapping ("/person")     PublicPerson Convertrstringtoperson (@Valid person, Bindingresult Bindingresult) {if(Bindingresult.haserrors ()) { for(Objecterror error:bindingResult.getAllErrors ()) {Throw Newmaxruntimeexception (Error.getdefaultmessage ()); }        }        returnPerson ; }}

Request to send post via postman: Http://localhost:9998/converter/person.

The returned data:

{    "timestamp":1513336242870,    "Status": -,    "Error":"Internal Server Error",    "Exception":"com.linux.huhx.exception.MaxRunTimeException",    "message":"the user's age cannot be less than",    "Path":"/converter/person"}

Friendship Link

Springboot---->springboot calibrator (i)

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.