简体   繁体   English

如何验证 pojo 集合

[英]How to validate a pojo collection

I have a collection of pojo objects我有一组 pojo 对象

public class Assignment {
    Date entrydate;
    Date exitdate;
    String team, 
    double allocation;
    String username,
    //getters and setters
}

The example values are Object 1 :示例值为 Object 1 :

entrydate: "2000-01-01", exitdate: "2019-03-31", team: "team1", allocation: 0.5 username: "user1"进入日期:“2000-01-01”,退出日期:“2019-03-31”,团队:“team1”,分配:0.5 用户名:“user1”

Object 2:对象 2:

entrydate: "2000-01-01", exitdate: "2019-03-31", team: "team2", allocation: 0.5 username: "user1"进入日期:“2000-01-01”,退出日期:“2019-03-31”,团队:“team2”,分配:0.5 用户名:“user1”

Object 3:对象 3:

entrydate: "2019-04-01", exitdate: "2019-07-31", team: "team3", allocation: 0.6 username: "user1"进入日期:“2019-04-01”,退出日期:“2019-07-31”,团队:“team3”,分配:0.6 用户名:“user1”

Object 4:对象 4:

entrydate: "2019-05-01", exitdate: "2019-07-31", team: "team4", allocation: 0.6 username: "user1"进入日期:“2019-05-01”,退出日期:“2019-07-31”,团队:“team4”,分配:0.6 用户名:“user1”

My criterion is for a specified user and for a specific date period the count of total allocation should be 1 => logical explanation will be the engineer should not work more than 100% or less than 100% for a specified period.我的标准是针对指定用户和特定日期期间的总分配计数应为 1 => 逻辑解释是工程师在指定期间的工作量不应超过 100% 或低于 100%。

We need a function in which we pass a collection of the above pojo objects and if it does not meet this criterion it should throw an error with proper explanation - like for this date range already allocation is over 1 for specified teams.我们需要一个函数来传递上述 pojo 对象的集合,如果它不符合这个标准,它应该抛出一个错误并给出正确的解释——比如对于这个日期范围,指定团队的分配已经超过 1。 Also this method should check for date gaps if any present -- logical explanation for this feature will be - the engineer should not be idle for a specified period此外,此方法应检查日期间隔(如果有) - 此功能的逻辑解释是 - 工程师不应在指定时间内闲置

Method signature will be方法签名将是

List<String> getValidation(List<Assignment > assignments);

(suppose we know that all the assignments belong to a specific user --> no need to worry aboubt username for now) (假设我们知道所有分配都属于特定用户 --> 现在无需担心用户名)

How to properly implement this如何正确实施

There are several ways to tackle it.有几种方法可以解决它。 There are existing annotations already in the javax-validation-api like NotNull , Min , Max , ... javax-validation-api已经存在注释,例如NotNullMinMax ……

You have simply use these annotations on your pojo variables for your case as specified below.您只需在您的 pojo 变量上为您的案例使用这些注释,如下所示。

public class Assignment {
    Date entrydate;
    Date exitdate;
    String team, 
    @Min(0)
    @Max(1)
    double allocation;
    String username,
    //getters and setters
}

You need to instruct your controller that you will need validations on request body through @Valid annotation as below.您需要通过@Valid注释指示您的控制器需要对请求正文进行验证,如下所示。

@PostMapping("someEndPoint")
public ResponseEntity getSomeThing(@Valid @RequestBody requestBody)
{
}

In the cases where you have to validate @PathVariable you have to use @Validated on the controller class itself like在必须验证@PathVariable的情况下,您必须在控制器类本身上使用@Validated ,例如

@Api("AssignmentController")
@RestController
@Validated
@RequestMapping(value = "/api/admin")
public class AssignmentController {
}

In the case where you need custom validations on the fields which are not provided by javax validations , you can implement your custom validator and custom annotation for that purpose.如果您需要对javax validations未提供的字段进行自定义验证,您可以为此目的实现自定义验证器和自定义注释。

Let's look an example where you want to create an annotation @ValidAllocation which will validate your allocation field.让我们看一个示例,您要创建一个注释@ValidAllocation来验证您的allocation字段。

Creating custom annotation:创建自定义注释:

@Documented
@Retention(RUNTIME)
@Target({FIELD, ANNOTATION_TYPE, PARAMETER})
@Constraint(validatedBy = AllocationValidator.class)
public @interface ValidAssignment {
    String message() default "Assignment should be between 0 and 1";
    Class<?>[] groups() default { };
    Class<? extends Payload>[] payload() default { };
}

Creating custom validator which is used by the annotation:创建注释使用的自定义验证器:

@Component
public class AllocationValidator implements ConstraintValidator<ValidAssignment, Double> {

    @Override
    public boolean isValid(Double value, ConstraintValidatorContext constraintValidatorContext) {
        return (value >=0 && value <=1);
    }

    @Override
    public void initialize(ValidAssignment validAssignment)  {}
}

You Pojo will look like as specified below您的 Pojo 将如下所示

public class Assignment {
    Date entrydate;
    Date exitdate;
    String team, 
    @ValidAllocation
    double allocation;
    String username,
    //getters and setters
}

Let me know if you have any questions :)如果您有任何问题,请告诉我 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM