Add a validator based on the Bean Validation API

Resolves BATCH-2690
This commit is contained in:
Mahmoud Ben Hassine
2018-06-26 16:21:09 +02:00
parent 84d349ea9d
commit 9f2b752f3f
8 changed files with 444 additions and 1 deletions

View File

@@ -2659,6 +2659,45 @@ public SpringValidator validator() {
}
----
You can also use the `BeanValidatingItemProcessor` to validate items annotated with
the Bean Validation API (JSR-303) annotations. For example, given the following type `Person`:
[source, java]
----
class Person {
@NotEmpty
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
----
you can validate items by declaring a `BeanValidatingItemProcessor` bean in your
application context and register it as a processor in your chunk-oriented step:
[source, java]
----
@Bean
public BeanValidatingItemProcessor<Person> beanValidatingItemProcessor() throws Exception {
BeanValidatingItemProcessor<Person> beanValidatingItemProcessor = new BeanValidatingItemProcessor<>();
beanValidatingItemProcessor.setFilter(true);
return beanValidatingItemProcessor;
}
----
[[process-indicator]]
=== Preventing State Persistence

View File

@@ -11,6 +11,7 @@ The Spring Batch 4.1 release adds the following features:
* A new `@SpringBatchTest` annotation to simplify testing batch components
* A new `@EnableBatchIntegration` annotation to simplify remote chunking configuration
* A new `JsonItemReader` and `JsonFileItemWriter` to support the JSON format
* Add support for validating items with the Bean Validation API
[[whatsNewTesting]]
=== `@SpringBatchTest` Annotation
@@ -160,3 +161,47 @@ Writing JSON data is also supported through the `JsonFileItemWriter`.
For more details about JSON support, see the
<<readersAndWriters.adoc#jsonReadingWriting,ItemReaders and ItemWriters>> chapter.
[[whatsNewBeanValidationApi]]
=== Bean Validation API support
This release brings a new `ValidatingItemProcessor` implementation called
`BeanValidatingItemProcessor` which allows you to validate items annotated with
the Bean Validation API (JSR-303) annotations. For example, given the following
type `Person`:
[source, java]
----
class Person {
@NotEmpty
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
----
you can validate items by declaring a `BeanValidatingItemProcessor` bean in your
application context and register it as a processor in your chunk-oriented step:
[source, java]
----
@Bean
public BeanValidatingItemProcessor<Person> beanValidatingItemProcessor() throws Exception {
BeanValidatingItemProcessor<Person> beanValidatingItemProcessor = new BeanValidatingItemProcessor<>();
beanValidatingItemProcessor.setFilter(true);
return beanValidatingItemProcessor;
}
----