org.springframework.format.support.DefaultFormattingConversionService Java Examples

The following examples show how to use org.springframework.format.support.DefaultFormattingConversionService. 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: 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 #2
Source File: SessionAttributeMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void resolveOptional() {
	MethodParameter param = initMethodParameter(3);
	Optional<Object> actual = (Optional<Object>) this.resolver
			.resolveArgument(param, new BindingContext(), this.exchange).block();

	assertNotNull(actual);
	assertFalse(actual.isPresent());

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

	Foo foo = new Foo();
	given(this.session.getAttribute("foo")).willReturn(foo);
	actual = (Optional<Object>) this.resolver.resolveArgument(param, bindingContext, this.exchange).block();

	assertNotNull(actual);
	assertTrue(actual.isPresent());
	assertSame(foo, actual.get());
}
 
Example #3
Source File: SessionAttributeMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void resolveOptional() {
	MethodParameter param = initMethodParameter(3);
	Optional<Object> actual = (Optional<Object>) this.resolver
			.resolveArgument(param, new BindingContext(), this.exchange).block();

	assertNotNull(actual);
	assertFalse(actual.isPresent());

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

	Foo foo = new Foo();
	when(this.session.getAttribute("foo")).thenReturn(foo);
	actual = (Optional<Object>) this.resolver.resolveArgument(param, bindingContext, this.exchange).block();

	assertNotNull(actual);
	assertTrue(actual.isPresent());
	assertSame(foo, actual.get());
}
 
Example #4
Source File: RequestHeaderMethodArgumentResolverTests.java    From spring-analysis-note 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 #5
Source File: OpenFeignAutoConfiguration.java    From summerframework with Apache License 2.0 6 votes vote down vote up
@Bean
public OpenFeignSpringMvcContract feignSpringMvcContract(
    @Autowired(required = false) List<AnnotatedParameterProcessor> parameterProcessors,
    List<ConversionService> conversionServices) {
    if (conversionServices.size() == 0) {
        throw new IllegalStateException("ConversionService can not be NULL");
    }
    ConversionService conversionService = null;
    if (conversionServices.size() == 1) {
        conversionService = conversionServices.get(0);
    } else {
        // 如果有多个实例,优先使用找到的第一个DefaultFormattingConversionService,如果没有,则使用FormattingConversionService
        conversionService = conversionServices.stream().filter(c -> c instanceof DefaultFormattingConversionService)
            .findFirst().orElseGet(() -> conversionServices.stream()
                .filter(c -> c instanceof FormattingConversionService).findFirst().get());
    }
    if (null == parameterProcessors) {
        parameterProcessors = new ArrayList<>();
    }
    return new OpenFeignSpringMvcContract(parameterProcessors, conversionService);
}
 
Example #6
Source File: RedirectAttributesModelMapTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
	this.conversionService = new DefaultFormattingConversionService();
	DataBinder dataBinder = new DataBinder(null);
	dataBinder.setConversionService(conversionService);

	this.redirectAttributes = new RedirectAttributesModelMap(dataBinder);
}
 
Example #7
Source File: TestUtil.java    From tutorials with MIT License 5 votes vote down vote up
/**
 * Create a FormattingConversionService which use ISO date format, instead of the localized one.
 * @return the FormattingConversionService
 */
public static FormattingConversionService createFormattingConversionService() {
    DefaultFormattingConversionService dfcs = new DefaultFormattingConversionService ();
    DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
    registrar.setUseIsoFormat(true);
    registrar.registerFormatters(dfcs);
    return dfcs;
}
 
Example #8
Source File: DataBinderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testConversionWithInappropriateStringEditor() {
	DataBinder dataBinder = new DataBinder(null);
	DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
	dataBinder.setConversionService(conversionService);
	dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));

	NameBean bean = new NameBean("Fred");
	assertEquals("ConversionService should have invoked toString()", "Fred", dataBinder.convertIfNecessary(bean, String.class));
	conversionService.addConverter(new NameBeanConverter());
	assertEquals("Type converter should have been used", "[Fred]", dataBinder.convertIfNecessary(bean, String.class));
}
 
Example #9
Source File: TestUtil.java    From ehcache3-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Create a FormattingConversionService which use ISO date format, instead of the localized one.
 * @return the FormattingConversionService
 */
