Jerry's Blog

Recording what I learned everyday

View on GitHub


5 June 2019

Spring validation

by Jerry Zhang

Day 1: Spring boot Web Evolved (Plan to make a flutter tutorial website)



Passing parameters with an Object

When we have more and more parameters in the controller methods, we might want to pass them in an easier way. We can pass in an object instead of passing the parameters separately.

@PostMapping(value = "/girls")
public Result<Girl> girlAdd(@Valid Girl girl, BindingResult bindingResult) {
    ...
}

Validation

If we need to validate a parameter value, we can do the following steps.

@Entity
public class Girl {
    @NotBlank(message = "cupSize cannot be blank")
    private String cupSize;

    @Min(value = 18, message = "At least 18")
//    @NotNull
//    @Max()
//    @Length()
    private Integer age;
}
@PostMapping(value = "/girls")
public Result<Girl> girlAdd(@Valid Girl girl, BindingResult bindingResult) {

}
tags: Spring, - validation