Java Code Examples for org.springframework.web.bind.support.ConfigurableWebBindingInitializer#getConversionService()

The following examples show how to use org.springframework.web.bind.support.ConfigurableWebBindingInitializer#getConversionService() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DelegatingWebMvcConfigurationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void requestMappingHandlerAdapter() throws Exception {

	delegatingConfig.setConfigurers(Arrays.asList(webMvcConfigurer));
	RequestMappingHandlerAdapter adapter = delegatingConfig.requestMappingHandlerAdapter();

	ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
	ConversionService initializerConversionService = initializer.getConversionService();
	assertTrue(initializer.getValidator() instanceof LocalValidatorFactoryBean);

	verify(webMvcConfigurer).configureMessageConverters(converters.capture());
	verify(webMvcConfigurer).configureContentNegotiation(contentNegotiationConfigurer.capture());
	verify(webMvcConfigurer).addFormatters(conversionService.capture());
	verify(webMvcConfigurer).addArgumentResolvers(resolvers.capture());
	verify(webMvcConfigurer).addReturnValueHandlers(handlers.capture());
	verify(webMvcConfigurer).configureAsyncSupport(asyncConfigurer.capture());

	assertSame(conversionService.getValue(), initializerConversionService);
	assertEquals(0, resolvers.getValue().size());
	assertEquals(0, handlers.getValue().size());
	assertEquals(converters.getValue(), adapter.getMessageConverters());
	assertNotNull(asyncConfigurer);
}
 
Example 2
Source File: WebConfig.java    From erp-framework with MIT License 5 votes vote down vote up
/**
 * 增加字符串转日期的功能
 */
@PostConstruct
public void initEditableValidation() {
    ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter
            .getWebBindingInitializer();
    if (initializer.getConversionService() != null) {
        GenericConversionService genericConversionService = (GenericConversionService) initializer
                .getConversionService();
        genericConversionService.addConverter(new DateConverter());
    }
}
 
Example 3
Source File: WebMvcConfigurationSupportTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void requestMappingHandlerAdapter() throws Exception {
	ApplicationContext context = initContext(WebConfig.class);
	RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
	List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
	assertEquals(12, converters.size());
	converters.stream()
			.filter(converter -> converter instanceof AbstractJackson2HttpMessageConverter)
			.forEach(converter -> {
				ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter) converter).getObjectMapper();
				assertFalse(mapper.getDeserializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION));
				assertFalse(mapper.getSerializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION));
				assertFalse(mapper.getDeserializationConfig().isEnabled(FAIL_ON_UNKNOWN_PROPERTIES));
				if (converter instanceof MappingJackson2XmlHttpMessageConverter) {
					assertEquals(XmlMapper.class, mapper.getClass());
				}
			});

	ConfigurableWebBindingInitializer initializer =
			(ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
	assertNotNull(initializer);

	ConversionService conversionService = initializer.getConversionService();
	assertNotNull(conversionService);
	assertTrue(conversionService instanceof FormattingConversionService);

	Validator validator = initializer.getValidator();
	assertNotNull(validator);
	assertTrue(validator instanceof LocalValidatorFactoryBean);

	DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
	@SuppressWarnings("unchecked")
	List<Object> bodyAdvice = (List<Object>) fieldAccessor.getPropertyValue("requestResponseBodyAdvice");
	assertEquals(2, bodyAdvice.size());
	assertEquals(JsonViewRequestBodyAdvice.class, bodyAdvice.get(0).getClass());
	assertEquals(JsonViewResponseBodyAdvice.class, bodyAdvice.get(1).getClass());
}
 
Example 4
Source File: WebConfigBeans.java    From youkefu with Apache License 2.0 5 votes vote down vote up
/**
 * 增加字符串转日期的功能
 */
@PostConstruct
public void initEditableValidation() {
	
    ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter
        .getWebBindingInitializer();
    if (initializer.getConversionService() != null) {
        GenericConversionService genericConversionService = (GenericConversionService) initializer
            .getConversionService();
        genericConversionService.addConverter(new StringToDateConverter());
    }

}
 
Example 5
Source File: MyWebAppConfiguration.java    From molicode with Apache License 2.0 5 votes vote down vote up
@PostConstruct
 public void initEditableValidation(){
    ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
    if(initializer.getConversionService()!=null){
        GenericConversionService conversionService = (GenericConversionService) initializer.getConversionService();
        conversionService.addConverter(new StringToDateConverter());
    }
}
 
