Java-在特殊情况下关闭类字段的Spring验证
在我当前的实现中,Java-在特殊情况下关闭类字段的Spring验证,java,spring,spring-boot,validation,Java,Spring,Spring Boot,Validation,在我当前的实现中,SpecialTransactionDTO(transactionMetric和transactionRank)两个字段的验证在所有情况下都有效。现在,我作为@RequestBody接收的父类TransactionDTO包含一个布尔字段shouldValidate,该字段指示是否验证SpecialTransactionDTO的两个字段 当shouldValidate标志的值为false时,我应该如何配置(关闭)验证 @PostMapping("{id}/transaction/
SpecialTransactionDTO
(transactionMetric
和transactionRank
)两个字段的验证在所有情况下都有效。现在,我作为@RequestBody接收的父类TransactionDTO包含一个布尔字段shouldValidate
,该字段指示是否验证SpecialTransactionDTO
的两个字段
当shouldValidate
标志的值为false时,我应该如何配置(关闭)验证
@PostMapping("{id}/transaction/")
@ApiOperation(value = "Create transaction", httpMethod = "POST", response = TransactionDTO.class)
public ResponseEntity<Object> createTransaction(@PathVariable("id") Long accountId,
@Validated({TransactionNumber.class})
@RequestBody TransactionDTO transaction)
throws NotFoundException, URISyntaxException {
TransactionDTO result = transactionService.createTransaction(accountId, transaction);
return ResponseEntity.created(new URI("api/userAccount/" + accountId)).body(result);
}
您可以完全消除
@Validated
注释,并模仿Spring的行为,如下所示:
@PostMapping("{id}/transaction/")
@ApiOperation(value = "Create transaction", httpMethod = "POST", response = TransactionDTO.class)
public ResponseEntity<Object> createTransaction(@PathVariable("id") Long accountId,
@RequestBody TransactionDTO transaction)
throws NotFoundException, URISyntaxException, MethodArgumentNotValidException {
// Check if we should validate here. Spring will convert your MethodArgumentNotValidException into HTTP 400
if(transaction.shouldValidate) {
SpringValidatorAdapter adapter = new SpringValidatorAdapter(validator);
BeanPropertyBindingResult result = new BeanPropertyBindingResult(transaction, transaction.getClass().getName());
adapter.validate(transaction, result, TransactionNumber.class);
if (result.hasErrors()) {
throw new MethodArgumentNotValidException(null, result);
}
}
TransactionDTO result = transactionService.createTransaction(accountId, transaction);
return ResponseEntity.created(new URI("api/userAccount/" + accountId)).body(result);
}
@PostMapping(“{id}/transaction/”)
@ApiOperation(value=“Create transaction”,httpMethod=“POST”,response=TransactionDTO.class)
public ResponseEntity createTransaction(@PathVariable(“id”)Long accountId,
@请求主体事务(到事务)
抛出NotFoundException、URISyntaxException、MethodArgumentNotValidException{
//检查我们是否应该在这里验证。Spring将把您的MethodArgumentNotValidException转换为HTTP 400
if(事务处理.shouldValidate){
SpringValidatorAdapter=新的SpringValidatorAdapter(验证器);
BeanPropertyBindingResult=新的BeanPropertyBindingResult(事务,transaction.getClass().getName());
验证(事务、结果、事务编号.class);
if(result.hasErrors()){
抛出新的MethodArgumentNotValidException(null,result);
}
}
TransactionDTO结果=transactionService.createTransaction(accountId,transaction);
返回ResponseEntity.created(新URI(“api/userAccount/”+accountId)).body(结果);
}
@PostMapping("{id}/transaction/")
@ApiOperation(value = "Create transaction", httpMethod = "POST", response = TransactionDTO.class)
public ResponseEntity<Object> createTransaction(@PathVariable("id") Long accountId,
@RequestBody TransactionDTO transaction)
throws NotFoundException, URISyntaxException, MethodArgumentNotValidException {
// Check if we should validate here. Spring will convert your MethodArgumentNotValidException into HTTP 400
if(transaction.shouldValidate) {
SpringValidatorAdapter adapter = new SpringValidatorAdapter(validator);
BeanPropertyBindingResult result = new BeanPropertyBindingResult(transaction, transaction.getClass().getName());
adapter.validate(transaction, result, TransactionNumber.class);
if (result.hasErrors()) {
throw new MethodArgumentNotValidException(null, result);
}
}
TransactionDTO result = transactionService.createTransaction(accountId, transaction);
return ResponseEntity.created(new URI("api/userAccount/" + accountId)).body(result);
}