Perform data validation in the background with spring @validated annotations and some annotations of the org.hibernate.validator.constraints.*

Source: Internet
Author: User

This demo is mainly to let Spring's @validated annotations and hibernate support the JSR data validation of some of the annotations together to complete the data validation. This demo is used by Springboot.

First the code for the domain object Foo is as follows:

Package Com.entity;import Org.hibernate.validator.constraints.email;import Org.hibernate.validator.constraints.notblank;import javax.validation.constraints.Min; Public classFoo {@NotBlank (message="This name cannot be empty")    PrivateString name; @Min (Value= -, message ="This age is the smallest")    PrivateInteger age; @Email (Message="Bad mailbox Format")    PrivateString Email;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicInteger getage () {returnAge ; }     Public voidsetage (Integer age) { This. Age =Age ; }     PublicString Getemail () {returnemail; }     Public voidsetemail (String email) { This. email =email; }}

Next the controller layer code is as follows:

Package Com.validation;import Com.entity.bar;import com.entity.barvalidator;import com.entity.foo;import Org.springframework.validation.bindingresult;import Org.springframework.validation.objecterror;import Org.springframework.validation.annotation.validated;import Org.springframework.web.bind.webdatabinder;import Org.springframework.web.bind.annotation.initbinder;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.restcontroller;import Javax.validation.valid;import java.util.List;@ Restcontroller Public classFoocontroller {@RequestMapping ("/footest")     PublicString foo (@Valid foo foo, bindingresult result) {if(Result.haserrors ()) {List<ObjectError> allerrors =result.getallerrors ();  for(Objecterror objecterror:allerrors) {String defaultmessage=Objecterror.getdefaultmessage (); System. out. println ("defalutmessage="+defaultmessage); } System. out. println ("allerrors="+Allerrors.toarray ()); return  "failed"; }        return  "Success"; }  }

The code to start spring boot is as follows:

Package Com.validation;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, @SpringBootApplication (exclude={ Datasourceautoconfiguration. class })publicclass  application {    publicstaticvoid  Main (string[] args) {        springapplication.run (application. class , args);}    }

Perform data validation in the background with some annotations of spring @validated annotations and org.hibernate.validator.constraints.*

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.