feat(forest): 增加基本类型转换
forest使用强制类型转换来处理基本类型,但和意图不符合,如-1无法被强转为long类型,所以覆盖原本的类型转换,使用Java包装类提供的字符串转换
This commit is contained in:
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.lanyuanxiaoyao.service.forest.configuration;
|
package com.lanyuanxiaoyao.service.forest.configuration;
|
||||||
|
|
||||||
import com.dtflys.forest.converter.json.ForestJacksonConverter;
|
import com.dtflys.forest.converter.json.ForestJacksonConverter;
|
||||||
|
import com.dtflys.forest.converter.text.DefaultTextConverter;
|
||||||
import com.dtflys.forest.springboot.annotation.ForestScan;
|
import com.dtflys.forest.springboot.annotation.ForestScan;
|
||||||
import com.fasterxml.jackson.datatype.eclipsecollections.EclipseCollectionsModule;
|
import com.fasterxml.jackson.datatype.eclipsecollections.EclipseCollectionsModule;
|
||||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
@@ -24,13 +25,8 @@ public class ForestsConfiguration {
|
|||||||
return converter;
|
return converter;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*@Bean
|
@Bean
|
||||||
public ForestConfigurationProperties forestConfigurationProperties() {
|
public DefaultTextConverter defaultTextConverter() {
|
||||||
ForestConfigurationProperties properties = new ForestConfigurationProperties();
|
return new EnhanceTextConverter();
|
||||||
properties.setBackend("httpclient");
|
}
|
||||||
properties.setInterceptors(ListUtil.of(SpringCloudDiscoveryInterceptor.class));
|
|
||||||
properties.setLogEnabled(false);
|
|
||||||
properties.setTimeout(60000);
|
|
||||||
return properties;
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,12 @@ package com.lanyuanxiaoyao.service.forest;
|
|||||||
|
|
||||||
import com.dtflys.forest.Forest;
|
import com.dtflys.forest.Forest;
|
||||||
import com.dtflys.forest.annotation.Get;
|
import com.dtflys.forest.annotation.Get;
|
||||||
|
import com.dtflys.forest.config.ForestConfiguration;
|
||||||
|
import com.dtflys.forest.converter.text.DefaultTextConverter;
|
||||||
|
import com.dtflys.forest.utils.ForestDataType;
|
||||||
|
import com.dtflys.forest.utils.StringUtils;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author lanyuanxiaoyao
|
* @author lanyuanxiaoyao
|
||||||
@@ -9,9 +15,13 @@ import com.dtflys.forest.annotation.Get;
|
|||||||
*/
|
*/
|
||||||
public class TestClient {
|
public class TestClient {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
ForestConfiguration config = Forest.config();
|
||||||
|
config.getConverterMap()
|
||||||
|
.put(ForestDataType.TEXT, new EnhanceTextConverter());
|
||||||
TestService testService = Forest.client(TestService.class);
|
TestService testService = Forest.client(TestService.class);
|
||||||
System.out.println(testService.success());
|
// System.out.println(testService.success());
|
||||||
System.out.println(testService.error());
|
// System.out.println(testService.error());
|
||||||
|
System.out.println(testService.number());
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface TestService {
|
public interface TestService {
|
||||||
@@ -20,5 +30,64 @@ public class TestClient {
|
|||||||
|
|
||||||
@Get("http://localhost:8000/error")
|
@Get("http://localhost:8000/error")
|
||||||
String error();
|
String error();
|
||||||
|
|
||||||
|
@Get(value = "http://localhost:8000/number")
|
||||||
|
String number();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,15 +16,17 @@ public class TestServer {
|
|||||||
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
|
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
|
||||||
server.createContext("/success", new SuccessHandler());
|
server.createContext("/success", new SuccessHandler());
|
||||||
server.createContext("/error", new ErrorHandler());
|
server.createContext("/error", new ErrorHandler());
|
||||||
|
server.createContext("/number", new NumberHandler());
|
||||||
server.start();
|
server.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class SuccessHandler implements HttpHandler {
|
public static class SuccessHandler implements HttpHandler {
|
||||||
@Override
|
@Override
|
||||||
public void handle(HttpExchange exchange) throws IOException {
|
public void handle(HttpExchange exchange) throws IOException {
|
||||||
exchange.sendResponseHeaders(200, 0);
|
byte[] result = "Success".getBytes();
|
||||||
|
exchange.sendResponseHeaders(200, result.length);
|
||||||
OutputStream body = exchange.getResponseBody();
|
OutputStream body = exchange.getResponseBody();
|
||||||
body.write("Success".getBytes());
|
body.write(result);
|
||||||
body.close();
|
body.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,9 +34,21 @@ public class TestServer {
|
|||||||
public static class ErrorHandler implements HttpHandler {
|
public static class ErrorHandler implements HttpHandler {
|
||||||
@Override
|
@Override
|
||||||
public void handle(HttpExchange exchange) throws IOException {
|
public void handle(HttpExchange exchange) throws IOException {
|
||||||
exchange.sendResponseHeaders(500, 0);
|
byte[] result = "Error".getBytes();
|
||||||
|
exchange.sendResponseHeaders(500, result.length);
|
||||||
OutputStream body = exchange.getResponseBody();
|
OutputStream body = exchange.getResponseBody();
|
||||||
body.write("Error".getBytes());
|
body.write(result);
|
||||||
|
body.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class NumberHandler implements HttpHandler {
|
||||||
|
@Override
|
||||||
|
public void handle(HttpExchange exchange) throws IOException {
|
||||||
|
byte[] result = "-1".getBytes();
|
||||||
|
exchange.sendResponseHeaders(200, result.length);
|
||||||
|
OutputStream body = exchange.getResponseBody();
|
||||||
|
body.write(result);
|
||||||
body.close();
|
body.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user