i send a POST request of "application/json" type with 「postman」,set the param "phone" to empty string, normally it should print error for annotation "@NotEmpty", however, it didn't print anything and work well.
controller:
@RequestMapping(value = "verify_smscode", method = RequestMethod.POST)
@ResponseBody
public ResponseDto verifySmsCode(HttpServletRequest request,
@Valid @RequestBody VerifySmsCodeParam params, Errors errors) {
if(errors.hasErrors()) {
System.out.println("error");
}
boolean success = userService.verifySmsCode(params.getPhone(), params.getSmsCode());
Map<String, Object> result = new HashMap<>();
result.put("status", success);
return new ResponseDto(result);
}
Model:
@Data
@NoArgsConstructor
public class VerifySmsCodeParam {
@NotEmpty //import org.hibernate.validator.constraints.NotEmpty;
private String phone;
@NotEmpty
private String smsCode;
}
SpringMvcConfig:
@Configuration
@ComponentScan(basePackages="com.shit.voiceshare")
@EnableWebMvc
public class SpringMvcConfig extends WebMvcConfigurerAdapter {
@Override
public Validator getValidator() {
LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
localValidatorFactoryBean.setProviderClass(HibernateValidator.class);
return localValidatorFactoryBean;
}
}
BindingResult
.this may be a good example of usage of this approach. – Wideopen