feat: 升级1.0.8。修复上传文件和传递body时param丢失的问题。

fix: 修正测试用例测试问题
This commit is contained in:
wangyu 2023-02-01 15:37:03 +08:00
parent 6aaa9b8cbe
commit a814b9e598
12 changed files with 46 additions and 28 deletions

View File

@ -13,7 +13,7 @@
<groupId>group.flyfish</groupId> <groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId> <artifactId>rest-proxy</artifactId>
<version>1.0.7</version> <version>1.0.8</version>
<properties> <properties>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.source>8</maven.compiler.source>
@ -22,7 +22,7 @@
<java.version>1.8</java.version> <java.version>1.8</java.version>
<commons-collection.version>4.4</commons-collection.version> <commons-collection.version>4.4</commons-collection.version>
<commons.lang.version>2.6</commons.lang.version> <commons.lang.version>2.6</commons.lang.version>
<sdk.version>1.0.7</sdk.version> <sdk.version>1.0.8</sdk.version>
</properties> </properties>
<packaging>pom</packaging> <packaging>pom</packaging>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>group.flyfish</groupId> <groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId> <artifactId>rest-proxy</artifactId>
<version>1.0.7</version> <version>1.0.8</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>group.flyfish</groupId> <groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId> <artifactId>rest-proxy</artifactId>
<version>1.0.7</version> <version>1.0.8</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -103,6 +103,6 @@ public class RestClientProperties implements InitializingBean {
if (DataUtils.isNotEmpty(configures)) { if (DataUtils.isNotEmpty(configures)) {
configures.forEach(item -> item.configure(this)); configures.forEach(item -> item.configure(this));
} }
RestInvokers.forEach(invoker -> invoker.configure(this)); RestInvokers.configure(this);
} }
} }

View File

@ -14,6 +14,7 @@ import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.StreamUtils; import org.springframework.util.StreamUtils;
@ -286,17 +287,17 @@ final class DefaultRestClient implements RestClient {
log.info("【Rest Invoke】{} {}", request.getMethod(), request.getURI()); log.info("【Rest Invoke】{} {}", request.getMethod(), request.getURI());
try (CloseableHttpResponse response = clientProvider.getClient().execute(request)) { try (CloseableHttpResponse response = clientProvider.getClient().execute(request)) {
StatusLine statusLine = response.getStatusLine(); StatusLine statusLine = response.getStatusLine();
if (200 == statusLine.getStatusCode()) { if (HttpStatus.valueOf(statusLine.getStatusCode()).isError()) {
int requestCode = statusLine.getStatusCode();
log.error(request.getURI() + "接口调用失败code:" + requestCode);
handleError(new RestClientException("网络请求状态异常!代码:" + requestCode, null, statusLine));
} else {
HttpEntity entity = response.getEntity(); HttpEntity entity = response.getEntity();
if (consumer != null) { if (consumer != null) {
consumer.accept(entity); consumer.accept(entity);
return null; return null;
} }
return resolveResponse(entity); return resolveResponse(entity);
} else {
int requestCode = statusLine.getStatusCode();
log.error(request.getURI() + "接口调用失败code:" + requestCode);
handleError(new RestClientException("网络请求状态异常!代码:" + requestCode, null, statusLine));
} }
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
handleError(new RestClientException(e.getMessage(), e, null)); handleError(new RestClientException(e.getMessage(), e, null));

View File

@ -20,7 +20,6 @@ public class HttpDeleteResolver extends AbstractParamResolver implements HttpMet
*/ */
@Override @Override
public HttpRequestBase resolve(RestClientBuilder builder) { public HttpRequestBase resolve(RestClientBuilder builder) {
resolveParams(builder); return new HttpDelete(resolveParams(builder).getUrl());
return new HttpDelete(builder.getUrl());
} }
} }

View File

@ -12,7 +12,6 @@ public class HttpGetResolver extends AbstractParamResolver implements HttpMethod
@Override @Override
public HttpRequestBase resolve(RestClientBuilder builder) { public HttpRequestBase resolve(RestClientBuilder builder) {
resolveParams(builder); return new HttpGet(resolveParams(builder).getUrl());
return new HttpGet(builder.getUrl());
} }
} }

View File

@ -2,6 +2,7 @@ package group.flyfish.rest.core.resolver;
import group.flyfish.rest.core.client.RestClientBuilder; import group.flyfish.rest.core.client.RestClientBuilder;
import group.flyfish.rest.core.resolver.support.AbstractBodyResolver; import group.flyfish.rest.core.resolver.support.AbstractBodyResolver;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.methods.HttpRequestBase;
@ -12,8 +13,9 @@ public class HttpPostResolver extends AbstractBodyResolver implements HttpMethod
@Override @Override
public HttpRequestBase resolve(RestClientBuilder builder) { public HttpRequestBase resolve(RestClientBuilder builder) {
HttpEntity entity = buildEntity(builder);
HttpPost post = new HttpPost(builder.getUrl()); HttpPost post = new HttpPost(builder.getUrl());
post.setEntity(buildEntity(builder)); post.setEntity(entity);
return post; return post;
} }

View File

