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

The following examples show how to use org.springframework.web.bind.support.ConfigurableWebBindingInitializer#setConversionService() . 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: RequestParamMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamValue() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
	Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.empty(), result);

	request.addParameter("name", "123");
	result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.class, result.getClass());
	assertEquals(123, ((Optional) result).get());
}
 
Example 2
Source File: RequestParamMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void resolveOptionalMultipartFile() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
	MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes());
	request.addFile(expected);
	webRequest = new ServletWebRequest(request);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
	Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);

	assertTrue(result instanceof Optional);
	assertEquals("Invalid result", expected, ((Optional<?>) result).get());
}
 
Example 3
Source File: RequestParamMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamArray() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer[].class);
	Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.empty(), result);

	request.addParameter("name", "123", "456");
	result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.class, result.getClass());
	assertArrayEquals(new Integer[] {123, 456}, (Integer[]) ((Optional) result).get());
}
 
Example 4
Source File: ServletModelAttributeMethodProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
public void setup() throws Exception {
	processor = new ServletModelAttributeMethodProcessor(false);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	binderFactory = new ServletRequestDataBinderFactory(null, initializer);

	mavContainer = new ModelAndViewContainer();
	request = new MockHttpServletRequest();
	webRequest = new ServletWebRequest(request);

	Method method = getClass().getDeclaredMethod("modelAttribute",
			TestBean.class, TestBeanWithoutStringConstructor.class, Optional.class);
	testBeanModelAttr = new MethodParameter(method, 0);
	testBeanWithoutStringConstructorModelAttr = new MethodParameter(method, 1);
	testBeanWithOptionalModelAttr = new MethodParameter(method, 2);
}
 
Example 5
Source File: ServletModelAttributeMethodProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	this.processor = new ServletModelAttributeMethodProcessor(false);

	Method method = getClass().getDeclaredMethod("modelAttribute",
			TestBean.class, TestBeanWithoutStringConstructor.class, Optional.class);

	this.testBeanModelAttr = new MethodParameter(method, 0);
	this.testBeanWithoutStringConstructorModelAttr = new MethodParameter(method, 1);
	this.testBeanWithOptionalModelAttr = new MethodParameter(method, 2);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());

	this.binderFactory = new ServletRequestDataBinderFactory(null, initializer);
	this.mavContainer = new ModelAndViewContainer();

	this.request = new MockHttpServletRequest();
	this.webRequest = new ServletWebRequest(request);
}
 
Example 6
Source File: RequestParamMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamValue() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
	Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.empty(), result);

	request.addParameter("name", "123");
	result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.class, result.getClass());
	assertEquals(123, ((Optional) result).get());
}
 
Example 7
Source File: RequestHeaderMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();
	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultFormattingConversionService());
	this.bindingContext = new BindingContext(initializer);

	Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
	this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
	this.paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
	this.paramSystemProperty = new SynthesizingMethodParameter(method, 2);
	this.paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 3);
	this.paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 4);
	this.paramNamedValueMap = new SynthesizingMethodParameter(method, 5);
	this.paramDate = new SynthesizingMethodParameter(method, 6);
	this.paramInstant = new SynthesizingMethodParameter(method, 7);
	this.paramMono = new SynthesizingMethodParameter(method, 8);
}
 
Example 8
Source File: RequestAttributeMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void resolveOptional() {
	MethodParameter param = this.testMethod.annot(requestAttribute().name("foo")).arg(Optional.class, Foo.class);
	Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);

	assertNotNull(mono.block());
	assertEquals(Optional.class, mono.block().getClass());
	assertFalse(((Optional<?>) mono.block()).isPresent());

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultFormattingConversionService());
	BindingContext bindingContext = new BindingContext(initializer);

	Foo foo = new Foo();
	this.exchange.getAttributes().put("foo", foo);
	mono = this.resolver.resolveArgument(param, bindingContext, this.exchange);

	assertNotNull(mono.block());
	assertEquals(Optional.class, mono.block().getClass());
	Optional<?> optional = (Optional<?>) mono.block();
	assertTrue(optional.isPresent());
	assertSame(foo, optional.get());
}
 
Example 9
Source File: WebFluxConfigurationSupport.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Return the {@link ConfigurableWebBindingInitializer} to use for
 * initializing all {@link WebDataBinder} instances.
 */
protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer() {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(webFluxConversionService());
	initializer.setValidator(webFluxValidator());
	MessageCodesResolver messageCodesResolver = getMessageCodesResolver();
	if (messageCodesResolver != null) {
		initializer.setMessageCodesResolver(messageCodesResolver);
	}
	return initializer;
}
 
Example 10
Source File: WebMvcConfigurationSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Return the {@link ConfigurableWebBindingInitializer} to use for
 * initializing all {@link WebDataBinder} instances.
 */
protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer(
		FormattingConversionService mvcConversionService,
		Validator mvcValidator) {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(mvcConversionService);
	initializer.setValidator(mvcValidator);
	MessageCodesResolver messageCodesResolver = getMessageCodesResolver();
	if (messageCodesResolver != null) {
		initializer.setMessageCodesResolver(messageCodesResolver);
	}
	return initializer;
}
 
Example 11
Source File: RequestParamMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {

	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new RequestParamMethodArgumentResolver(null, adapterRegistry, true);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultFormattingConversionService());
	this.bindContext = new BindingContext(initializer);
}
 
Example 12
Source File: RequestHeaderMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("deprecation")
public void dateConversion() throws Exception {
	String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100";
	servletRequest.addHeader("name", rfc1123val);

	ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
	bindingInitializer.setConversionService(new DefaultFormattingConversionService());
	Object result = resolver.resolveArgument(paramDate, null, webRequest,
			new DefaultDataBinderFactory(bindingInitializer));

	assertTrue(result instanceof Date);
	assertEquals(new Date(rfc1123val), result);
}
 
Example 13
Source File: PathVariableMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void wrapEmptyWithOptional() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	assertEquals(Optional.empty(), resolver.resolveArgument(paramOptional, mavContainer, webRequest, binderFactory));
}
 
Example 14
Source File: WebFluxConfigurationSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Return the {@link ConfigurableWebBindingInitializer} to use for
 * initializing all {@link WebDataBinder} instances.
 */
protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer(
		FormattingConversionService webFluxConversionService,
		Validator webFluxValidator) {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(webFluxConversionService);
	initializer.setValidator(webFluxValidator);
	MessageCodesResolver messageCodesResolver = getMessageCodesResolver();
	if (messageCodesResolver != null) {
		initializer.setMessageCodesResolver(messageCodesResolver);
	}
	return initializer;
}
 
Example 15
Source File: RequestParamMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void missingOptionalParamValue() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
	Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.empty(), result);

	result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.class, result.getClass());
	assertFalse(((Optional) result).isPresent());
}
 
Example 16
Source File: RequestParamMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void missingOptionalMultipartFile() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	request.setMethod("POST");
	request.setContentType("multipart/form-data");

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
	Object actual = resolver.resolveArgument(param, null, webRequest, binderFactory);

	assertEquals(Optional.empty(), actual);
}
 
Example 17
Source File: RequestParamMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void missingOptionalParamArray() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer[].class);
	Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.empty(), result);

	result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.class, result.getClass());
	assertFalse(((Optional) result).isPresent());
}
 
Example 18
Source File: RequestParamMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void missingOptionalParamList() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, List.class);
	Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.empty(), result);

	result = resolver.resolveArgument(param, null, webRequest, binderFactory);
	assertEquals(Optional.class, result.getClass());
	assertFalse(((Optional) result).isPresent());
}
 
Example 19
Source File: PathVariableMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void resolveArgumentWrappedAsOptional() throws Exception {
	Map<String, String> uriTemplateVars = new HashMap<>();
	uriTemplateVars.put("name", "value");
	this.exchange.getAttributes().put(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultFormattingConversionService());
	BindingContext bindingContext = new BindingContext(initializer);

	Mono<Object> mono = this.resolver.resolveArgument(this.paramOptional, bindingContext, this.exchange);
	Object result = mono.block();
	assertEquals(Optional.of("value"), result);
}
 
Example 20
Source File: RequestParamMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void optionalMultipartFileWithoutMultipartRequest() throws Exception {
	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
	Object actual = resolver.resolveArgument(param, null, webRequest, binderFactory);

	assertEquals(Optional.empty(), actual);
}