feat: 1.0.5版本升级,一个小补丁

This commit is contained in:
wangyu 2023-01-30 15:04:49 +08:00
parent 0b2ccbf3e7
commit 43e89f7399
5 changed files with 44 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import group.flyfish.rest.core.ThreadPoolManager;
import group.flyfish.rest.core.builder.TypedMapBuilder; import group.flyfish.rest.core.builder.TypedMapBuilder;
import group.flyfish.rest.core.resolver.*; import group.flyfish.rest.core.resolver.*;
import group.flyfish.rest.enums.HttpMethod; import group.flyfish.rest.enums.HttpMethod;
import group.flyfish.rest.enums.ResponseType;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import java.util.Map; import java.util.Map;
@ -206,4 +207,11 @@ public interface RestConstants {
static TypedMapBuilder<HttpMethod, HttpMethodResolver> resolverBuilder() { static TypedMapBuilder<HttpMethod, HttpMethodResolver> resolverBuilder() {
return TypedMapBuilder.builder(); return TypedMapBuilder.builder();
} }
// 响应类型映射
Map<String, ResponseType> RESPONSE_TYPE_MAP = TypedMapBuilder.<String, ResponseType>builder()
.with(TypeConstants.STRING, ResponseType.TEXT)
.with(TypeConstants.BYTE_ARRAY, ResponseType.BINARY)
.build();
} }

View File

@ -0,0 +1,13 @@
package group.flyfish.rest.constants;
/**
* 类型常量
*
* @author wangyu
*/
public interface TypeConstants {
String STRING = "java.lang.String";
String BYTE_ARRAY = "[B";
}

View File

@ -2,6 +2,7 @@ package group.flyfish.rest.core.client;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JavaType;
import group.flyfish.rest.constants.RestConstants;
import group.flyfish.rest.core.exception.RestClientException; import group.flyfish.rest.core.exception.RestClientException;
import group.flyfish.rest.core.factory.HttpClientFactoryBean; import group.flyfish.rest.core.factory.HttpClientFactoryBean;
import group.flyfish.rest.core.factory.HttpClientProvider; import group.flyfish.rest.core.factory.HttpClientProvider;
@ -214,7 +215,7 @@ final class DefaultRestClient implements RestClient {
@Nullable @Nullable
@Override @Override
public <T> T execute(Class<T> clazz) { public <T> T execute(Class<T> clazz) {
this.responseType = ResponseType.OBJECT; this.responseType = resolveType(clazz);
this.resultClass = clazz; this.resultClass = clazz;
try { try {
return innerExecute(); return innerExecute();
@ -234,7 +235,7 @@ final class DefaultRestClient implements RestClient {
@Nullable @Nullable
@Override @Override
public <T> T execute(JavaType type) { public <T> T execute(JavaType type) {
this.responseType = ResponseType.OBJECT; this.responseType = resolveType(type.getRawClass());
this.resultType = type; this.resultType = type;
try { try {
return innerExecute(); return innerExecute();
@ -289,6 +290,7 @@ final class DefaultRestClient implements RestClient {
HttpEntity entity = response.getEntity(); HttpEntity entity = response.getEntity();
if (consumer != null) { if (consumer != null) {
consumer.accept(entity); consumer.accept(entity);
return null;
} }
return resolveResponse(entity); return resolveResponse(entity);
} else { } else {
@ -305,6 +307,16 @@ final class DefaultRestClient implements RestClient {
return null; return null;
} }
/**
* 解析目标类型
*
* @param clazz 简单类型
* @return 响应类型
*/
private ResponseType resolveType(Class<?> clazz) {
return RestConstants.RESPONSE_TYPE_MAP.getOrDefault(clazz.getName(), ResponseType.OBJECT);
}
/** /**
* 解析结果 * 解析结果
* *

View File

@ -77,9 +77,6 @@ public final class JacksonUtil {
if (null == json || "".equals(json)) { if (null == json || "".equals(json)) {
return null; return null;
} }
if (String.class.isAssignableFrom(clazz)) {
return DataUtils.cast(json);
}
try { try {
return mapper.readValue(json, clazz); return mapper.readValue(json, clazz);
} catch (IOException e) { } catch (IOException e) {
@ -89,9 +86,6 @@ public final class JacksonUtil {
} }
public static <T> T fromJson(final String json, JavaType type) { public static <T> T fromJson(final String json, JavaType type) {
if (type.isTypeOrSubTypeOf(String.class)) {
return DataUtils.cast(json);
}
if (null == json || "".equals(json)) { if (null == json || "".equals(json)) {
return null; return null;
} }

View File

@ -48,6 +48,12 @@ public class MultipartTest {
} }
} }
@Test
public void download() {
byte[] data = testRestService.downloadByte();
System.out.println(data.length);
}
@RestService(baseUrl = "http://localhost:8999", timeout = 500) @RestService(baseUrl = "http://localhost:8999", timeout = 500)
public interface TestRestService { public interface TestRestService {
@ -56,5 +62,8 @@ public class MultipartTest {
@RestApi(uri = "/files", method = HttpMethod.POST) @RestApi(uri = "/files", method = HttpMethod.POST)
String uploadAnno(@RestPart("fbl") InputStream file, @RestPart.Filename("fbl") String name); String uploadAnno(@RestPart("fbl") InputStream file, @RestPart.Filename("fbl") String name);
@RestApi(uri = "/files")
byte[] downloadByte();
} }
} }