public static FormattingConversionService createFormattingConversionService() {
    DefaultFormattingConversionService dfcs = new DefaultFormattingConversionService ();
    DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
    registrar.setUseIsoFormat(true);
    registrar.registerFormatters(dfcs);
    return dfcs;
}
 
Example #10
Source File: ConvertControllerTests.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Before
public void setup() throws Exception {
	FormattingConversionService cs = new DefaultFormattingConversionService();
	cs.addFormatterForFieldAnnotation(new MaskFormatAnnotationFormatterFactory());

	this.mockMvc = standaloneSetup(new ConvertController()).setConversionService(cs).alwaysExpect(status().isOk())
		.build();
}
 
Example #11
Source File: RestControllerTestSupport.java    From fullstop with Apache License 2.0 5 votes vote down vote up
protected void configure(final StandaloneMockMvcBuilder mockMvcBuilder) {
    mockMvcBuilder.setCustomArgumentResolvers(mockMvcCustomArgumentResolvers());
    mockMvcBuilder.setMessageConverters(mockMvcMessageConverters());

    final DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
    mockMvcBuilder.setConversionService(conversionService);
}
 
Example #12
Source File: InitBinderDataBinderFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void createBinderWithGlobalInitialization() throws Exception {
	ConversionService conversionService = new DefaultFormattingConversionService();
	bindingInitializer.setConversionService(conversionService);

	WebDataBinderFactory factory = createBinderFactory("initBinder", WebDataBinder.class);
	WebDataBinder dataBinder = factory.createBinder(webRequest, null, null);

	assertSame(conversionService, dataBinder.getConversionService());
}
 
Example #13
Source File: TestUtil.java    From 21-points with Apache License 2.0 5 votes vote down vote up
/**
 * Create a FormattingConversionService which use ISO date format, instead of the localized one.
 * @return the FormattingConversionService
 */
public static FormattingConversionService createFormattingConversionService() {
    DefaultFormattingConversionService dfcs = new DefaultFormattingConversionService ();
    DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
    registrar.setUseIsoFormat(true);
    registrar.registerFormatters(dfcs);
    return dfcs;
}
 
Example #14
Source File: TestUtil.java    From TeamDojo with Apache License 2.0 5 votes vote down vote up
/**
 * Create a FormattingConversionService which use ISO date format, instead of the localized one.
 *
 * @return the FormattingConversionService
 */
public static FormattingConversionService createFormattingConversionService() {
    DefaultFormattingConversionService dfcs = new DefaultFormattingConversionService();
    DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
    registrar.setUseIsoFormat(true);
    registrar.registerFormatters(dfcs);
    return dfcs;
}
 
Example #15
Source File: WebMvcConfigurationSupport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Return a {@link FormattingConversionService} for use with annotated
 * controller methods and the {@code spring:eval} JSP tag.
 * Also see {@link #addFormatters} as an alternative to overriding this method.
 */
@Bean
public FormattingConversionService mvcConversionService() {
	FormattingConversionService conversionService = new DefaultFormattingConversionService();
	addFormatters(conversionService);
	return conversionService;
}
 
Example #16
Source File: WebFluxConfigurationSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #17
Source File: TestUtil.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Create a {@link FormattingConversionService} which use ISO date format, instead of the localized one.
 *
 * @return the {@link FormattingConversionService}.
 */
public static FormattingConversionService createFormattingConversionService() {
	DefaultFormattingConversionService dfcs = new DefaultFormattingConversionService();
	DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
	registrar.setUseIsoFormat(true);
	registrar.registerFormatters(dfcs);
	return dfcs;
}
 
Example #18
Source File: TestUtil.java    From jhipster-online with Apache License 2.0 5 votes vote down vote up
/**
 * Create a {@link FormattingConversionService} which use ISO date format, instead of the localized one.
 * @return the {@link FormattingConversionService}.
 */
public static FormattingConversionService createFormattingConversionService() {
    DefaultFormattingConversionService dfcs = new DefaultFormattingConversionService ();
    DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
    registrar.setUseIsoFormat(true);
    registrar.registerFormatters(dfcs);
    return dfcs;
}
 
Example #19
Source File: SearchableTest.java    From es with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    try {
        oldConversionService = SearchableConvertUtils.getConversionService();
    } catch (Exception e) {
        //ignore null case
    }
    SearchableConvertUtils.setConversionService(new DefaultFormattingConversionService());
}
 
