feat(forest): 增加基本类型转换

forest使用强制类型转换来处理基本类型,但和意图不符合,如-1无法被强转为long类型,所以覆盖原本的类型转换,使用Java包装类提供的字符串转换
This commit is contained in:
v-zhangjc9
2024-05-06 15:15:05 +08:00
parent 69b5ab558e
commit 864df40f34
4 changed files with 162 additions and 15 deletions

View File

@@ -0,0 +1,68 @@
package com.lanyuanxiaoyao.service.forest.configuration;
import com.dtflys.forest.converter.text.DefaultTextConverter;
import com.dtflys.forest.utils.StringUtils;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
/**
* Forest增强文本类型转换
*
* @author lanyuanxiaoyao
* @date 2024-05-06
*/
@SuppressWarnings("unchecked")
public class EnhanceTextConverter extends DefaultTextConverter {
private Object convert(byte[] source, Charset charset, String type) throws Exception {
return convert(StringUtils.fromBytes(source, charset), type);
}
private Object convert(String source, String type) throws Exception {
switch (type) {
case "boolean":
case "java.lang.Boolean":
return Boolean.valueOf(source);
case "int":
case "java.lang.Integer":
return Integer.valueOf(source);
case "long":
case "java.lang.Long":
return Long.valueOf(source);
case "float":
case "java.lang.Float":
return Float.valueOf(source);
case "double":
case "java.lang.Double":
return Double.valueOf(source);
default:
throw new Exception("the source may not be primary type");
}
}
@Override
public <T> T convertToJavaObject(String source, Type targetType) {
try {
return (T) convert(source, targetType.getTypeName());
} catch (Exception e) {
return super.convertToJavaObject(source, targetType);
}
}
@Override
public <T> T convertToJavaObject(byte[] source, Class<T> targetType, Charset charset) {
try {
return (T) convert(source, charset, targetType.getName());
} catch (Exception e) {
return super.convertToJavaObject(source, targetType, charset);
}
}
@Override
public <T> T convertToJavaObject(byte[] source, Type targetType, Charset charset) {
try {
return (T) convert(source, charset, targetType.getTypeName());
} catch (Exception e) {
return super.convertToJavaObject(source, targetType, charset);
}
}
}

View File

@@ -1,6 +1,7 @@
package com.lanyuanxiaoyao.service.forest.configuration;
import com.dtflys.forest.converter.json.ForestJacksonConverter;
import com.dtflys.forest.converter.text.DefaultTextConverter;
import com.dtflys.forest.springboot.annotation.ForestScan;
import com.fasterxml.jackson.datatype.eclipsecollections.EclipseCollectionsModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
@@ -24,13 +25,8 @@ public class ForestsConfiguration {
return converter;
}
/*@Bean
public ForestConfigurationProperties forestConfigurationProperties() {
ForestConfigurationProperties properties = new ForestConfigurationProperties();
properties.setBackend("httpclient");
properties.setInterceptors(ListUtil.of(SpringCloudDiscoveryInterceptor.class));
properties.setLogEnabled(false);
properties.setTimeout(60000);
return properties;
}*/
@Bean
public DefaultTextConverter defaultTextConverter() {
return new EnhanceTextConverter();
}
}