diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/config/OpenServiceConfig.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/config/OpenServiceConfig.java index 1cfba892..0c8f1023 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/config/OpenServiceConfig.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/config/OpenServiceConfig.java @@ -36,6 +36,11 @@ public class OpenServiceConfig extends AlipayServiceConfiguration { protected String getDocTitle() { return "故事API"; } + + @Override + protected boolean swaggerAccessProtected() { + return false; + } } } diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenHttp.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenHttp.java index 46dc0153..64dc253d 100644 --- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenHttp.java +++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenHttp.java @@ -71,13 +71,14 @@ public class OpenHttp { /** * 提交表单 * - * @param url - * @param form - * @param header header内容 + * @param url url + * @param form 参数 + * @param header header + * @param method 请求方式,post,get等 * @return * @throws IOException */ - public String postFormBody(String url, Map form, Map header) throws IOException { + public String requestFormBody(String url, Map form, Map header, String method) throws IOException { FormBody.Builder paramBuilder = new FormBody.Builder(StandardCharsets.UTF_8); for (Map.Entry entry : form.entrySet()) { paramBuilder.add(entry.getKey(), entry.getValue()); @@ -85,7 +86,7 @@ public class OpenHttp { FormBody formBody = paramBuilder.build(); Request.Builder requestBuilder = new Request.Builder() .url(url) - .post(formBody); + .method(method, formBody); // 添加header addHeader(requestBuilder, header); @@ -110,7 +111,7 @@ public class OpenHttp { * @return * @throws IOException */ - public String postFile(String url, Map form, Map header, List files) + public String requestFile(String url, Map form, Map header, List files) throws IOException { // 创建MultipartBody.Builder,用于添加请求的数据 MultipartBody.Builder bodyBuilder = new MultipartBody.Builder(); diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java index 2ca5f123..b3556566 100644 --- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java +++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java @@ -3,10 +3,13 @@ package com.gitee.sop.sdk.client; import com.alibaba.fastjson.JSON; import com.gitee.sop.sdk.common.OpenConfig; import com.gitee.sop.sdk.common.RequestForm; +import com.gitee.sop.sdk.common.RequestMethod; import com.gitee.sop.sdk.common.UploadFile; import com.gitee.sop.sdk.response.BaseResponse; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.util.List; import java.util.Map; @@ -26,23 +29,45 @@ public class OpenRequest { } public String request(String url, RequestForm requestForm, Map header) { - return this.doPost(url, requestForm, header); - } - - protected String doPost(String url, RequestForm requestForm, Map header) { try { Map form = requestForm.getForm(); List files = requestForm.getFiles(); if (files != null && files.size() > 0) { - return openHttp.postFile(url, form, header, files); + return openHttp.requestFile(url, form, header, files); } else { - return openHttp.postFormBody(url, form, header); + RequestMethod requestMethod = requestForm.getRequestMethod(); + if (requestMethod == RequestMethod.GET) { + String query = this.buildGetQueryString(form, requestForm.getCharset()); + if (query != null && query.length() > 0) { + url = url + "?" + query; + } + return openHttp.get(url, header); + } else { + return openHttp.requestFormBody(url, form, header, requestMethod.name()); + } } } catch (IOException e) { return this.causeException(e); } } + protected String buildGetQueryString(Map params, String charset) throws UnsupportedEncodingException { + if (params == null || params.size() == 0) { + return ""; + } + StringBuilder query = new StringBuilder(); + int i = 0; + for (Map.Entry entry : params.entrySet()) { + String name = entry.getKey(); + String value = entry.getValue(); + if (i++ > 0) { + query.append("&"); + } + query.append(name).append("=").append(URLEncoder.encode(value, charset)); + } + return query.toString(); + } + protected String causeException(Exception e) { ErrorResponse result = new ErrorResponse(); result.setCode(HTTP_ERROR_CODE); diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/common/RequestForm.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/common/RequestForm.java index e981fe53..852e1f18 100644 --- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/common/RequestForm.java +++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/common/RequestForm.java @@ -3,7 +3,6 @@ package com.gitee.sop.sdk.common; import lombok.Getter; import lombok.Setter; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -16,6 +15,10 @@ public class RequestForm { /** 上传文件 */ private List files; + private String charset; + + private RequestMethod requestMethod = RequestMethod.POST; + public RequestForm(Map m) { this.form = m; } diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/common/RequestMethod.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/common/RequestMethod.java new file mode 100644 index 00000000..7089ab98 --- /dev/null +++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/common/RequestMethod.java @@ -0,0 +1,7 @@ +package com.gitee.sop.sdk.common; + +public enum RequestMethod { + + GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE + +} \ No newline at end of file diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/BaseRequest.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/BaseRequest.java index 53d328d9..59a00b2a 100644 --- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/BaseRequest.java +++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/BaseRequest.java @@ -3,6 +3,7 @@ package com.gitee.sop.sdk.request; import com.alibaba.fastjson.JSON; import com.gitee.sop.sdk.common.OpenConfig; import com.gitee.sop.sdk.common.RequestForm; +import com.gitee.sop.sdk.common.RequestMethod; import com.gitee.sop.sdk.common.SdkConfig; import com.gitee.sop.sdk.common.UploadFile; import com.gitee.sop.sdk.response.BaseResponse; @@ -99,6 +100,8 @@ public abstract class BaseRequest { params.put(openConfig.getDataName(), biz_content); RequestForm requestForm = new RequestForm(params); + requestForm.setRequestMethod(getRequestMethod()); + requestForm.setCharset(this.charset); requestForm.setFiles(this.files); return requestForm; } @@ -115,6 +118,10 @@ public abstract class BaseRequest { return method; } + /** + * 指定版本号 + * @param version + */ public void setVersion(String version) { this.version = version; } @@ -134,4 +141,12 @@ public abstract class BaseRequest { public Class getResponseClass() { return responseClass; } + + /** + * 指定HTTP请求method,默认POST + * @return + */ + protected RequestMethod getRequestMethod() { + return RequestMethod.POST; + } } diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/GetStoryRequest.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/GetStoryRequest.java index 7db3c3b9..e8a1f062 100644 --- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/GetStoryRequest.java +++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/GetStoryRequest.java @@ -7,4 +7,5 @@ public class GetStoryRequest extends BaseRequest { protected String method() { return "alipay.story.find"; } + }