4

I have entity:

public class User{
   @NotNull
   private Integer age;
} 

In Restcontroller:

@RestController
public UserController {
 ..... 
} 

I have BindingResult, but field age Spring doesn't validate. Can you tell me why?

Thanks for your answers.

2
  • Please post your controller method.. Commented Jan 19, 2017 at 18:43
  • @RequestMapping("/users") public void addUser(@Valid @RequestBody User user, BindingResult result) { if(result.hasErrors()){ some action } } Commented Jan 19, 2017 at 19:00

2 Answers 2

10

If your posting something like JSON data representing the User class you can use the annotation @Valid in conjunction with @RequestBody to trigger validation of annotations such as the @NotNull you have on your age property. Then with BindingResult you can check if the entity/data has errors and handle accordingly.

@RestController
public UserController {

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<?> create(@Valid @RequestBody User user, BindingResult bindingResult) {
        if(bindingResult.hasErrors()) {
            // handle errors
        }
        else {
            // entity/date is valid
        }
    }
}

I'd make sure your User class also has the @Entity annotation as well.

@Entity
public class User {
    @NotNull
    @Min(18)
    private Integer age;

    public Integer getAge() { return age; }

    public setAge(Integer age) { this.age = age; }
}

You may want to set properties to output/log SQL so that you can see that the proper restraints are being added to the User table.

Hopefully that helps!

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answers, I do it, like you, but @NotNull on Integer attribute age not validation, but every for example String attribute works fine.
Try adding the validation annotation @Min(1) or @Range(min=1, max=10000) to the Integer age property and see if it successfully validates.
Yes, Thanks, I try it. But It is strange. On this tutorial: spring.io/guides/gs/validating-form-input use @NotNull annotation on Integer attribute
Great to hear! I think @RestController needs the @Min or @Range because your data is coming in via JSON as a String for the age property. Try getting the JSON to send as a number instead if you can and @NotNull might be all you need. In the example the form the age field has a bean-backed property so it must be converting to a number on submit. The @Min or @Range implicitly converts to a number type when doing the check. Adding a @Range to something like age would make sense though as you wouldn't want user to enter age 999. Please mark answer that help you solve the issue. Thanks!
2

If needed you can specifiy default messages

@NotNull("message": "age: positive number value is required")

@Min(value=18, message="age: positive number, min 18 is required")

and please use dto's

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.