@ -25,18 +25,21 @@ import static group.flyfish.rest.constants.RestConstants.MIME_MAP;
* *
* @author wangyu * @author wangyu
*/ */
public abstract class AbstractBodyResolver { public abstract class AbstractBodyResolver extends AbstractParamResolver {
/** /**
* 构建entity * 构建entity
* 对于纯urlencoded form我们将所有参数处理为formdata
* 对于上传和body请求我们照旧处理query
* *
* @param builder 构建器 * @param builder 构建器
* @return 结果 * @return 结果
*/ */
protected HttpEntity buildEntity(RestClientBuilder builder) { protected HttpEntity buildEntity(RestClientBuilder builder) {
return builder.isMultipart() ? buildMultipart(builder) return builder.isMultipart() ? buildMultipart(resolveParams(builder)) :
: DataUtils.isNotBlank(builder.getBody()) ? buildJson(builder) DataUtils.isNotBlank(builder.getBody()) ?
: buildFormData(builder); buildJson(resolveParams(builder)) :
buildFormData(builder);
} }
/** /**
@ -48,6 +51,7 @@ public abstract class AbstractBodyResolver {
private HttpEntity buildMultipart(RestClientBuilder clientBuilder) { private HttpEntity buildMultipart(RestClientBuilder clientBuilder) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setCharset(clientBuilder.getCharset());
clientBuilder.getMultipartList().forEach(multipart -> { clientBuilder.getMultipartList().forEach(multipart -> {
Object data = multipart.getData(); Object data = multipart.getData();
String name = multipart.getName(); String name = multipart.getName();
@ -59,9 +63,11 @@ public abstract class AbstractBodyResolver {
} else if (data instanceof InputStream) { } else if (data instanceof InputStream) {
builder.addBinaryBody(name, (InputStream) data, resolveType(filename), filename); builder.addBinaryBody(name, (InputStream) data, resolveType(filename), filename);
} else { } else {
throw new IllegalArgumentException("上传时,输入的数据不被支持!"); // 对于无法识别的内容统一处理为string
builder.addTextBody(name, String.valueOf(data));
} }
}); });
// 处理query参数
return builder.build(); return builder.build();
} }

View File

@ -17,7 +17,7 @@ public abstract class AbstractParamResolver {
* *
* @param builder 构建器 * @param builder 构建器
*/ */
protected void resolveParams(RestClientBuilder builder) { protected RestClientBuilder resolveParams(RestClientBuilder builder) {
if (DataUtils.isNotEmpty(builder.getParams())) { if (DataUtils.isNotEmpty(builder.getParams())) {
String start = builder.getUrl().contains("?") ? "&" : "?"; String start = builder.getUrl().contains("?") ? "&" : "?";
String params = builder.getParams().entrySet().stream() String params = builder.getParams().entrySet().stream()
@ -25,5 +25,6 @@ public abstract class AbstractParamResolver {
.collect(Collectors.joining("&")); .collect(Collectors.joining("&"));
builder.url(builder.getUrl() + start + params); builder.url(builder.getUrl() + start + params);
} }
return builder;
} }
} }

View File

@ -1,8 +1,9 @@
package group.flyfish.rest.registry.proxy; package group.flyfish.rest.registry.proxy;
import group.flyfish.rest.configuration.RestClientProperties;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
/** /**
* rest执行器实例集合 * rest执行器实例集合
@ -11,13 +12,22 @@ import java.util.function.Consumer;
*/ */
public class RestInvokers { public class RestInvokers {
private static final List<RestProxyInvoker> invokers = new ArrayList<>(); private static RestClientProperties properties;
private static List<RestProxyInvoker> invokers = new ArrayList<>();
public static void add(RestProxyInvoker invoker) { public static void add(RestProxyInvoker invoker) {
if (null != properties) {
invoker.configure(properties);
} else {
invokers.add(invoker); invokers.add(invoker);
} }
}
public static void forEach(Consumer<RestProxyInvoker> handler) { public static synchronized void configure(RestClientProperties properties) {
invokers.forEach(handler); invokers.forEach(invoker -> invoker.configure(properties));
invokers.clear();
invokers = null;
RestInvokers.properties = properties;
} }
} }

View File

@ -38,7 +38,7 @@ public class MultipartTest {
@Test @Test
public void test() { public void test() {
File file = new File("/Users/wangyu/Desktop/2022年终述职报告.docx"); File file = new File("/Users/wangyu/Desktop/2022年终述职报告.docx");
String filename = testRestService.uploadPart(new Multipart("file", file.getName(), file)); String filename = testRestService.uploadPart(new Multipart("file", file.getName(), file), file.getName(), 55L);
System.out.println(filename); System.out.println(filename);
try (InputStream in = Files.newInputStream(file.toPath())) { try (InputStream in = Files.newInputStream(file.toPath())) {
filename = testRestService.uploadAnno(in, file.getName()); filename = testRestService.uploadAnno(in, file.getName());
@ -58,7 +58,7 @@ public class MultipartTest {
public interface TestRestService { public interface TestRestService {
@RestApi(uri = "/files", method = HttpMethod.POST) @RestApi(uri = "/files", method = HttpMethod.POST)
String uploadPart(@RestPart Multipart file); String uploadPart(@RestPart Multipart file, String name, @RestPart("token") Long token);
@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);