use of org.springframework.format.support.FormattingConversionService in project spring-boot by spring-projects.
the class WebMvcAutoConfigurationTests method overrideDateFormat.
@Test
public void overrideDateFormat() throws Exception {
load(AllResources.class, "spring.mvc.dateFormat:dd*MM*yyyy");
FormattingConversionService cs = this.context.getBean(FormattingConversionService.class);
Date date = new DateTime(1988, 6, 25, 20, 30).toDate();
assertThat(cs.convert(date, String.class)).isEqualTo("25*06*1988");
}
use of org.springframework.format.support.FormattingConversionService in project spring-framework by spring-projects.
the class JodaTimeFormattingTests method setUp.
private void setUp(JodaTimeFormatterRegistrar registrar) {
conversionService = new FormattingConversionService();
DefaultConversionService.addDefaultConverters(conversionService);
registrar.registerFormatters(conversionService);
JodaTimeBean bean = new JodaTimeBean();
bean.getChildren().add(new JodaTimeBean());
binder = new DataBinder(bean);
binder.setConversionService(conversionService);
LocaleContextHolder.setLocale(Locale.US);
JodaTimeContext context = new JodaTimeContext();
context.setTimeZone(DateTimeZone.forID("-05:00"));
JodaTimeContextHolder.setJodaTimeContext(context);
}
use of org.springframework.format.support.FormattingConversionService in project spring-data-commons by spring-projects.
the class SpringDataWebConfiguration method addFormatters.
/*
* (non-Javadoc)
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addFormatters(org.springframework.format.FormatterRegistry)
*/
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addFormatter(DistanceFormatter.INSTANCE);
registry.addFormatter(PointFormatter.INSTANCE);
if (!(registry instanceof FormattingConversionService)) {
return;
}
FormattingConversionService conversionService = (FormattingConversionService) registry;
DomainClassConverter<FormattingConversionService> converter = new DomainClassConverter<>(conversionService);
converter.setApplicationContext(context);
}
use of org.springframework.format.support.FormattingConversionService in project spring-framework by spring-projects.
the class WebMvcConfigurationSupportTests method requestMappingHandlerAdapter.
@Test
public void requestMappingHandlerAdapter() {
ApplicationContext context = initContext(WebConfig.class);
RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
assertThat(converters.size()).isEqualTo(12);
converters.stream().filter(converter -> converter instanceof AbstractJackson2HttpMessageConverter).forEach(converter -> {
ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter) converter).getObjectMapper();
assertThat(mapper.getDeserializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION)).isFalse();
assertThat(mapper.getSerializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION)).isFalse();
assertThat(mapper.getDeserializationConfig().isEnabled(FAIL_ON_UNKNOWN_PROPERTIES)).isFalse();
if (converter instanceof MappingJackson2XmlHttpMessageConverter) {
assertThat(mapper.getClass()).isEqualTo(XmlMapper.class);
}
});
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
assertThat(initializer).isNotNull();
ConversionService conversionService = initializer.getConversionService();
assertThat(conversionService).isNotNull();
boolean condition1 = conversionService instanceof FormattingConversionService;
assertThat(condition1).isTrue();
Validator validator = initializer.getValidator();
assertThat(validator).isNotNull();
boolean condition = validator instanceof LocalValidatorFactoryBean;
assertThat(condition).isTrue();
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
@SuppressWarnings("unchecked") List<Object> bodyAdvice = (List<Object>) fieldAccessor.getPropertyValue("requestResponseBodyAdvice");
assertThat(bodyAdvice.size()).isEqualTo(2);
assertThat(bodyAdvice.get(0).getClass()).isEqualTo(JsonViewRequestBodyAdvice.class);
assertThat(bodyAdvice.get(1).getClass()).isEqualTo(JsonViewResponseBodyAdvice.class);
}
use of org.springframework.format.support.FormattingConversionService in project spring-framework by spring-projects.
the class WebFluxConfigurationSupport method webFluxConversionService.
/**
* Return a {@link FormattingConversionService} for use with annotated controllers.
* <p>See {@link #addFormatters} as an alternative to overriding this method.
*/
@Bean
public FormattingConversionService webFluxConversionService() {
FormattingConversionService service = new DefaultFormattingConversionService();
addFormatters(service);
return service;
}
Aggregations