diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/BeanWrapperFieldSetMapper.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/BeanWrapperFieldSetMapper.java index d3741e913..31a153147 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/BeanWrapperFieldSetMapper.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/BeanWrapperFieldSetMapper.java @@ -90,6 +90,8 @@ public class BeanWrapperFieldSetMapper extends DefaultPropertyEditorRegistrar private static Map, Map> propertiesMatched = new HashMap, Map>(); private static int distanceLimit = 5; + + private boolean strict = true; /* * (non-Javadoc) @@ -186,7 +188,7 @@ public class BeanWrapperFieldSetMapper extends DefaultPropertyEditorRegistrar */ protected DataBinder createBinder(Object target) { DataBinder binder = new DataBinder(target); - binder.setIgnoreUnknownFields(false); + binder.setIgnoreUnknownFields(!this.strict); initBinder(binder); registerCustomEditors(binder); return binder; @@ -320,4 +322,14 @@ public class BeanWrapperFieldSetMapper extends DefaultPropertyEditorRegistrar properties.setProperty(newName, value); } + /** + * Public setter for the 'strict' property. If true, then + * {@link #mapFieldSet(FieldSet)} will fail of the FieldSet contains fields + * that cannot be mapped to the bean. + * + * @param strict + */ + public void setStrict(boolean strict) { + this.strict = strict; + } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/mapping/BeanWrapperFieldSetMapperTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/mapping/BeanWrapperFieldSetMapperTests.java index d3b7e0388..84a023d94 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/mapping/BeanWrapperFieldSetMapperTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/mapping/BeanWrapperFieldSetMapperTests.java @@ -403,4 +403,34 @@ public class BeanWrapperFieldSetMapperTests extends TestCase { } } + public void testStrict() throws Exception { + BeanWrapperFieldSetMapper mapper = new BeanWrapperFieldSetMapper(); + mapper.setStrict(true); + mapper.setTargetType(TestObject.class); + mapper.afterPropertiesSet(); + + FieldSet fieldSet = new DefaultFieldSet(new String[] { "This is some dummy string", "This won't be mapped", + "true", "C" }, new String[] { "varString", "illegalPropertyName", "varBoolean", "varChar" }); + try { + mapper.mapFieldSet(fieldSet); + fail("expected error"); + } + catch(NotWritablePropertyException e) { + assertTrue(e.getMessage().contains("'illegalPropertyName'")); + } + } + + public void testNotStrict() throws Exception { + BeanWrapperFieldSetMapper mapper = new BeanWrapperFieldSetMapper(); + mapper.setStrict(false); + mapper.setTargetType(TestObject.class); + mapper.afterPropertiesSet(); + + FieldSet fieldSet = new DefaultFieldSet(new String[] { "This is some dummy string", "This won't be mapped", + "true", "C" }, new String[] { "varString", "illegalPropertyName", "varBoolean", "varChar" }); + TestObject result = mapper.mapFieldSet(fieldSet); + assertEquals("This is some dummy string", result.getVarString()); + assertEquals(true, result.isVarBoolean()); + assertEquals('C', result.getVarChar()); + } }