проверить длину поля при чтении CSV-файла в весеннем пакете - PullRequest
0 голосов
/ 05 октября 2019

Есть ли способ проверить длину поля при чтении CSV-файла в пакетной загрузке SpringFileItemReader? Я попробовал javax.validation и установил минимальную и максимальную длину в pojo, но не знаю, как запустить проверку.

Ответы [ 2 ]

2 голосов
/ 05 октября 2019

Я создал здесь пример с READ.me здесь.

Я переопределяю FlatItemReader, а затем выполняю Валидацию Бина с использованием Hibernate Validator на уровне Reader, как показано ниже

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 + '\'' +
                '}';
    }
}

Полный код см. Здесь здесь

Пример запуска локально Вводis

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
0 голосов
/ 10 октября 2019

Если вы хотите выполнить проверку на уровне считывателя, вы можете использовать ItemReadListener или переопределить FlatFileItemReader и вызвать логику проверки в doRead, как показано @Nghia Do.

Item validation - это типичный вариант использования процессора предмета. Если вы используете Spring Batch 4.1+, вы можете использовать BeanValidatingItemProcessor в своем шаге, ориентированном на чанк. Вы можете найти пример здесь: https://docs.spring.io/spring-batch/4.1.x/reference/html/whatsnew.html#whatsNewBeanValidationApi

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...