Search in sources :

Example 1 with FormattingConversionService

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");
}
Also used : FormattingConversionService(org.springframework.format.support.FormattingConversionService) Date(java.util.Date) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 2 with FormattingConversionService

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);
}
Also used : DataBinder(org.springframework.validation.DataBinder) FormattingConversionService(org.springframework.format.support.FormattingConversionService)

Example 3 with FormattingConversionService

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);
}
Also used : DomainClassConverter(org.springframework.data.repository.support.DomainClassConverter) FormattingConversionService(org.springframework.format.support.FormattingConversionService)

Example 4 with FormattingConversionService

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);
}
Also used : LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) PathVariable(org.springframework.web.bind.annotation.PathVariable) DefaultRequestToViewNameTranslator(org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator) DEFAULT_VIEW_INCLUSION(com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION) Validator(org.springframework.validation.Validator) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) LocaleResolver(org.springframework.web.servlet.LocaleResolver) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) DateTimeFormat(org.springframework.format.annotation.DateTimeFormat) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) FAIL_ON_UNKNOWN_PROPERTIES(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) HandlerExceptionResolverComposite(org.springframework.web.servlet.handler.HandlerExceptionResolverComposite) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) Locale(java.util.Locale) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) AntPathMatcher(org.springframework.util.AntPathMatcher) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) JsonViewRequestBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewRequestBodyAdvice) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ISO(org.springframework.format.annotation.DateTimeFormat.ISO) StaticMessageSource(org.springframework.context.support.StaticMessageSource) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) HandlerMapping(org.springframework.web.servlet.HandlerMapping) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) FlashMapManager(org.springframework.web.servlet.FlashMapManager) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) ExceptionHandlerExceptionResolver(org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) FLASH_MAP_MANAGER_BEAN_NAME(org.springframework.web.servlet.DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME) ResponseStatusExceptionResolver(org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) HandlerMethodReturnValueHandler(org.springframework.web.method.support.HandlerMethodReturnValueHandler) THEME_RESOLVER_BEAN_NAME(org.springframework.web.servlet.DispatcherServlet.THEME_RESOLVER_BEAN_NAME) REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME(org.springframework.web.servlet.DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME) Ordered(org.springframework.core.Ordered) ResourceUrlProviderExposingInterceptor(org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ThemeResolver(org.springframework.web.servlet.ThemeResolver) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Controller(org.springframework.stereotype.Controller) Scope(org.springframework.context.annotation.Scope) RequestToViewNameTranslator(org.springframework.web.servlet.RequestToViewNameTranslator) LOCALE_RESOLVER_BEAN_NAME(org.springframework.web.servlet.DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME) CompositeUriComponentsContributor(org.springframework.web.method.support.CompositeUriComponentsContributor) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ConversionService(org.springframework.core.convert.ConversionService) MessageSource(org.springframework.context.MessageSource) ViewResolver(org.springframework.web.servlet.ViewResolver) PathMatcher(org.springframework.util.PathMatcher) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ScopedProxyMode(org.springframework.context.annotation.ScopedProxyMode) FormattingConversionService(org.springframework.format.support.FormattingConversionService) ApplicationContext(org.springframework.context.ApplicationContext) BeanNameViewResolver(org.springframework.web.servlet.view.BeanNameViewResolver) HttpStatus(org.springframework.http.HttpStatus) BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) UrlPathHelper(org.springframework.web.util.UrlPathHelper) Bean(org.springframework.context.annotation.Bean) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) FormattingConversionService(org.springframework.format.support.FormattingConversionService) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) ConversionService(org.springframework.core.convert.ConversionService) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) List(java.util.List) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Validator(org.springframework.validation.Validator) Test(org.junit.jupiter.api.Test)

Example 5 with FormattingConversionService

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;
}
Also used : FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Bean(org.springframework.context.annotation.Bean)

Aggregations

FormattingConversionService (org.springframework.format.support.FormattingConversionService)38 Test (org.junit.jupiter.api.Test)27 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)13 Date (java.util.Date)8 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)8 Locale (java.util.Locale)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)6 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)6 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)6 TestBean (org.springframework.beans.testfixture.beans.TestBean)6 NumberStyleFormatter (org.springframework.format.number.NumberStyleFormatter)6 ParseException (java.text.ParseException)5 LocalDateTime (java.time.LocalDateTime)4 LocalTime (java.time.LocalTime)4 List (java.util.List)4 Bean (org.springframework.context.annotation.Bean)4 StringReader (java.io.StringReader)3 Document (org.dom4j.Document)3 Element (org.dom4j.Element)3