1注解知识点-@Valid用法

前提:在 pom.xml 导入


    org.hibernate
    hibernate-validator
    5.4.1.Final

1.客户端传入参数

public class ParkEnterpriseModel extends BaseModel {
    @Min(value=1,message="id不能小于0")
    private long businessesid;

    @Min(value=1,message="id不能小于0")
    private long enterpriseid;

    @NotEmpty(message="名称不能为空")
    private String name;

    @Email(message = "邮件格式错误");
    private String email;
}

2.响应客户端

public WebResponse set(@Valid ParkEnterpriseModel parkEnterpriseModel, BindingResult result) {
        if (result.hasErrors()) {
            List errorList = result.getAllErrors();
            for(ObjectError error : errorList){
                System.out.println(error.getDefaultMessage());
                return WebResponse.ERROR( ResultCode.SERVER_ERROR, error.getDefaultMessage() );
            }
        }
        ParkEnterpriseModel parkModel = parkService.getByUuid( parkEnterpriseModel.getEnterpriseid() );
        if (parkModel == null) {

            int ret = parkService.insertSelective( parkEnterpriseModel  );
            if (ret == 0) {
                return WebResponse.ERROR( ResultCode.SERVER_ERROR, "数据插入数据库失败" );
            }
        }

        parkService.updateByPrimaryKeySelective( parkEnterpriseModel );
        return WebResponse.SUCCESS(true);
    }

你可能感兴趣的:(1注解知识点-@Valid用法)