本文整理汇总了Java中org.springframework.core.convert.ConversionFailedException类的典型用法代码示例。如果您正苦于以下问题:Java ConversionFailedException类的具体用法?Java ConversionFailedException怎么用?Java ConversionFailedException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ConversionFailedException类属于org.springframework.core.convert包,在下文中一共展示了ConversionFailedException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testDefaultFormattersOff
import org.springframework.core.convert.ConversionFailedException; //导入依赖的package包/类
@Test
public void testDefaultFormattersOff() throws Exception {
FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
factory.setRegisterDefaultFormatters(false);
factory.afterPropertiesSet();
FormattingConversionService fcs = factory.getObject();
TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("pattern"));
try {
fcs.convert("15,00", TypeDescriptor.valueOf(String.class), descriptor);
fail("This format should not be parseable");
}
catch (ConversionFailedException ex) {
assertTrue(ex.getCause() instanceof NumberFormatException);
}
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:17,代码来源:FormattingConversionServiceFactoryBeanTests.java
示例2: scalarMap
import org.springframework.core.convert.ConversionFailedException; //导入依赖的package包/类
@Test
public void scalarMap() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "9");
map.put("2", "37");
TypeDescriptor sourceType = TypeDescriptor.forObject(map);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
try {
conversionService.convert(map, sourceType, targetType);
} catch (ConversionFailedException e) {
assertTrue(e.getCause() instanceof ConverterNotFoundException);
}
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked")
Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
assertFalse(map.equals(result));
assertEquals((Integer) 9, result.get(1));
assertEquals((Integer) 37, result.get(2));
}
示例3: scalarMapNotGenericSourceField
import org.springframework.core.convert.ConversionFailedException; //导入依赖的package包/类
@Test
public void scalarMapNotGenericSourceField() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "9");
map.put("2", "37");
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource"));
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
try {
conversionService.convert(map, sourceType, targetType);
} catch (ConversionFailedException e) {
assertTrue(e.getCause() instanceof ConverterNotFoundException);
}
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked")
Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
assertFalse(map.equals(result));
assertEquals((Integer) 9, result.get(1));
assertEquals((Integer) 37, result.get(2));
}
示例4: collectionMap
import org.springframework.core.convert.ConversionFailedException; //导入依赖的package包/类
@Test
public void collectionMap() throws Exception {
Map<String, List<String>> map = new HashMap<String, List<String>>();
map.put("1", Arrays.asList("9", "12"));
map.put("2", Arrays.asList("37", "23"));
TypeDescriptor sourceType = TypeDescriptor.forObject(map);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
try {
conversionService.convert(map, sourceType, targetType);
} catch (ConversionFailedException e) {
assertTrue(e.getCause() instanceof ConverterNotFoundException);
}
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked")
Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
assertFalse(map.equals(result));
assertEquals(Arrays.asList(9, 12), result.get(1));
assertEquals(Arrays.asList(37, 23), result.get(2));
}
示例5: convert
import org.springframework.core.convert.ConversionFailedException; //导入依赖的package包/类
@Nullable
@Override
public Object convert(Object src, TypeDescriptor srcType, TypeDescriptor targetType) {
if (!this.matches(srcType, targetType)) {
return null;
}
boolean srcArr = srcType.isArray(), resolveAll = (srcArr || targetType.isArray());
String strSrc = (!srcArr ? ((String) src) : null);
String[] strSrcs = (srcArr ? SdcctStringUtils.splitTokens(((String[]) src)) : SdcctStringUtils.splitTokens(strSrc));
if (!resolveAll && (strSrcs.length > 1)) {
resolveAll = true;
}
try {
return (resolveAll ? this.resourceSrcResolver.resolveAll(strSrcs) : this.resourceSrcResolver.resolve(strSrc));
} catch (IOException e) {
throw new ConversionFailedException(srcType, targetType, src, e);
}
}
示例6: scalarList
import org.springframework.core.convert.ConversionFailedException; //导入依赖的package包/类
@Test
public void scalarList() throws Exception {
List<String> list = new ArrayList<String>();
list.add("9");
list.add("37");
TypeDescriptor sourceType = TypeDescriptor.forObject(list);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarListTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
try {
conversionService.convert(list, sourceType, targetType);
}
catch (ConversionFailedException ex) {
assertTrue(ex.getCause() instanceof ConverterNotFoundException);
}
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked")
List<String> result = (List<String>) conversionService.convert(list, sourceType, targetType);
assertFalse(list.equals(result));
assertEquals(9, result.get(0));
assertEquals(37, result.get(1));
}
示例7: getParamErrors
import org.springframework.core.convert.ConversionFailedException; //导入依赖的package包/类
/**
* TypeMismatchException中获取到参数错误类型
*
* @param e
*/
private ModelAndView getParamErrors(TypeMismatchException e) {
Throwable t = e.getCause();
if (t instanceof ConversionFailedException) {
ConversionFailedException x = (ConversionFailedException) t;
TypeDescriptor type = x.getTargetType();
Annotation[] annotations = type != null ? type.getAnnotations() : new Annotation[0];
Map<String, String> errors = new HashMap<String, String>();
for (Annotation a : annotations) {
if (a instanceof RequestParam) {
errors.put(((RequestParam) a).value(), "parameter type error!");
}
}
if (errors.size() > 0) {
return paramError(errors, ErrorCode.TYPE_MIS_MATCH);
}
}
JsonObjectBase jsonObject = JsonObjectUtils.buildGlobalError("parameter type error!", ErrorCode.TYPE_MIS_MATCH);
return JsonObjectUtils.JsonObjectError2ModelView((JsonObjectError) jsonObject);
}
示例8: convert
import org.springframework.core.convert.ConversionFailedException; //导入依赖的package包/类
@Override
public PrometheusAlert convert(PrometheusAlertRequest source) {
PrometheusAlert alert = new PrometheusAlert();
alert.setName(source.getAlertName());
alert.setDescription(source.getDescription());
alert.setPeriod(source.getPeriod());
alert.setAlertState(source.getAlertState() != null ? source.getAlertState() : CRITICAL);
double threshold = source.getThreshold();
String alertRuleName = source.getAlertRuleName();
try {
AlertOperator alertOperator = source.getAlertOperator() != null ? source.getAlertOperator() : AlertOperator.MORE_THAN;
String operator = alertOperator.getOperator();
String alertRule = templateService.createAlert(alertRuleName, alert.getName(), String.valueOf(threshold), alert.getPeriod(), operator);
alert.setAlertRule(alertRule);
alert.setParameters(createParametersFrom(threshold, alertOperator));
} catch (Exception e) {
throw new ConversionFailedException(
TypeDescriptor.valueOf(PrometheusAlertRequest.class),
TypeDescriptor.valueOf(PrometheusAlert.class),
source.toString(),
e);
}
return alert;
}
示例9: convertValueToDurationIfPossible
import org.springframework.core.convert.ConversionFailedException; //导入依赖的package包/类
private String convertValueToDurationIfPossible(final String value) {
try {
final ConversionService service = applicationContext.getEnvironment().getConversionService();
final Duration dur = service.convert(value, Duration.class);
if (dur != null) {
return String.valueOf(dur.toMillis());
}
} catch (final ConversionFailedException e) {
LOGGER.trace(e.getMessage());
}
return null;
}
示例10: get
import org.springframework.core.convert.ConversionFailedException; //导入依赖的package包/类
@Override
public <V> V get(String k, Class<V> vClass) {
if (conversionService.canConvert(String.class, vClass)) {
Object val = environment.getProperty("management.metrics.filter." + k);
try {
return conversionService.convert(val, vClass);
} catch (ConversionFailedException e) {
throw new ConfigurationException("Invalid configuration for '" + k + "' value '" + val + "' as " + vClass, e);
}
}
return null;
}
示例11: testToBigDecimalException
import org.springframework.core.convert.ConversionFailedException; //导入依赖的package包/类
@Test(expected = ConversionFailedException.class)
public void testToBigDecimalException()
throws NoSuchMethodException, SecurityException {
Method testMethod = getClass().getDeclaredMethod("BigDecimalParam",
BigDecimal.class);
MethodParameter param = new MethodParameter(testMethod, 0);
assertThat(this.invocableHandlerMethod.convert(param, "str")).isEqualTo("str");
}
示例12: receiveAndConvertFailed
import org.springframework.core.convert.ConversionFailedException; //导入依赖的package包/类
@Test
public void receiveAndConvertFailed() {
Message<?> expected = new GenericMessage<Object>("not a number test");
this.template.setReceiveMessage(expected);
this.template.setMessageConverter(new GenericMessageConverter());
thrown.expect(MessageConversionException.class);
thrown.expectCause(isA(ConversionFailedException.class));
this.template.receiveAndConvert("somewhere", Integer.class);
}
示例13: nothingInCommon
import org.springframework.core.convert.ConversionFailedException; //导入依赖的package包/类
@Test(expected = ConversionFailedException.class)
public void nothingInCommon() throws Exception {
List<Object> resources = new ArrayList<Object>();
resources.add(new ClassPathResource("test"));
resources.add(3);
TypeDescriptor sourceType = TypeDescriptor.forObject(resources);
assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources"))));
}
示例14: convertFromStreamToArrayNoConverter
import org.springframework.core.convert.ConversionFailedException; //导入依赖的package包/类
@Test
public void convertFromStreamToArrayNoConverter() throws NoSuchFieldException {
Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
TypeDescriptor arrayOfLongs = new TypeDescriptor(Types.class.getField("arrayOfLongs")); ;
thrown.expect(ConversionFailedException.class);
thrown.expectCause(is(instanceOf(ConverterNotFoundException.class)));
this.conversionService.convert(stream, arrayOfLongs);
}
示例15: setPropertyIntermediateListIsNullWithBadConversionService
import org.springframework.core.convert.ConversionFailedException; //导入依赖的package包/类
@Test
public void setPropertyIntermediateListIsNullWithBadConversionService() {
Foo target = new Foo();
AbstractPropertyAccessor accessor = createAccessor(target);
accessor.setConversionService(new GenericConversionService() {
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
throw new ConversionFailedException(sourceType, targetType, source, null);
}
});
accessor.setAutoGrowNestedPaths(true);
accessor.setPropertyValue("listOfMaps[0]['luckyNumber']", "9");
assertEquals("9", target.listOfMaps.get(0).get("luckyNumber"));
}