diff --git a/service-forest/src/main/java/com/lanyuanxiaoyao/service/forest/configuration/EnhanceTextConverter.java b/service-forest/src/main/java/com/lanyuanxiaoyao/service/forest/configuration/EnhanceTextConverter.java new file mode 100644 index 0000000..8df52d5 --- /dev/null +++ b/service-forest/src/main/java/com/lanyuanxiaoyao/service/forest/configuration/EnhanceTextConverter.java @@ -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 convertToJavaObject(String source, Type targetType) { + try { + return (T) convert(source, targetType.getTypeName()); + } catch (Exception e) { + return super.convertToJavaObject(source, targetType); + } + } + + @Override + public T convertToJavaObject(byte[] source, Class targetType, Charset charset) { + try { + return (T) convert(source, charset, targetType.getName()); + } catch (Exception e) { + return super.convertToJavaObject(source, targetType, charset); + } + } + + @Override + public 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); + } + } +} diff --git a/service-forest/src/main/java/com/lanyuanxiaoyao/service/forest/configuration/ForestsConfiguration.java b/service-forest/src/main/java/com/lanyuanxiaoyao/service/forest/configuration/ForestsConfiguration.java index a65561a..fdd1fb3 100644 --- a/service-forest/src/main/java/com/lanyuanxiaoyao/service/forest/configuration/ForestsConfiguration.java +++ b/service-forest/src/main/java/com/lanyuanxiaoyao/service/forest/configuration/ForestsConfiguration.java @@ -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(); + } } diff --git a/service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestClient.java b/service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestClient.java index d99bee1..b30a9cb 100644 --- a/service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestClient.java +++ b/service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestClient.java @@ -2,6 +2,12 @@ package com.lanyuanxiaoyao.service.forest; import com.dtflys.forest.Forest; 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 @@ -9,9 +15,13 @@ import com.dtflys.forest.annotation.Get; */ public class TestClient { public static void main(String[] args) { + ForestConfiguration config = Forest.config(); + config.getConverterMap() + .put(ForestDataType.TEXT, new EnhanceTextConverter()); TestService testService = Forest.client(TestService.class); - System.out.println(testService.success()); - System.out.println(testService.error()); + // System.out.println(testService.success()); + // System.out.println(testService.error()); + System.out.println(testService.number()); } public interface TestService { @@ -20,5 +30,64 @@ public class TestClient { @Get("http://localhost:8000/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 convertToJavaObject(String source, Type targetType) { + try { + return (T) convert(source, targetType.getTypeName()); + } catch (Exception e) { + return super.convertToJavaObject(source, targetType); + } + } + + @Override + public T convertToJavaObject(byte[] source, Class targetType, Charset charset) { + try { + return (T) convert(source, charset, targetType.getName()); + } catch (Exception e) { + return super.convertToJavaObject(source, targetType, charset); + } + } + + @Override + public 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); + } + } } } diff --git a/service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestServer.java b/service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestServer.java index 6fca7a3..9750ea5 100644 --- a/service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestServer.java +++ b/service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestServer.java @@ -16,15 +16,17 @@ public class TestServer { HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0); server.createContext("/success", new SuccessHandler()); server.createContext("/error", new ErrorHandler()); + server.createContext("/number", new NumberHandler()); server.start(); } public static class SuccessHandler implements HttpHandler { @Override public void handle(HttpExchange exchange) throws IOException { - exchange.sendResponseHeaders(200, 0); + byte[] result = "Success".getBytes(); + exchange.sendResponseHeaders(200, result.length); OutputStream body = exchange.getResponseBody(); - body.write("Success".getBytes()); + body.write(result); body.close(); } } @@ -32,9 +34,21 @@ public class TestServer { public static class ErrorHandler implements HttpHandler { @Override public void handle(HttpExchange exchange) throws IOException { - exchange.sendResponseHeaders(500, 0); + byte[] result = "Error".getBytes(); + exchange.sendResponseHeaders(500, result.length); 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(); } }