Example 6
Source File: WebMvcConfigurationSupportTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void requestMappingHandlerAdapter() throws Exception {
	ApplicationContext context = initContext(WebConfig.class);
	RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
	List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
	assertEquals(12, converters.size());
	converters.stream()
			.filter(converter -> converter instanceof AbstractJackson2HttpMessageConverter)
			.forEach(converter -> {
				ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter) converter).getObjectMapper();
				assertFalse(mapper.getDeserializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION));
				assertFalse(mapper.getSerializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION));
				assertFalse(mapper.getDeserializationConfig().isEnabled(FAIL_ON_UNKNOWN_PROPERTIES));
				if (converter instanceof MappingJackson2XmlHttpMessageConverter) {
					assertEquals(XmlMapper.class, mapper.getClass());
				}
			});

	ConfigurableWebBindingInitializer initializer =
			(ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
	assertNotNull(initializer);

	ConversionService conversionService = initializer.getConversionService();
	assertNotNull(conversionService);
	assertTrue(conversionService instanceof FormattingConversionService);

	Validator validator = initializer.getValidator();
	assertNotNull(validator);
	assertTrue(validator instanceof LocalValidatorFactoryBean);

	DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
	@SuppressWarnings("unchecked")
	List<Object> bodyAdvice = (List<Object>) fieldAccessor.getPropertyValue("requestResponseBodyAdvice");
	assertEquals(2, bodyAdvice.size());
	assertEquals(JsonViewRequestBodyAdvice.class, bodyAdvice.get(0).getClass());
	assertEquals(JsonViewResponseBodyAdvice.class, bodyAdvice.get(1).getClass());
}
 
Example 7
Source File: String2DateConfig.java    From Guns with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 默认时间转化器
 */
@PostConstruct
public void addConversionConfig() {
    ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter.getWebBindingInitializer();
    if ((initializer != null ? initializer.getConversionService() : null) != null) {
        GenericConversionService genericConversionService = (GenericConversionService) initializer.getConversionService();
        genericConversionService.addConverter(new StringToDateConverter());
    }
}
 
Example 8
Source File: ConfigurerAdapter.java    From Resource with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 日期类型转换
 */
@PostConstruct
public void initEditableValidation()
{
    ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer)adapter.getWebBindingInitializer();
    if (initializer.getConversionService() != null)
    {
        GenericConversionService service = (GenericConversionService)initializer.getConversionService();
        service.addConverter(new StringToDateConverter());
    }
}
 
Example 9
Source File: WebMvcConfigurationSupportTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void requestMappingHandlerAdapter() throws Exception {
	ApplicationContext context = initContext(WebConfig.class);
	RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
	List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
	assertEquals(9, converters.size());
	for(HttpMessageConverter<?> converter : converters) {
		if (converter instanceof AbstractJackson2HttpMessageConverter) {
			ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
			assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
			assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
			assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
			if (converter instanceof MappingJackson2XmlHttpMessageConverter) {
				assertEquals(XmlMapper.class, objectMapper.getClass());
			}
		}
	}

	ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
	assertNotNull(initializer);

	ConversionService conversionService = initializer.getConversionService();
	assertNotNull(conversionService);
	assertTrue(conversionService instanceof FormattingConversionService);

	Validator validator = initializer.getValidator();
	assertNotNull(validator);
	assertTrue(validator instanceof LocalValidatorFactoryBean);

	DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
	@SuppressWarnings("unchecked")
	List<Object> bodyAdvice = (List<Object>) fieldAccessor.getPropertyValue("requestResponseBodyAdvice");
	assertEquals(2, bodyAdvice.size());
	assertEquals(JsonViewRequestBodyAdvice.class, bodyAdvice.get(0).getClass());
	assertEquals(JsonViewResponseBodyAdvice.class, bodyAdvice.get(1).getClass());
}
 
Example 10
Source File: DefaultWebConfig.java    From MeetingFilm with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void addConversionConfig() {
    ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter.getWebBindingInitializer();
    GenericConversionService genericConversionService = (GenericConversionService) initializer.getConversionService();
    genericConversionService.addConverter(new StringToDateConverter());
}