use of org.springframework.format.support.FormattingConversionService in project spring-framework by spring-projects.
the class WebMvcConfigurationSupport method mvcConversionService.
/**
* Return a {@link FormattingConversionService} for use with annotated controllers.
* <p>See {@link #addFormatters} as an alternative to overriding this method.
*/
@Bean
public FormattingConversionService mvcConversionService() {
FormattingConversionService conversionService = new DefaultFormattingConversionService();
addFormatters(conversionService);
return conversionService;
}
use of org.springframework.format.support.FormattingConversionService in project spring-framework by spring-projects.
the class DelegatingWebFluxConfigurationTests method requestMappingHandlerAdapter.
@Test
public void requestMappingHandlerAdapter() {
delegatingConfig.setConfigurers(Collections.singletonList(webFluxConfigurer));
ReactiveAdapterRegistry reactiveAdapterRegistry = delegatingConfig.webFluxAdapterRegistry();
ServerCodecConfigurer serverCodecConfigurer = delegatingConfig.serverCodecConfigurer();
FormattingConversionService formattingConversionService = delegatingConfig.webFluxConversionService();
Validator validator = delegatingConfig.webFluxValidator();
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) this.delegatingConfig.requestMappingHandlerAdapter(reactiveAdapterRegistry, serverCodecConfigurer, formattingConversionService, validator).getWebBindingInitializer();
verify(webFluxConfigurer).configureHttpMessageCodecs(codecsConfigurer.capture());
verify(webFluxConfigurer).getValidator();
verify(webFluxConfigurer).getMessageCodesResolver();
verify(webFluxConfigurer).addFormatters(formatterRegistry.capture());
verify(webFluxConfigurer).configureArgumentResolvers(any());
assertThat(initializer).isNotNull();
boolean condition = initializer.getValidator() instanceof LocalValidatorFactoryBean;
assertThat(condition).isTrue();
assertThat(initializer.getConversionService()).isSameAs(formatterRegistry.getValue());
assertThat(codecsConfigurer.getValue().getReaders().size()).isEqualTo(14);
}
use of org.springframework.format.support.FormattingConversionService in project spring-framework by spring-projects.
the class CheckboxesTagTests method withMultiValueWithFormatter.
@Test
public void withMultiValueWithFormatter() throws Exception {
this.tag.setPath("stringArray");
this.tag.setItems(new Object[] { " foo", " bar", " baz" });
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
FormattingConversionService cs = new FormattingConversionService();
cs.addFormatterForFieldType(String.class, new Formatter<String>() {
@Override
public String print(String object, Locale locale) {
return object;
}
@Override
public String parse(String text, Locale locale) throws ParseException {
return text.trim();
}
});
bindingResult.initConversion(cs);
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);
int result = this.tag.doStartTag();
assertThat(result).isEqualTo(Tag.SKIP_BODY);
String output = getOutput();
// wrap the output so it is valid XML
output = "<doc>" + output + "</doc>";
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(output));
Element spanElement1 = document.getRootElement().elements().get(0);
Element checkboxElement1 = spanElement1.elements().get(0);
assertThat(checkboxElement1.getName()).isEqualTo("input");
assertThat(checkboxElement1.attribute("type").getValue()).isEqualTo("checkbox");
assertThat(checkboxElement1.attribute("name").getValue()).isEqualTo("stringArray");
assertThat(checkboxElement1.attribute("checked").getValue()).isEqualTo("checked");
assertThat(checkboxElement1.attribute("value").getValue()).isEqualTo(" foo");
Element spanElement2 = document.getRootElement().elements().get(1);
Element checkboxElement2 = spanElement2.elements().get(0);
assertThat(checkboxElement2.getName()).isEqualTo("input");
assertThat(checkboxElement2.attribute("type").getValue()).isEqualTo("checkbox");
assertThat(checkboxElement2.attribute("name").getValue()).isEqualTo("stringArray");
assertThat(checkboxElement2.attribute("checked").getValue()).isEqualTo("checked");
assertThat(checkboxElement2.attribute("value").getValue()).isEqualTo(" bar");
Element spanElement3 = document.getRootElement().elements().get(2);
Element checkboxElement3 = spanElement3.elements().get(0);
assertThat(checkboxElement3.getName()).isEqualTo("input");
assertThat(checkboxElement3.attribute("type").getValue()).isEqualTo("checkbox");
assertThat(checkboxElement3.attribute("name").getValue()).isEqualTo("stringArray");
assertThat(checkboxElement3.attribute("checked")).as("not checked").isNull();
assertThat(checkboxElement3.attribute("value").getValue()).isEqualTo(" baz");
}
use of org.springframework.format.support.FormattingConversionService in project spring-boot by spring-projects.
the class WebFluxAutoConfigurationTests method defaultDateFormat.
@Test
void defaultDateFormat() {
this.contextRunner.run((context) -> {
FormattingConversionService conversionService = context.getBean(FormattingConversionService.class);
Date date = Date.from(ZonedDateTime.of(1988, 6, 25, 20, 30, 0, 0, ZoneId.systemDefault()).toInstant());
// formatting conversion service should use simple toString()
assertThat(conversionService.convert(date, String.class)).isEqualTo(date.toString());
});
}
use of org.springframework.format.support.FormattingConversionService in project spring-boot by spring-projects.
the class WebFluxAutoConfigurationTests method customDateTimeTimeFormat.
@Test
void customDateTimeTimeFormat() {
this.contextRunner.withPropertyValues("spring.webflux.format.date-time=yyyy-MM-dd HH:mm:ss").run((context) -> {
FormattingConversionService conversionService = context.getBean(FormattingConversionService.class);
LocalDateTime dateTime = LocalDateTime.of(2020, 4, 28, 11, 43, 10);
assertThat(conversionService.convert(dateTime, String.class)).isEqualTo("2020-04-28 11:43:10");
});
}
Aggregations