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)