spring @valid doesn't work
Asked Answered
D

4

5

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;
  }
}
Disruptive answered 16/4, 2017 at 11:36 Comment(3)
have you tried with BindingResult .this may be a good example of usage of this approach.Wideopen
try to use Validated instead of ValidApoenzyme
It still doesn't workDisruptive
E
3

You are probably missing the dependencies.

<dependency>
   <groupId>javax.validation</groupId>
   <artifactId>validation-api</artifactId>
</dependency>
<dependency>
   <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
</dependency>

Also, make sure you import NotNull correctly.

import javax.validation.constraints.NotNull;
Evzone answered 15/9, 2020 at 19:52 Comment(0)
G
2

In my case adding of @Validated on top of RestController from "org.springframework.validation.annotation" solved the problem

Garin answered 26/1, 2022 at 12:8 Comment(1)
Exactly what I was missing too.Maximilianus
K
1

Am, I am still new in Spring. But here is an answer for my situation if it suits you.

To make @Valid work you should include two packages javax validation-api and hibernate-validator, but I only included validation-api.

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.0.Final</version>
</dependency>
    
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.1.Final</version>
</dependency>
Kean answered 16/8, 2017 at 13:13 Comment(0)
R
1

It doesn't work with hibernate validator version 7.x.x. Works fine with 6.2.1.Final

Repartee answered 14/2, 2022 at 12:51 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Roentgenograph

© 2022 - 2024 — McMap. All rights reserved.