本文整理汇总了Java中org.springframework.core.convert.support.GenericConversionService类的典型用法代码示例。如果您正苦于以下问题:Java GenericConversionService类的具体用法?Java GenericConversionService怎么用?Java GenericConversionService使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
GenericConversionService类属于org.springframework.core.convert.support包,在下文中一共展示了GenericConversionService类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: safeConversionService
import org.springframework.core.convert.support.GenericConversionService; //导入依赖的package包/类
private static GenericConversionService safeConversionService() {
final GenericConversionService converter = new GenericConversionService();
converter.addConverter(Object.class, byte[].class, new SerializingConverter());
final DeserializingConverter byteConverter = new DeserializingConverter();
converter.addConverter(byte[].class, Object.class, (byte[] bytes) -> {
try {
return byteConverter.convert(bytes);
} catch (SerializationFailedException e) {
LOG.error("Could not extract attribute: {}", e.getMessage());
return null;
}
});
return converter;
}
示例2: addDateConverters
import org.springframework.core.convert.support.GenericConversionService; //导入依赖的package包/类
private static void addDateConverters(GenericConversionService conversionService) {
conversionService.addConverter(Date.class, String.class, new Converter<Date, String>() {
@Override
public String convert(Date dateVal) {
return ValueConversionUtil.dateToIsoString(dateVal);
}
});
conversionService.addConverter(String.class, Date.class, blankConverter(new Converter<String, Date>() {
@Override
public Date convert(String stringVal) {
return ValueConversionUtil.isoStringToDate(stringVal);
}
}));
}
示例3: customConversion
import org.springframework.core.convert.support.GenericConversionService; //导入依赖的package包/类
@Test
public void customConversion() throws Exception {
DefaultMessageHandlerMethodFactory instance = createInstance();
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(SampleBean.class, String.class, new Converter<SampleBean, String>() {
@Override
public String convert(SampleBean source) {
return "foo bar";
}
});
instance.setConversionService(conversionService);
instance.afterPropertiesSet();
InvocableHandlerMethod invocableHandlerMethod =
createInvocableHandlerMethod(instance, "simpleString", String.class);
invocableHandlerMethod.invoke(MessageBuilder.withPayload(sample).build());
assertMethodInvocation(sample, "simpleString");
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:20,代码来源:DefaultMessageHandlerMethodFactoryTests.java
示例4: test
import org.springframework.core.convert.support.GenericConversionService; //导入依赖的package包/类
@Test
@Deprecated
public void test() throws Exception {
AnnotationMethodHandlerAdapter adapter = new AnnotationMethodHandlerAdapter();
ConfigurableWebBindingInitializer binder = new ConfigurableWebBindingInitializer();
GenericConversionService service = new DefaultConversionService();
service.addConverter(new ColorConverter());
binder.setConversionService(service);
adapter.setWebBindingInitializer(binder);
Spr7766Controller controller = new Spr7766Controller();
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/colors");
request.setPathInfo("/colors");
request.addParameter("colors", "#ffffff,000000");
MockHttpServletResponse response = new MockHttpServletResponse();
adapter.handle(request, response, controller);
}
示例5: testConvertAndHandleNull
import org.springframework.core.convert.support.GenericConversionService; //导入依赖的package包/类
@Test
public void testConvertAndHandleNull() { // SPR-9445
// without null conversion
evaluateAndCheckError("null or true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
evaluateAndCheckError("null and true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
evaluateAndCheckError("!null", SpelMessage.TYPE_CONVERSION_ERROR, 1, "null", "boolean");
evaluateAndCheckError("null ? 'foo' : 'bar'", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
// with null conversion (null -> false)
GenericConversionService conversionService = new GenericConversionService() {
@Override
protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) {
return targetType.getType() == Boolean.class ? false : null;
}
};
eContext.setTypeConverter(new StandardTypeConverter(conversionService));
evaluate("null or true", Boolean.TRUE, Boolean.class, false);
evaluate("null and true", Boolean.FALSE, Boolean.class, false);
evaluate("!null", Boolean.TRUE, Boolean.class, false);
evaluate("null ? 'foo' : 'bar'", "bar", String.class, false);
}
示例6: testCustomConverter
import org.springframework.core.convert.support.GenericConversionService; //导入依赖的package包/类
@Test
public void testCustomConverter() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
GenericConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new Converter<String, Float>() {
@Override
public Float convert(String source) {
try {
NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
return nf.parse(source).floatValue();
}
catch (ParseException ex) {
throw new IllegalArgumentException(ex);
}
}
});
lbf.setConversionService(conversionService);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("myFloat", "1,1");
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("testBean", bd);
TestBean testBean = (TestBean) lbf.getBean("testBean");
assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
示例7: noDelegateMapConversion
import org.springframework.core.convert.support.GenericConversionService; //导入依赖的package包/类
@Test
public void noDelegateMapConversion() {
GenericConversionService conversionService = new GenericConversionService();
GenericMapConverter mapConverter = new GenericMapConverter(conversionService);
conversionService.addConverter(mapConverter);
@SuppressWarnings("unchecked")
Map<String, String> result = conversionService.convert("foo = bar, wizz = jogg", Map.class);
assertThat(result, hasEntry("foo", "bar"));
assertThat(result, hasEntry("wizz", "jogg"));
assertThat(
conversionService.canConvert(TypeDescriptor.valueOf(String.class), TypeDescriptor.map(Map.class,
TypeDescriptor.valueOf(GenericMapConverterTests.class), TypeDescriptor.valueOf(Integer.class))),
is(false));
}
示例8: registerConvertersIn
import org.springframework.core.convert.support.GenericConversionService; //导入依赖的package包/类
/**
* Register custom converters within given {@link org.springframework.core.convert.support.GenericConversionService}
*
* @param conversionService must not be null
*/
public void registerConvertersIn(GenericConversionService conversionService) {
Assert.notNull(conversionService);
for (Object converter : converters) {
if (converter instanceof Converter) {
conversionService.addConverter((Converter<?, ?>) converter);
} else if (converter instanceof ConverterFactory) {
conversionService.addConverterFactory((ConverterFactory<?, ?>) converter);
} else if (converter instanceof GenericConverter) {
conversionService.addConverter((GenericConverter) converter);
} else {
throw new IllegalArgumentException("Given object '" + converter
+ "' expected to be a Converter, ConverterFactory or GenericeConverter!");
}
}
}
示例9: setup
import org.springframework.core.convert.support.GenericConversionService; //导入依赖的package包/类
@SuppressWarnings("resource")
@Before
public void setup() throws Exception {
DefaultListableBeanFactory dlbf = new DefaultListableBeanFactory();
dlbf.registerSingleton("mvcValidator", testValidator());
GenericApplicationContext ctx = new GenericApplicationContext(dlbf);
ctx.refresh();
this.resolver = new PayloadArgumentResolver(ctx, new MethodParameterConverter(
new ObjectMapper(), new GenericConversionService()));
this.payloadMethod = getClass().getDeclaredMethod("handleMessage", String.class,
String.class, String.class, String.class, String.class, String.class,
String.class, Integer.class);
this.paramAnnotated = getMethodParameter(this.payloadMethod, 0);
this.paramAnnotatedNotRequired = getMethodParameter(this.payloadMethod, 1);
this.paramAnnotatedRequired = getMethodParameter(this.payloadMethod, 2);
this.paramWithSpelExpression = getMethodParameter(this.payloadMethod, 3);
this.paramValidated = getMethodParameter(this.payloadMethod, 4);
this.paramValidated.initParameterNameDiscovery(
new LocalVariableTableParameterNameDiscoverer());
this.paramValidatedNotAnnotated = getMethodParameter(this.payloadMethod, 5);
this.paramNotAnnotated = getMethodParameter(this.payloadMethod, 6);
}
示例10: registerConvertersIn
import org.springframework.core.convert.support.GenericConversionService; //导入依赖的package包/类
/**
* Populates the given {@link GenericConversionService} with the converters registered.
*
* @param conversionService the service to register.
*/
public void registerConvertersIn(final GenericConversionService conversionService) {
for (Object converter : converters) {
boolean added = false;
if (converter instanceof Converter) {
conversionService.addConverter((Converter<?, ?>) converter);
added = true;
}
if (converter instanceof ConverterFactory) {
conversionService.addConverterFactory((ConverterFactory<?, ?>) converter);
added = true;
}
if (converter instanceof GenericConverter) {
conversionService.addConverter((GenericConverter) converter);
added = true;
}
if (!added) {
throw new IllegalArgumentException("Given set contains element that is neither Converter nor ConverterFactory!");
}
}
}
示例11: registerConvertersIn
import org.springframework.core.convert.support.GenericConversionService; //导入依赖的package包/类
/**
* Register custom converters within given {@link GenericConversionService}
*
* @param conversionService must not be null
*/
public void registerConvertersIn(GenericConversionService conversionService) {
Assert.notNull(conversionService);
for (Object converter : converters) {
if (converter instanceof Converter) {
conversionService.addConverter((Converter<?, ?>) converter);
} else if (converter instanceof ConverterFactory) {
conversionService.addConverterFactory((ConverterFactory<?, ?>) converter);
} else if (converter instanceof GenericConverter) {
conversionService.addConverter((GenericConverter) converter);
} else {
throw new IllegalArgumentException("Given object '" + converter
+ "' expected to be a Converter, ConverterFactory or GenericeConverter!");
}
}
}
示例12: setupProcessor
import org.springframework.core.convert.support.GenericConversionService; //导入依赖的package包/类
/**
* Sets up the bean post processor and conversion service
*
* @param beans - The bean factory for the the dictionary beans
*/
public static void setupProcessor(DefaultListableBeanFactory beans) {
try {
// UIF post processor that sets component ids
BeanPostProcessor idPostProcessor = ComponentBeanPostProcessor.class.newInstance();
beans.addBeanPostProcessor(idPostProcessor);
beans.setBeanExpressionResolver(new StandardBeanExpressionResolver() {
@Override
protected void customizeEvaluationContext(StandardEvaluationContext evalContext) {
try {
evalContext.registerFunction("getService", ExpressionFunctions.class.getDeclaredMethod("getService", new Class[]{String.class}));
} catch(NoSuchMethodException me) {
LOG.error("Unable to register custom expression to data dictionary bean factory", me);
}
}
});
// special converters for shorthand map and list property syntax
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(new StringMapConverter());
conversionService.addConverter(new StringListConverter());
beans.setConversionService(conversionService);
} catch (Exception e1) {
throw new DataDictionaryException("Cannot create component decorator post processor: " + e1.getMessage(),
e1);
}
}
示例13: setupProcessor
import org.springframework.core.convert.support.GenericConversionService; //导入依赖的package包/类
/**
* Sets up the bean post processor and conversion service
*
* @param beans - The bean factory for the the dictionary beans
*/
public static void setupProcessor(KualiDefaultListableBeanFactory beans) {
try {
// UIF post processor that sets component ids
BeanPostProcessor idPostProcessor = ComponentBeanPostProcessor.class.newInstance();
beans.addBeanPostProcessor(idPostProcessor);
beans.setBeanExpressionResolver(new StandardBeanExpressionResolver());
// special converters for shorthand map and list property syntax
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(new StringMapConverter());
conversionService.addConverter(new StringListConverter());
beans.setConversionService(conversionService);
} catch (Exception e1) {
throw new DataDictionaryException("Cannot create component decorator post processor: " + e1.getMessage(),
e1);
}
}
示例14: getConversionService
import org.springframework.core.convert.support.GenericConversionService; //导入依赖的package包/类
protected GenericConversionService getConversionService(Workbook workbook,
Map<String, Map<String, GenericEntity<Long, ?>>> idsMapping) {
GenericConversionService service = new GenericConversionService();
GenericEntityConverter genericEntityConverter = new GenericEntityConverter(importDataDao, workbook,
new HashMap<Class<?>, Class<?>>(0), idsMapping);
genericEntityConverter.setConversionService(service);
service.addConverter(genericEntityConverter);
service.addConverter(new ProjectConverter());
service.addConverter(new ArtifactConverter());
service.addConverter(new ExternalLinkWrapperConverter());
DefaultConversionService.addDefaultConverters(service);
return service;
}
示例15: setUp
import org.springframework.core.convert.support.GenericConversionService; //导入依赖的package包/类
@Before
public void setUp() throws NoSuchFieldException {
GenericConversionService genericConversionService = new GenericConversionService();
genericConversionService.addConverter(new Converter<String, Integer>() {
@Override
public Integer convert(String source) {
if (0 == source.length()) {
return null;
}
return NumberUtils.parseNumber(source, Integer.class);
}
});
Mockito.when(conversionServiceFactoryBean.getObject()).thenReturn(genericConversionService);
mockCartEntry();
mockProduct();
}