在春季批处理中读取csv文件时验证字段长度

时间:2019-10-05 14:02:46

标签: spring-boot spring-batch

在spring-boot批处理FlatFileItemReader中读取csv文件时,有什么方法可以验证字段长度? 我尝试了javax.validation并在pojo中设置了最小和最大长度,但不知道如何触发验证。

2 个答案:

答案 0 :(得分:2)

我在这里用READ.me创建了一个示例。

我重写FlatItemReader,然后使用Hibernate Validator在下面的Reader级别执行Bean验证

public class SimpleReader extends FlatFileItemReader<SoccerTeam> {
    private Validator factory = Validation.buildDefaultValidatorFactory().getValidator();

    @Override
    public SoccerTeam doRead() throws Exception {
        SoccerTeam soccerTeam = super.doRead();

        if (Objects.isNull(soccerTeam)) return null;

        Set<ConstraintViolation<SoccerTeam>> violations = this.factory.validate(soccerTeam);
        if (!violations.isEmpty()) {
            System.out.println(violations);
            String errorMsg = String.format("The input has validation failed. Data is '%s'", soccerTeam);
            throw new FlatFileParseException(errorMsg, Objects.toString(soccerTeam));
        }
        else {
            return soccerTeam;
        }
    }
}

该模型正在使用验证注释

package com.bigzidane.example.springbatch;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;

public class SoccerTeam {

    @Size(max = 60)
    private String name;
    private String national;

    @Min(value = 1)
    @Max(value = 100)
    private int rank;

    public void setName(String name) {
        this.name = name;
    }

    public void setNational(String national) {
        this.national = national;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    @Override
    public String toString() {
        return "SoccerTeam{" +
                "name='" + name + '\'' +
                ", national='" + national + '\'' +
                ", rank='" + rank + '\'' +
                '}';
    }
}

有关完整代码,请查看here

示例在本地运行 输入为

name,national,rank
real madrid,spain,100
ars,english,1

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.9.RELEASE)

2019-10-05 14:41:27.754  INFO 10912 --- [           main] c.b.example.springbatch.HelloBatch       : Starting HelloBatch on WINDOWS-ESDA5FC with PID 10912 (C:\Users\dotha\IdeaProjects\spring-boot-batch\target\classes started by dotha in C:\Users\dotha\IdeaProjects\spring-boot-batch)
2019-10-05 14:41:27.760  INFO 10912 --- [           main] c.b.example.springbatch.HelloBatch       : No active profile set, falling back to default profiles: default
2019-10-05 14:41:28.985  INFO 10912 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-10-05 14:41:29.232  INFO 10912 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-10-05 14:41:29.383  INFO 10912 --- [           main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: H2
2019-10-05 14:41:29.684  INFO 10912 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
2019-10-05 14:41:29.934  INFO 10912 --- [           main] c.b.example.springbatch.HelloBatch       : Started HelloBatch in 2.643 seconds (JVM running for 3.269)
2019-10-05 14:41:29.936  INFO 10912 --- [           main] o.s.b.a.b.JobLauncherCommandLineRunner   : Running default command line with: []
2019-10-05 14:41:30.035  INFO 10912 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=processJob]] launched with the following parameters: [{run.id=1}]
2019-10-05 14:41:30.066  INFO 10912 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
SoccerTeam{name='real madrid', national='spain', rank='100'}
SoccerTeam{name='ars', national='english', rank='1'}
2019-10-05 14:41:30.220  INFO 10912 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=processJob]] completed with the following parameters: [{run.id=1}] and the following status: [COMPLETED]
2019-10-05 14:41:30.225  INFO 10912 --- [       Thread-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2019-10-05 14:41:30.227  INFO 10912 --- [       Thread-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

现在,如果输入的验证失败 输入为

name,national,rank
real madrid,spain,101
ars,english,1
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.9.RELEASE)

2019-10-05 14:42:37.757  INFO 11268 --- [           main] c.b.example.springbatch.HelloBatch       : Starting HelloBatch on WINDOWS-ESDA5FC with PID 11268 (C:\Users\dotha\IdeaProjects\spring-boot-batch\target\classes started by dotha in C:\Users\dotha\IdeaProjects\spring-boot-batch)
2019-10-05 14:42:37.761  INFO 11268 --- [           main] c.b.example.springbatch.HelloBatch       : No active profile set, falling back to default profiles: default
2019-10-05 14:42:39.319  INFO 11268 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-10-05 14:42:39.648  INFO 11268 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-10-05 14:42:39.798  INFO 11268 --- [           main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: H2
2019-10-05 14:42:40.101  INFO 11268 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
2019-10-05 14:42:40.372  INFO 11268 --- [           main] c.b.example.springbatch.HelloBatch       : Started HelloBatch in 3.231 seconds (JVM running for 3.89)
2019-10-05 14:42:40.374  INFO 11268 --- [           main] o.s.b.a.b.JobLauncherCommandLineRunner   : Running default command line with: []
2019-10-05 14:42:40.524  INFO 11268 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=processJob]] launched with the following parameters: [{run.id=1}]
2019-10-05 14:42:40.559  INFO 11268 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
[ConstraintViolationImpl{interpolatedMessage='must be less than or equal to 100', propertyPath=rank, rootBeanClass=class com.bigzidane.example.springbatch.SoccerTeam, messageTemplate='{javax.validation.constraints.Max.message}'}]
2019-10-05 14:42:40.746 ERROR 11268 --- [           main] o.s.batch.core.step.AbstractStep         : Encountered an error executing step step1 in job processJob

org.springframework.batch.item.file.FlatFileParseException: The input has validation failed. Data is 'SoccerTeam{name='real madrid', national='spain', rank='101'}'
    at com.bigzidane.example.springbatch.SimpleReader.doRead(SimpleReader.java:25) ~[classes/:na]
    at com.bigzidane.example.springbatch.SimpleReader.doRead(SimpleReader.java:12) ~[classes/:na]
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:92) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:94) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.step.item.SimpleChunkProvider.read(SimpleChunkProvider.java:161) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:119) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:113) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:69) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:407) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:331) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140) ~[spring-tx-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:273) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:82) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:258) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:203) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:68) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:136) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:313) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:144) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) [spring-core-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:137) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) [spring-aop-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) [spring-aop-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) [spring-aop-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) [spring-aop-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at com.sun.proxy.$Proxy44.run(Unknown Source) [na:na]
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:207) [spring-boot-autoconfigure-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:181) [spring-boot-autoconfigure-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:168) [spring-boot-autoconfigure-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:163) [spring-boot-autoconfigure-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:781) [spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:765) [spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:319) [spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) [spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at com.bigzidane.example.springbatch.HelloBatch.main(HelloBatch.java:9) [classes/:na]

2019-10-05 14:42:40.759  INFO 11268 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=processJob]] completed with the following parameters: [{run.id=1}] and the following status: [FAILED]
2019-10-05 14:42:40.764  INFO 11268 --- [       Thread-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2019-10-05 14:42:40.768  INFO 11268 --- [       Thread-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Process finished with exit code 0

答案 1 :(得分:0)

如果要在阅读器级别执行验证,可以使用ItemReadListener或通过覆盖FlatFileItemReader并在doRead中调用验证逻辑,如@Nghia Do所示。

项目validation是项目处理器的典型用例。如果您使用Spring Batch 4.1+,则可以在面向块的步骤中使用BeanValidatingItemProcessor。您可以在此处找到示例:https://docs.spring.io/spring-batch/4.1.x/reference/html/whatsnew.html#whatsNewBeanValidationApi