Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation 用于列表验证的自定义约束验证器_Validation_Spring Boot - Fatal编程技术网

Validation 用于列表验证的自定义约束验证器

Validation 用于列表验证的自定义约束验证器,validation,spring-boot,Validation,Spring Boot,使用Spring Boot 2.0.3.RELEASE、javax.validation 2.0.1.Final 我需要验证进入控制器的请求: @RestController @CrossOrigin @RequestMapping("/quotas/") @AllArgsConstructor @Slf4j public class QuotasController { private QuotasService quotasService; @ApiOperation("

使用Spring Boot 2.0.3.RELEASE、javax.validation 2.0.1.Final

我需要验证进入控制器的请求:

@RestController
@CrossOrigin
@RequestMapping("/quotas/")
@AllArgsConstructor
@Slf4j
public class QuotasController {

    private QuotasService quotasService;

    @ApiOperation("Post quotas")
    @PostMapping(value = "/quotas", produces = MediaType.APPLICATION_JSON_VALUE)
    public void quotas(@Valid @RequestBody List<Quota> quotaList){
        quotasService.insertQuotas(quotaList);
    }
}
@RestController
@交叉起源
@请求映射(“/quota/”)
@AllArgsConstructor
@Slf4j
公共类QuotasController{
私人QuotaService QuotaService;
@API操作(“职位配额”)
@PostMapping(value=“/quotas”,products=MediaType.APPLICATION\u JSON\u value)
公共无效配额(@Valid@RequestBody List quotaList){
QuotaService.insertQuotas(quotaList);
}
}
它调用一个服务:

@AllArgsConstructor
@Service
@Slf4j
public class QuotasService {
    private QuotasRepository quotasRepository;

    public void insertQuotas(List<Quota> quotaList) {
        quotasRepository.saveAll(quotaList);
    }
}
@allargsconstuctor
@服务
@Slf4j
公共类QuotaService{
私有QuotasRepository QuotasRepository;
公共void insertQuotas(列表quotaList){
quotasRepository.saveAll(quotaList);
}
}
它将对象保存到MongoRepository方法
列表saveAll(Iterable var1)

我还有一个自定义注释:

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = 
SumOfValuesEqualsToOneHundredValidator.class)
public @interface SumOfValuesEqualsToOneHundred {
    String message() default "Sum of fields doesn't equal to 100";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}
@Target(ElementType.FIELD)
@保留(RetentionPolicy.RUNTIME)
@约束(validatedBy=
SumofValueSequalTooneHundredValidator.class)
public@interface SumOfValuesEqualsToOneHundred{
String message()默认值“字段之和不等于100”;
类[]组()默认值{};

类使用@Valid-inside-list和@Validated-in-controller类:

@RestController
@CrossOrigin
@RequestMapping("/quotas/")
@AllArgsConstructor
@Slf4j
@Validated
public class QuotasController {

private QuotasService quotasService;

@ApiOperation("Post quotas")
@PostMapping(value = "/quotas", produces = MediaType.APPLICATION_JSON_VALUE)
public void quotas(@Valid @RequestBody List<Quota> quotaList){
    quotasService.insertQuotas(quotaList);
}
@RestController
@交叉起源
@请求映射(“/quota/”)
@AllArgsConstructor
@Slf4j
@验证
公共类QuotasController{
私人QuotaService QuotaService;
@API操作(“职位配额”)
@PostMapping(value=“/quotas”,products=MediaType.APPLICATION\u JSON\u value)
公共无效配额(@Valid@RequestBody List quotaList){
QuotaService.insertQuotas(quotaList);
}
}

@数据
@诺尔格构装师
@AllArgsConstructor
公营班级配额{
@身份证
私有整数存储ID;
@JsonProperty(“配额”)
私有列表<@Valid QuotaDelivery>quotaDeliveryList;
}
列表<@Valid QuotaPayment>quotaPaymentList;

use@Valid before quotapeymentno effect不幸的是,我认为您在使用列表的地方缺少@Valid让我做一个双重检查use@validatedbefore controller
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Quota {
    @Id
    private Integer storeId;
    @JsonProperty("quotas")
    private List<QuotaDelivery> quotaDeliveryList;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class QuotaDelivery {
    QuotaDeliveryTypeEnum type;
    @JsonProperty("quotasForPaymentTypes")
    @SumOfValuesEqualsToOneHundred
    List<QuotaPayment> quotaPaymentList;
}
@RestController
@CrossOrigin
@RequestMapping("/quotas/")
@AllArgsConstructor
@Slf4j
@Validated
public class QuotasController {

private QuotasService quotasService;

@ApiOperation("Post quotas")
@PostMapping(value = "/quotas", produces = MediaType.APPLICATION_JSON_VALUE)
public void quotas(@Valid @RequestBody List<Quota> quotaList){
    quotasService.insertQuotas(quotaList);
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Quota {
    @Id
    private Integer storeId;
    @JsonProperty("quotas")
    private List< @Valid QuotaDelivery> quotaDeliveryList;
}

List< @Valid QuotaPayment> quotaPaymentList;