基于 DTO 对象验证验证对象
Validate object based on DTO object validation
我想在处理之前根据我的 DTO CustomerFailedPaymentDto 对象从 csv 文件验证我的对象。我使用 @Valid 但仍然无法正常工作。
Mycontroller.java
@PostMapping("/proccessFile")
public String proccessFile(@RequestParam("file") MultipartFile file) {
// parse CSV file to create a list of `User` objects
try (Reader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {
// create csv bean reader
CsvToBean<CustomerFailedPaymentDto> csvToBean = new CsvToBeanBuilder(reader)
.withType(CustomerFailedPaymentDto.class)
.withIgnoreLeadingWhiteSpace(true)
.build();
// I WANT TO VALIDATE THIS OBJECT
List <@Valid CustomerFailedPaymentDto> customerFailedPayment = csvToBean.parse();
return virtualAccountService.buildFailedQuery(customerFailedPayment);
} catch (Exception ex) {
return "An error occurred while processing the CSV file.";
}
}
CustomerFailedPaymentDto.java
@Data
public class CustomerFailedPaymentDto {
private Long id;
@NotEmpty(message = "Please provide a bankPartner")
private String bankPartner;
@NotEmpty(message = "Please provide a bankCoreCode")
private String bankCoreCode;
@NotEmpty(message = "Please provide a transactionDate")
private String transactionDate;
}
如何根据我的 DTO class 验证确保我从 csv 文件中获取的对象有效?
需要触发验证,在您的情况下 spring 不会执行。
使用javax.validation.Validator
首先自动装配验证器
@Autowired
private final Validator validator;
然后使用验证器对每个列表项进行验证。
Set<ConstraintViolation<CustomerFailedPaymentDto>> violations = validator.validate(perCustomerFailedPayment);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(new HashSet<ConstraintViolation<?>>(violations));
}
我想在处理之前根据我的 DTO CustomerFailedPaymentDto 对象从 csv 文件验证我的对象。我使用 @Valid 但仍然无法正常工作。
Mycontroller.java
@PostMapping("/proccessFile")
public String proccessFile(@RequestParam("file") MultipartFile file) {
// parse CSV file to create a list of `User` objects
try (Reader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {
// create csv bean reader
CsvToBean<CustomerFailedPaymentDto> csvToBean = new CsvToBeanBuilder(reader)
.withType(CustomerFailedPaymentDto.class)
.withIgnoreLeadingWhiteSpace(true)
.build();
// I WANT TO VALIDATE THIS OBJECT
List <@Valid CustomerFailedPaymentDto> customerFailedPayment = csvToBean.parse();
return virtualAccountService.buildFailedQuery(customerFailedPayment);
} catch (Exception ex) {
return "An error occurred while processing the CSV file.";
}
}
CustomerFailedPaymentDto.java
@Data
public class CustomerFailedPaymentDto {
private Long id;
@NotEmpty(message = "Please provide a bankPartner")
private String bankPartner;
@NotEmpty(message = "Please provide a bankCoreCode")
private String bankCoreCode;
@NotEmpty(message = "Please provide a transactionDate")
private String transactionDate;
}
如何根据我的 DTO class 验证确保我从 csv 文件中获取的对象有效?
需要触发验证,在您的情况下 spring 不会执行。
使用javax.validation.Validator
首先自动装配验证器
@Autowired
private final Validator validator;
然后使用验证器对每个列表项进行验证。
Set<ConstraintViolation<CustomerFailedPaymentDto>> violations = validator.validate(perCustomerFailedPayment);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(new HashSet<ConstraintViolation<?>>(violations));
}