本文整理汇总了Java中org.springframework.core.convert.ConversionService类的典型用法代码示例。如果您正苦于以下问题:Java ConversionService类的具体用法?Java ConversionService怎么用?Java ConversionService使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ConversionService类属于org.springframework.core.convert包,在下文中一共展示了ConversionService类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: HandlerMethodService
import org.springframework.core.convert.ConversionService; //导入依赖的package包/类
public HandlerMethodService(ConversionService conversionService,
List<HandlerMethodArgumentResolver> customArgumentResolvers,
ObjectMapper objectMapper, ApplicationContext applicationContext) {
this.conversionService = conversionService;
this.parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
this.argumentResolvers = new HandlerMethodArgumentResolverComposite();
ConfigurableBeanFactory beanFactory = ClassUtils.isAssignableValue(
ConfigurableApplicationContext.class, applicationContext)
? ((ConfigurableApplicationContext) applicationContext)
.getBeanFactory()
: null;
this.argumentResolvers.addResolver(
new HeaderMethodArgumentResolver(this.conversionService, beanFactory));
this.argumentResolvers.addResolver(new HeadersMethodArgumentResolver());
this.argumentResolvers.addResolver(new WampMessageMethodArgumentResolver());
this.argumentResolvers.addResolver(new PrincipalMethodArgumentResolver());
this.argumentResolvers.addResolver(new WampSessionIdMethodArgumentResolver());
this.argumentResolvers.addResolvers(customArgumentResolvers);
this.objectMapper = objectMapper;
}
示例2: getArguments
import org.springframework.core.convert.ConversionService; //导入依赖的package包/类
protected Object[] getArguments(Step step, StepImplementation implementation) {
Method method = implementation.getMethod();
Parameter[] parameters = method.getParameters();
Object[] arguments = new Object[parameters.length];
if (parameters.length > 0) {
String text = step.getText();
Pattern pattern = implementation.getPattern();
Matcher matcher = pattern.matcher(text);
checkState(matcher.find(),
"unable to locate substitution parameters for pattern %s with input %s", pattern.pattern(), text);
int groupCount = matcher.groupCount();
ConversionService conversionService = SpringPreProcessor.getBean(ConversionService.class);
for (int i = 0; i < groupCount; i++) {
String parameterAsString = matcher.group(i + 1);
Parameter parameter = parameters[i];
Class<?> parameterType = parameter.getType();
Object converted = conversionService.convert(parameterAsString, parameterType);
arguments[i] = converted;
}
}
return arguments;
}
示例3: contributeMethodArgument
import org.springframework.core.convert.ConversionService; //导入依赖的package包/类
@Override
public void contributeMethodArgument(MethodParameter parameter, Object value,
UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) {
Class<?> paramType = parameter.getParameterType();
if (Map.class.isAssignableFrom(paramType) || MultipartFile.class.equals(paramType) ||
"javax.servlet.http.Part".equals(paramType.getName())) {
return;
}
RequestParam annot = parameter.getParameterAnnotation(RequestParam.class);
String name = StringUtils.isEmpty(annot.value()) ? parameter.getParameterName() : annot.value();
if (value == null) {
builder.queryParam(name);
}
else if (value instanceof Collection) {
for (Object element : (Collection<?>) value) {
element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element);
builder.queryParam(name, element);
}
}
else {
builder.queryParam(name, formatUriValue(conversionService, new TypeDescriptor(parameter), value));
}
}
示例4: finishBeanFactoryInitialization
import org.springframework.core.convert.ConversionService; //导入依赖的package包/类
/**
* Finish the initialization of this context's bean factory,
* initializing all remaining singleton beans.
*/
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
// Initialize conversion service for this context.
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}
// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
}
// Stop using the temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(null);
// Allow for caching all bean definition metadata, not expecting further changes.
beanFactory.freezeConfiguration();
// Instantiate all remaining (non-lazy-init) singletons.
beanFactory.preInstantiateSingletons();
}
示例5: normaliseValueForPut
import org.springframework.core.convert.ConversionService; //导入依赖的package包/类
public static Object normaliseValueForPut(Object value, ConversionService conversionService) {
if (value instanceof Document) {
return conversionService.convert(value, TypeDescriptor.forObject(value), TypeDescriptor.valueOf(IData.class));
}
else if (value instanceof Document[]) {
return conversionService.convert(value, TypeDescriptor.forObject(value), TypeDescriptor.valueOf(IData[].class));
}
else if (value instanceof Iterable<?>) {
if (CollectionUtil.areAllElementsOfType((Collection<?>) value, Document.class)) {
return conversionService.convert(value, TypeDescriptor.forObject(value), TypeDescriptor.valueOf(IData[].class));
}
else {
return value;
}
}
else {
return value;
}
}
示例6: contributeMethodArgument
import org.springframework.core.convert.ConversionService; //导入依赖的package包/类
@Override
public void contributeMethodArgument(MethodParameter parameter, Object value, UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) {
Class<?> paramType = parameter.getNestedParameterType();
if (Map.class.isAssignableFrom(paramType)) {
return;
}
WxApiParam wxApiParam = parameter.getParameterAnnotation(WxApiParam.class);
String name = (wxApiParam == null || StringUtils.isEmpty(wxApiParam.name()) ? parameter.getParameterName() : wxApiParam.name());
WxAppAssert.notNull(name, "请添加编译器的-parameter或者为参数添加注解名称");
if (value == null) {
if (wxApiParam != null) {
if (!wxApiParam.required() || !wxApiParam.defaultValue().equals(ValueConstants.DEFAULT_NONE)) {
return;
}
}
builder.queryParam(name);
} else if (value instanceof Collection) {
for (Object element : (Collection<?>) value) {
element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element);
builder.queryParam(name, element);
}
} else {
builder.queryParam(name, formatUriValue(conversionService, new TypeDescriptor(parameter), value));
}
}
示例7: setEntryValue
import org.springframework.core.convert.ConversionService; //导入依赖的package包/类
@Override
protected void setEntryValue(Map nativeEntry, String key, Object value) {
if (value == null || !shouldConvert(value)) {
return;
}
Class type = value.getClass();
if(value != null && type.isArray() && byte.class.isAssignableFrom(type.getComponentType())) {
nativeEntry.put(key, SafeEncoder.encode((byte[])value));
}
else {
final ConversionService conversionService = getMappingContext().getConversionService();
nativeEntry.put(key, conversionService.convert(value, String.class));
}
}
示例8: addDefaultConverters
import org.springframework.core.convert.ConversionService; //导入依赖的package包/类
/**
* Add converters appropriate for most environments.
* @param converterRegistry the registry of converters to add to (must also be castable to ConversionService,
* e.g. being a {@link ConfigurableConversionService})
* @throws ClassCastException if the given ConverterRegistry could not be cast to a ConversionService
*/
public static void addDefaultConverters(ConverterRegistry converterRegistry) {
addScalarConverters(converterRegistry);
addCollectionConverters(converterRegistry);
converterRegistry.addConverter(new ByteBufferConverter((ConversionService) converterRegistry));
if (jsr310Available) {
Jsr310ConverterRegistrar.registerJsr310Converters(converterRegistry);
}
converterRegistry.addConverter(new ObjectToObjectConverter());
converterRegistry.addConverter(new IdToEntityConverter((ConversionService) converterRegistry));
converterRegistry.addConverter(new FallbackObjectToStringConverter());
if (javaUtilOptionalClassAvailable) {
converterRegistry.addConverter(new ObjectToOptionalConverter((ConversionService) converterRegistry));
}
}
示例9: canConvertElements
import org.springframework.core.convert.ConversionService; //导入依赖的package包/类
public static boolean canConvertElements(TypeDescriptor sourceElementType, TypeDescriptor targetElementType, ConversionService conversionService) {
if (targetElementType == null) {
// yes
return true;
}
if (sourceElementType == null) {
// maybe
return true;
}
if (conversionService.canConvert(sourceElementType, targetElementType)) {
// yes
return true;
}
else if (sourceElementType.getType().isAssignableFrom(targetElementType.getType())) {
// maybe;
return true;
}
else {
// no;
return false;
}
}
示例10: convertProperty
import org.springframework.core.convert.ConversionService; //导入依赖的package包/类
private static Object convertProperty(BeanWrapper parentBean, String property,
ConversionService conversionService, Map<String, Object> parentValue) {
TypeDescriptor source = parentBean.getPropertyTypeDescriptor(property);
Object propertyValue = parentBean.getPropertyValue(property);
int dotIndex = property.indexOf('.');
if (dotIndex > 0) {
String baseProperty = property.substring(0, dotIndex);
String childProperty = property.substring(dotIndex + 1);
Map<String, Object> childValue = getChildValue(parentValue, baseProperty, childProperty);
BeanWrapper childBean = new BeanWrapperImpl(parentBean.getPropertyValue(baseProperty));
Object convertedProperty =
convertProperty(childBean, childProperty, conversionService, childValue);
childValue.put(childProperty, convertedProperty);
return childValue;
} else {
String convertedPropertyValue =
(String) conversionService.convert(propertyValue, source, TYPE_STRING);
return convertedPropertyValue;
}
}
示例11: createAttributeFromRequestValue
import org.springframework.core.convert.ConversionService; //导入依赖的package包/类
protected Object createAttributeFromRequestValue(String sourceValue,
String attributeName,
MethodParameter parameter,
WebDataBinderFactory binderFactory,
NativeWebRequest request) throws Exception {
DataBinder binder = binderFactory.createBinder(request, null, attributeName);
ConversionService conversionService = binder.getConversionService();
if (conversionService != null) {
TypeDescriptor source = TypeDescriptor.valueOf(String.class);
TypeDescriptor target = new TypeDescriptor(parameter);
if (conversionService.canConvert(source, target)) {
return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
}
}
return null;
}
示例12: requestMappingHandlerAdapter
import org.springframework.core.convert.ConversionService; //导入依赖的package包/类
@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);
}
示例13: contributeMethodArgument
import org.springframework.core.convert.ConversionService; //导入依赖的package包/类
@Override
public void contributeMethodArgument(MethodParameter parameter, Object value,
UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) {
Class<?> paramType = parameter.getParameterType();
if (Map.class.isAssignableFrom(paramType) || MultipartFile.class == paramType ||
"javax.servlet.http.Part".equals(paramType.getName())) {
return;
}
RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class);
String name = (requestParam == null || StringUtils.isEmpty(requestParam.name()) ? parameter.getParameterName() : requestParam.name());
if (value == null) {
builder.queryParam(name);
}
else if (value instanceof Collection) {
for (Object element : (Collection<?>) value) {
element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element);
builder.queryParam(name, element);
}
}
else {
builder.queryParam(name, formatUriValue(conversionService, new TypeDescriptor(parameter), value));
}
}
示例14: createAttributeFromRequestValue
import org.springframework.core.convert.ConversionService; //导入依赖的package包/类
/**
* Create a model attribute from a String request value (e.g. URI template
* variable, request parameter) using type conversion.
* <p>The default implementation converts only if there a registered
* {@link Converter} that can perform the conversion.
* @param sourceValue the source value to create the model attribute from
* @param attributeName the name of the attribute, never {@code null}
* @param parameter the method parameter
* @param binderFactory for creating WebDataBinder instance
* @param request the current request
* @return the created model attribute, or {@code null}
* @throws Exception
*/
protected Object createAttributeFromRequestValue(String sourceValue,
String attributeName,
MethodParameter parameter,
WebDataBinderFactory binderFactory,
NativeWebRequest request) throws Exception {
DataBinder binder = binderFactory.createBinder(request, null, attributeName);
ConversionService conversionService = binder.getConversionService();
if (conversionService != null) {
TypeDescriptor source = TypeDescriptor.valueOf(String.class);
TypeDescriptor target = new TypeDescriptor(parameter);
if (conversionService.canConvert(source, target)) {
return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
}
}
return null;
}
示例15: createAttributeFromRequestValue
import org.springframework.core.convert.ConversionService; //导入依赖的package包/类
/**
* Create a model attribute from a String request value (e.g. URI template
* variable, request parameter) using type conversion.
* <p>The default implementation converts only if there a registered
* {@link org.springframework.core.convert.converter.Converter} that can perform the conversion.
*
* @param sourceValue the source value to create the model attribute from
* @param attributeName the name of the attribute, never {@code null}
* @param parameter the method parameter
* @param binderFactory for creating WebDataBinder instance
* @param request the current request
* @return the created model attribute, or {@code null}
* @throws Exception
*/
protected Object createAttributeFromRequestValue(String sourceValue,
String attributeName,
MethodParameter parameter,
WebDataBinderFactory binderFactory,
NativeWebRequest request) throws Exception {
DataBinder binder = binderFactory.createBinder(request, null, attributeName);
ConversionService conversionService = binder.getConversionService();
if (conversionService != null) {
TypeDescriptor source = TypeDescriptor.valueOf(String.class);
TypeDescriptor target = new TypeDescriptor(parameter);
if (conversionService.canConvert(source, target)) {
return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
}
}
return null;
}