Example #20
Source File: TestUtil.java    From Spring-5.0-Projects with MIT License 5 votes vote down vote up
/**
 * Create a FormattingConversionService which use ISO date format, instead of the localized one.
 * @return the FormattingConversionService
 */
public static FormattingConversionService createFormattingConversionService() {
    DefaultFormattingConversionService dfcs = new DefaultFormattingConversionService ();
    DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
    registrar.setUseIsoFormat(true);
    registrar.registerFormatters(dfcs);
    return dfcs;
}
 
Example #21
Source File: ConversionServiceResolverTest.java    From spring-context-support with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateDefaultConversionService() {
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    ConversionServiceResolver resolver = new ConversionServiceResolver(beanFactory);
    ConversionService conversionService = resolver.resolve(false);
    assertTrue(isAssignable(DefaultFormattingConversionService.class, conversionService.getClass()));

    conversionService = resolver.resolve(true);
    assertTrue(isAssignable(DefaultFormattingConversionService.class, conversionService.getClass()));

    conversionService = resolver.resolve();
    assertTrue(isAssignable(DefaultFormattingConversionService.class, conversionService.getClass()));
}
 
Example #22
Source File: RestControllerTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@BeforeAll
static void beforeClass() {
  DataConverter.setConversionService(new DefaultFormattingConversionService());

  ResourceBundleMessageSource validationMessages = new ResourceBundleMessageSource();
  validationMessages.addBasenames("org.hibernate.validator.ValidationMessages");
  TestAllPropertiesMessageSource messageSource =
      new TestAllPropertiesMessageSource(new MessageFormatFactory());
  messageSource.addMolgenisNamespaces("data", "web");
  messageSource.setParentMessageSource(validationMessages);
  MessageSourceHolder.setMessageSource(messageSource);
}
 
Example #23
Source File: TestUtil.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 5 votes vote down vote up
/**
 * Create a FormattingConversionService which use ISO date format, instead of the localized one.
 * @return the FormattingConversionService
 */
public static FormattingConversionService createFormattingConversionService() {
    DefaultFormattingConversionService dfcs = new DefaultFormattingConversionService ();
    DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
    registrar.setUseIsoFormat(true);
    registrar.registerFormatters(dfcs);
    return dfcs;
}
 
Example #24
Source File: TestUtil.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 5 votes vote down vote up
/**
 * Create a FormattingConversionService which use ISO date format, instead of the localized one.
 * @return the FormattingConversionService
 */
public static FormattingConversionService createFormattingConversionService() {
    DefaultFormattingConversionService dfcs = new DefaultFormattingConversionService ();
    DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
    registrar.setUseIsoFormat(true);
    registrar.registerFormatters(dfcs);
    return dfcs;
}
 
Example #25
Source File: InitBinderDataBinderFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void createBinderWithGlobalInitialization() throws Exception {
	ConversionService conversionService = new DefaultFormattingConversionService();
	bindingInitializer.setConversionService(conversionService);

	WebDataBinderFactory factory = createFactory("initBinder", WebDataBinder.class);
	WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, null);

	assertSame(conversionService, dataBinder.getConversionService());
}
 
Example #26
Source File: RequestHeaderMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void instantConversion() 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(paramInstant, null, webRequest,
			new DefaultDataBinderFactory(bindingInitializer));

	assertTrue(result instanceof Instant);
	assertEquals(Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(rfc1123val)), result);
}
 
Example #27
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 #28
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@InitBinder
public void initBinder(WebDataBinder binder) {
	binder.initDirectFieldAccess();
	binder.setConversionService(new DefaultFormattingConversionService());
	LocalValidatorFactoryBean vf = new LocalValidatorFactoryBean();
	vf.afterPropertiesSet();
	binder.setValidator(vf);
}
 
Example #29
Source File: TestUtil.java    From jhipster-registry with Apache License 2.0 5 votes vote down vote up
/**
 * Create a {@link FormattingConversionService} which use ISO date format, instead of the localized one.
 * @return the {@link FormattingConversionService}.
 */
public static FormattingConversionService createFormattingConversionService() {
    DefaultFormattingConversionService dfcs = new DefaultFormattingConversionService ();
    DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
    registrar.setUseIsoFormat(true);
    registrar.registerFormatters(dfcs);
    return dfcs;
}
 
Example #30
Source File: WebMvcConfigurationSupport.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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;
}