From de4adbf72a76ea46759d66dd9c36c43a08d78d48 Mon Sep 17 00:00:00 2001 From: mxd <838425805@qq.com> Date: Mon, 22 Nov 2021 20:54:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E`http`=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84`exceptBytes`=E6=96=B9=E6=B3=95=EF=BC=8C=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E8=BF=94=E5=9B=9E`byte[]`=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ssssssss/magicapi/modules/HttpModule.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/magic-api/src/main/java/org/ssssssss/magicapi/modules/HttpModule.java b/magic-api/src/main/java/org/ssssssss/magicapi/modules/HttpModule.java index f1fb6cee..fd7832a1 100644 --- a/magic-api/src/main/java/org/ssssssss/magicapi/modules/HttpModule.java +++ b/magic-api/src/main/java/org/ssssssss/magicapi/modules/HttpModule.java @@ -23,7 +23,7 @@ public class HttpModule implements MagicModule { private final RestTemplate template; private final HttpHeaders httpHeaders = new HttpHeaders(); - private final Class responseType = Object.class; + private Class responseType = Object.class; private final MultiValueMap params = new LinkedMultiValueMap<>(); private final MultiValueMap data = new LinkedMultiValueMap<>(); private final Map variables = new HashMap<>(); @@ -131,32 +131,38 @@ public class HttpModule implements MagicModule { return this; } + @Comment("设置返回值为`byte[]`") + public HttpModule expectBytes() { + this.responseType = byte[].class; + return this; + } + @Comment("发送`POST`请求") - public ResponseEntity post() { + public ResponseEntity post() { this.method(HttpMethod.POST); return this.execute(); } @Comment("发送`GET`请求") - public ResponseEntity get() { + public ResponseEntity get() { this.method(HttpMethod.GET); return this.execute(); } @Comment("发送`PUT`请求") - public ResponseEntity put() { + public ResponseEntity put() { this.method(HttpMethod.PUT); return this.execute(); } @Comment("发送`DELETE`请求") - public ResponseEntity delete() { + public ResponseEntity delete() { this.method(HttpMethod.DELETE); return this.execute(); } @Comment("执行请求") - public ResponseEntity execute() { + public ResponseEntity execute() { if (!this.params.isEmpty()) { String queryString = this.params.entrySet().stream() .map(it -> it.getValue().stream() @@ -174,6 +180,6 @@ public class HttpModule implements MagicModule { } else { this.entity = new HttpEntity<>(null, this.httpHeaders); } - return template.exchange(url, this.method, entity, Object.class, responseType, variables); + return template.exchange(url, this.method, entity, responseType, variables); } }