11

Example:

public String getStudentResult(@RequestParam(value = "regNo", required = true) String regNo, ModelMap model){

How can I use @valid for the regNo parameter here?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sofs1
  • 3,834
  • 11
  • 51
  • 89
  • 1
    No you cannot. `@Valid` only works for objects not primitive structures like a `String` or `Integer` for example. If you want to validate you will have to do it yourself. – M. Deinum Oct 07 '14 at 09:23
  • https://jira.spring.io/browse/SPR-6380 – Bax Mar 12 '16 at 00:11

3 Answers3

15

Late answer. I encounter this problem recently and find a solution. You can do it as follows, Firstly register a bean of MethodValidationPostProcessor:

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
    return new MethodValidationPostProcessor();
}

and then add the @Validated to the type level of your controller:

@RestController
@Validated
public class FooController {
    @RequestMapping("/email")
    public Map<String, Object> validate(@Email(message="请输入合法的email地址") @RequestParam String email){
    Map<String, Object> result = new HashMap<String, Object>();
    result.put("email", email);
    return result;
    }
}

And if user requested with a invalid email address, the ConstraintViolationException will be thrown. And you can catch it with:

@ControllerAdvice
public class AmazonExceptionHandler {

@ExceptionHandler(ConstraintViolationException.class)
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String handleValidationException(ConstraintViolationException e){
    for(ConstraintViolation<?> s:e.getConstraintViolations()){
        return s.getInvalidValue()+": "+s.getMessage();
    }
    return "请求参数不合法";
}

}

You can check out my demo here

Guisong He
  • 1,886
  • 1
  • 15
  • 27
4

@Valid can be used to validate beans. I have'nt seen it used on single string parameters. Also it requires a validator to be configured.

The @Valid annotation is part of the standard JSR-303 Bean Validation API, and is not a Spring-specific construct. Spring MVC will validate a @Valid object after binding so-long as an appropriate Validator has been configured.

Reference : http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html

Sandeep
  • 712
  • 1
  • 10
  • 22
  • Thanks for answering. May I know how can I validate a single String parameter for my Spring MVC project to validate in Sever side. – sofs1 Oct 07 '14 at 06:48
  • 2) My another question is, can I use both @ModelAttribute and @RequestParam(value = "regNo", required = true) together? – sofs1 Oct 07 '14 at 07:16
2

one way to do it is to write a Wrapper Bean like the following :

 public class RegWrapperBean{

        @NotNull
        String regNo ; 

        public String getRegNo(){
             return regNo ;
        }

        public void setRegNo(String str){
             this.regNo=str;
        }

}

and your handler method will be like the following :

  @RequestMapping(value="/getStudentResult", method=RequestMethod.POST)
    public String getStudentResult(@Valid @ModelAttribute RegWrapperBean bean,
            BindingResult validationResult, Model model) {
    }

and please refer to these answers here and here .

Hope that Helps .

Community
  • 1
  • 1