控制器:
package com.awaimai.web; import org.hibernate.validator.constraints.*; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.Size; import java.util.Enumeration; @RestController @Validated public class kzq { @RequestMapping(value ="/test2") public String test2( @Valid Student student, BindingResult bindingResult) { String a = student.getFirstName(); String b = student.getLastName(); return a+b; } }
实体类:
package com.awaimai.web; import org.springframework.validation.annotation.Validated; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; public class Student { @Size(min = 2,max = 5) private String firstName; @NotNull @Size(min = 2,max = 3) private String lastName; public void setFirstName(String firstName) { this.firstName = firstName; } public String getFirstName() { return firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getLastName() { return lastName; } }
postman执行: