org.springframework.format.support.FormattingConversionServiceFactoryBean Java Examples

The following examples show how to use org.springframework.format.support.FormattingConversionServiceFactoryBean. 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: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-12903
public void pathVariableWithCustomConverter() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
		csDef.getPropertyValues().add("converters", new AnnotatedExceptionRaisingConverter());
		RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
		wbiDef.getPropertyValues().add("conversionService", csDef);
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, PathVariableWithCustomConverterController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath/1");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(404, response.getStatus());
}
 
Example #2
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void parameterCsvAsStringArray() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
		RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
		wbiDef.getPropertyValues().add("conversionService", csDef);
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, CsvController.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/integerArray");
	request.setMethod("POST");
	request.addParameter("content", "1,2");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("1-2", response.getContentAsString());
}
 
Example #3
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void typeNestedSetBinding() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
		csDef.getPropertyValues().add("converters", new TestBeanConverter());
		RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
		wbiDef.getPropertyValues().add("conversionService", csDef);
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, NestedSetController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
	request.addParameter("testBeanSet", "1", "2");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("[1, 2]-org.springframework.tests.sample.beans.TestBean", response.getContentAsString());
}
 
Example #4
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void typeNestedSetBinding() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
		csDef.getPropertyValues().add("converters", new TestBeanConverter());
		RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
		wbiDef.getPropertyValues().add("conversionService", csDef);
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, NestedSetController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
	request.addParameter("testBeanSet", "1", "2");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("[1, 2]-org.springframework.tests.sample.beans.TestBean", response.getContentAsString());
}
 
Example #5
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-12903
public void pathVariableWithCustomConverter() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
		csDef.getPropertyValues().add("converters", new AnnotatedExceptionRaisingConverter());
		RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
		wbiDef.getPropertyValues().add("conversionService", csDef);
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, PathVariableWithCustomConverterController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath/1");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(404, response.getStatus());
}
 
Example #6
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void parameterCsvAsStringArray() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
		RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
		wbiDef.getPropertyValues().add("conversionService", csDef);
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, CsvController.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/integerArray");
	request.setMethod("POST");
	request.addParameter("content", "1,2");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("1-2", response.getContentAsString());
}
 
Example #7
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void parameterCsvAsStringArray() throws Exception {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
			RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
			wbiDef.getPropertyValues().add("conversionService", csDef);
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, CsvController.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/integerArray");
	request.setMethod("POST");
	request.addParameter("content", "1,2");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("1-2", response.getContentAsString());
}
 
Example #8
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void pathVariableWithCustomConverter() throws Exception {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext context) {
			RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
			csDef.getPropertyValues().add("converters", new AnnotatedExceptionRaisingConverter());
			RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
			wbiDef.getPropertyValues().add("conversionService", csDef);
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
			context.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, PathVariableWithCustomConverterController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath/1");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(404, response.getStatus());
}
 
Example #9
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void typeNestedSetBinding() throws Exception {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext context) {
			RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
			csDef.getPropertyValues().add("converters", new TestBeanConverter());
			RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
			wbiDef.getPropertyValues().add("conversionService", csDef);
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
			context.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, NestedSetController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
	request.addParameter("testBeanSet", new String[] {"1", "2"});
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("[1, 2]-org.springframework.tests.sample.beans.TestBean", response.getContentAsString());
}
 
Example #10
Source File: RestControllerV2Test.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
FormattingConversionService conversionService() {
  FormattingConversionServiceFactoryBean conversionServiceFactoryBean =
      new FormattingConversionServiceFactoryBean();
  conversionServiceFactoryBean.setConverters(
      Collections.singleton(new AttributeFilterConverter()));
  conversionServiceFactoryBean.afterPropertiesSet();
  return conversionServiceFactoryBean.getObject();
}
 
Example #11
Source File: DateTimeConverter.java    From DCMonitor with MIT License 5 votes vote down vote up
@Bean
FormattingConversionServiceFactoryBean conversionService() {
  FormattingConversionServiceFactoryBean bean = new FormattingConversionServiceFactoryBean();
  Set<FormatterRegistrar> set = new HashSet<FormatterRegistrar>();
  JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
  registrar.setUseIsoFormat(true);
  set.add(registrar);
  bean.setFormatterRegistrars(set);
  return bean;
}
 
Example #12
Source File: EvalTagTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	context = createPageContext();
	FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
	factory.afterPropertiesSet();
	context.getRequest().setAttribute("org.springframework.core.convert.ConversionService", factory.getObject());
	context.getRequest().setAttribute("bean", new Bean());
	tag = new EvalTag();
	tag.setPageContext(context);
}
 
Example #13
Source File: ServletAnnotationControllerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("serial")
public void parameterCsvAsIntegerSetWithCustomSeparator() throws Exception {
	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(CsvController.class));
			RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
			RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
			wbiDef.getPropertyValues().add("conversionService", csDef);
			wbiDef.getPropertyValues().add("propertyEditorRegistrars", new RootBeanDefinition(ListEditorRegistrar.class));
			RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
			adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
			wac.refresh();
			return wac;
		}
	};
	servlet.init(new MockServletConfig());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/integerSet");
	request.setMethod("POST");
	request.addParameter("content", "1;2");
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("1-2", response.getContentAsString());
}
 
Example #14
Source File: ServletAnnotationControllerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("serial")
public void parameterCsvAsIntegerSet() throws Exception {
	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(CsvController.class));
			RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
			RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
			wbiDef.getPropertyValues().add("conversionService", csDef);
			RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
			adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
			wac.refresh();
			return wac;
		}
	};
	servlet.init(new MockServletConfig());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/integerSet");
	request.setMethod("POST");
	request.addParameter("content", "1,2");
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("1-2", response.getContentAsString());
}
 
Example #15
Source File: ServletAnnotationControllerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("serial")
public void parameterCsvAsIntegerArray() throws Exception {
	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(CsvController.class));
			RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
			RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
			wbiDef.getPropertyValues().add("conversionService", csDef);
			RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
			adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
			wac.refresh();
			return wac;
		}
	};
	servlet.init(new MockServletConfig());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/integerArray");
	request.setMethod("POST");
	request.addParameter("content", "1,2");
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("1-2", response.getContentAsString());
}
 
Example #16
Source File: ServletAnnotationControllerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("serial")
public void typeNestedSetBinding() throws Exception {
	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(NestedSetController.class));
			RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
			csDef.getPropertyValues().add("converters", new TestBeanConverter());
			RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
			wbiDef.getPropertyValues().add("conversionService", csDef);
			RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
			adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
			wac.refresh();
			return wac;
		}
	};
	servlet.init(new MockServletConfig());

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
	request.addParameter("testBeanSet", new String[] {"1", "2"});
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("[1, 2]-org.springframework.tests.sample.beans.TestBean", response.getContentAsString());
}
 
Example #17
Source File: SpringMvcContractTests.java    From spring-cloud-openfeign with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
	FormattingConversionServiceFactoryBean conversionServiceFactoryBean = new FormattingConversionServiceFactoryBean();
	conversionServiceFactoryBean.afterPropertiesSet();
	ConversionService conversionService = conversionServiceFactoryBean.getObject();

	this.contract = new SpringMvcContract(Collections.emptyList(), conversionService);
}
 
Example #18
Source File: EvalTagTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	context = createPageContext();
	FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
	factory.afterPropertiesSet();
	context.getRequest().setAttribute("org.springframework.core.convert.ConversionService", factory.getObject());
	context.getRequest().setAttribute("bean", new Bean());
	tag = new EvalTag();
	tag.setPageContext(context);
}
 
Example #19
Source File: EvalTagTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	context = createPageContext();
	FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
	factory.afterPropertiesSet();
	context.getRequest().setAttribute("org.springframework.core.convert.ConversionService", factory.getObject());
	context.getRequest().setAttribute("bean", new Bean());
	tag = new EvalTag();
	tag.setPageContext(context);
}
 
Example #20
Source File: CustomConversionServiceConfiguration.java    From GreenSummer with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Bean
public FormattingConversionServiceFactoryBean applicationConversionService() {
    final FormattingConversionServiceFactoryBean formattingConversionServiceFactoryBean = new FormattingConversionServiceFactoryBean();
    formattingConversionServiceFactoryBean.setFormatterRegistrars(new HashSet<>(Arrays.asList(autoregisterFormatterRegistrar)));
    return formattingConversionServiceFactoryBean;
}