使用Hibernate Validator进行Spring Boot验证

时间:2016-02-22 00:50:40

标签: validation spring-boot hibernate-validator

任何人都知道我是否有可能在我的实体中创建一个方法,当我在我的班级中添加注释@Valid时执行?

示例:

我有这个对象:

public class Area {
    @NotEmpty
    private String unidade;
    @NotNull
    private double tamanho;

    public String getUnidade() {
        return unidade;
    }

    public void setUnidade(String unidade) {
        this.unidade = unidade;
    }

    public double getTamanho() {
        return tamanho;
    }

    public void setTamanho(double tamanho) {
        this.tamanho = tamanho;
    }
}

我有这个方法:

@RestController
@RequestMapping("/recolhimento")
public class RecolhimentoController {

    @RequestMapping(method = RequestMethod.GET)
    public boolean getRecolhimento(@Valid Area area){
        ...
    }
}

所以当我调用这个方法时,Spring Boot将验证我的模型区域(但我想创建一个在使用@Valid时将执行的方法。

有可能吗?如何?

1 个答案:

答案 0 :(得分:-1)

是的,这是可能的。

您可以在此项目中找到示例:https://github.com/malkusch/validation