private void registerFormatters(FormattingConversionService conversionService) { if (this.formatters != null) { for (Object formatter : this.formatters) { if (formatter instanceof Formatter<?>) { conversionService.addFormatter((Formatter<?>) formatter); } else if (formatter instanceof AnnotationFormatterFactory<?>) { conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter); } else { throw new IllegalArgumentException( "Custom formatters must be implementations of Formatter or AnnotationFormatterFactory"); } } } if (this.formatterRegistrars != null) { for (FormatterRegistrar registrar : this.formatterRegistrars) { registrar.registerFormatters(conversionService); } } }
@Test public void proxiedFormatter() throws ParseException { Formatter<?> formatter = new NumberStyleFormatter(); formattingService.addFormatter((Formatter<?>) new ProxyFactory(formatter).getProxy()); assertNull(formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))); }
private void registerFormatters(FormattingConversionService conversionService) { if (this.formatters != null) { for (Object formatter : this.formatters) { if (formatter instanceof Formatter<?>) { conversionService.addFormatter((Formatter<?>) formatter); } else if (formatter instanceof AnnotationFormatterFactory<?>) { conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter); } else { throw new IllegalArgumentException( "Custom formatters must be implementations of Formatter or AnnotationFormatterFactory"); } } } if (this.formatterRegistrars != null) { for (FormatterRegistrar registrar : this.formatterRegistrars) { registrar.registerFormatters(conversionService); } } }
@Test public void introspectedFormatter() throws ParseException { formattingService.addFormatter(new NumberStyleFormatter()); assertNull(formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))); }
private void registerFormatters(FormattingConversionService conversionService) { if (this.formatters != null) { for (Object formatter : this.formatters) { if (formatter instanceof Formatter<?>) { conversionService.addFormatter((Formatter<?>) formatter); } else if (formatter instanceof AnnotationFormatterFactory<?>) { conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter); } else { throw new IllegalArgumentException( "Custom formatters must be implementations of Formatter or AnnotationFormatterFactory"); } } } if (this.formatterRegistrars != null) { for (FormatterRegistrar registrar : this.formatterRegistrars) { registrar.registerFormatters(conversionService); } } }
@Test public void testBindingErrorWithParseExceptionFromFormatter() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatter(new Formatter<String>() { @Override public String parse(String text, Locale locale) throws ParseException { throw new ParseException(text, 0); } @Override public String print(String object, Locale locale) { return object; } }); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "test"); binder.bind(pvs); assertTrue(binder.getBindingResult().hasFieldErrors("name")); assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode()); assertEquals("test", binder.getBindingResult().getFieldValue("name")); }
@Test public void testBindingErrorWithRuntimeExceptionFromFormatter() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatter(new Formatter<String>() { @Override public String parse(String text, Locale locale) throws ParseException { throw new RuntimeException(text); } @Override public String print(String object, Locale locale) { return object; } }); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "test"); binder.bind(pvs); assertTrue(binder.getBindingResult().hasFieldErrors("name")); assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode()); assertEquals("test", binder.getBindingResult().getFieldValue("name")); }
public class WebMvcConfiguration extends WebMvcConfigurerAdapter { other stuff..... @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasenames("Messages/Messages", "Messages/Labels"); messageSource.setDefaultEncoding("UTF-8"); messageSource.setCacheSeconds(1); return messageSource; } @Bean public AppDateFormatter appDateFormatter(){ return new AppDateFormatter(); } @Bean public FormattingConversionService mvcConversionService() { FormattingConversionService conversionService = new DefaultFormattingConversionService(); conversionService.addFormatter(appDateFormatter()); return conversionService; } }