可指定requestMethod

1.x
tanghc 6 years ago
parent 8db5d290c2
commit 4bd3869ed8
  1. 5
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/config/OpenServiceConfig.java
  2. 13
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenHttp.java
  3. 37
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java
  4. 5
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/common/RequestForm.java
  5. 7
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/common/RequestMethod.java
  6. 15
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/BaseRequest.java
  7. 1
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/GetStoryRequest.java

@ -36,6 +36,11 @@ public class OpenServiceConfig extends AlipayServiceConfiguration {
protected String getDocTitle() { protected String getDocTitle() {
return "故事API"; return "故事API";
} }
@Override
protected boolean swaggerAccessProtected() {
return false;
}
} }
} }

@ -71,13 +71,14 @@ public class OpenHttp {
/** /**
* 提交表单 * 提交表单
* *
* @param url * @param url url
* @param form * @param form 参数
* @param header header内容 * @param header header
* @param method 请求方式postget等
* @return * @return
* @throws IOException * @throws IOException
*/ */
public String postFormBody(String url, Map<String, String> form, Map<String, String> header) throws IOException { public String requestFormBody(String url, Map<String, String> form, Map<String, String> header, String method) throws IOException {
FormBody.Builder paramBuilder = new FormBody.Builder(StandardCharsets.UTF_8); FormBody.Builder paramBuilder = new FormBody.Builder(StandardCharsets.UTF_8);
for (Map.Entry<String, String> entry : form.entrySet()) { for (Map.Entry<String, String> entry : form.entrySet()) {
paramBuilder.add(entry.getKey(), entry.getValue()); paramBuilder.add(entry.getKey(), entry.getValue());
@ -85,7 +86,7 @@ public class OpenHttp {
FormBody formBody = paramBuilder.build(); FormBody formBody = paramBuilder.build();
Request.Builder requestBuilder = new Request.Builder() Request.Builder requestBuilder = new Request.Builder()
.url(url) .url(url)
.post(formBody); .method(method, formBody);
// 添加header // 添加header
addHeader(requestBuilder, header); addHeader(requestBuilder, header);
@ -110,7 +111,7 @@ public class OpenHttp {
* @return * @return
* @throws IOException * @throws IOException
*/ */
public String postFile(String url, Map<String, String> form, Map<String, String> header, List<UploadFile> files) public String requestFile(String url, Map<String, String> form, Map<String, String> header, List<UploadFile> files)
throws IOException { throws IOException {
// 创建MultipartBody.Builder,用于添加请求的数据 // 创建MultipartBody.Builder,用于添加请求的数据
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder(); MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();

@ -3,10 +3,13 @@ package com.gitee.sop.sdk.client;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.gitee.sop.sdk.common.OpenConfig; import com.gitee.sop.sdk.common.OpenConfig;
import com.gitee.sop.sdk.common.RequestForm; 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.common.UploadFile;
import com.gitee.sop.sdk.response.BaseResponse; import com.gitee.sop.sdk.response.BaseResponse;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -26,23 +29,45 @@ public class OpenRequest {
} }
public String request(String url, RequestForm requestForm, Map<String, String> header) { public String request(String url, RequestForm requestForm, Map<String, String> header) {
return this.doPost(url, requestForm, header);
}
protected String doPost(String url, RequestForm requestForm, Map<String, String> header) {
try { try {
Map<String, String> form = requestForm.getForm(); Map<String, String> form = requestForm.getForm();
List<UploadFile> files = requestForm.getFiles(); List<UploadFile> files = requestForm.getFiles();
if (files != null && files.size() > 0) { if (files != null && files.size() > 0) {
return openHttp.postFile(url, form, header, files); return openHttp.requestFile(url, form, header, files);
} else { } 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) { } catch (IOException e) {
return this.causeException(e); return this.causeException(e);
} }
} }
protected String buildGetQueryString(Map<String, String> params, String charset) throws UnsupportedEncodingException {
if (params == null || params.size() == 0) {
return "";
}
StringBuilder query = new StringBuilder();
int i = 0;
for (Map.Entry<String, String> 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) { protected String causeException(Exception e) {
ErrorResponse result = new ErrorResponse(); ErrorResponse result = new ErrorResponse();
result.setCode(HTTP_ERROR_CODE); result.setCode(HTTP_ERROR_CODE);

@ -3,7 +3,6 @@ package com.gitee.sop.sdk.common;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -16,6 +15,10 @@ public class RequestForm {
/** 上传文件 */ /** 上传文件 */
private List<UploadFile> files; private List<UploadFile> files;
private String charset;
private RequestMethod requestMethod = RequestMethod.POST;
public RequestForm(Map<String, String> m) { public RequestForm(Map<String, String> m) {
this.form = m; this.form = m;
} }

@ -0,0 +1,7 @@
package com.gitee.sop.sdk.common;
public enum RequestMethod {
GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
}

@ -3,6 +3,7 @@ package com.gitee.sop.sdk.request;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.gitee.sop.sdk.common.OpenConfig; import com.gitee.sop.sdk.common.OpenConfig;
import com.gitee.sop.sdk.common.RequestForm; 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.SdkConfig;
import com.gitee.sop.sdk.common.UploadFile; import com.gitee.sop.sdk.common.UploadFile;
import com.gitee.sop.sdk.response.BaseResponse; import com.gitee.sop.sdk.response.BaseResponse;
@ -99,6 +100,8 @@ public abstract class BaseRequest<T extends BaseResponse> {
params.put(openConfig.getDataName(), biz_content); params.put(openConfig.getDataName(), biz_content);
RequestForm requestForm = new RequestForm(params); RequestForm requestForm = new RequestForm(params);
requestForm.setRequestMethod(getRequestMethod());
requestForm.setCharset(this.charset);
requestForm.setFiles(this.files); requestForm.setFiles(this.files);
return requestForm; return requestForm;
} }
@ -115,6 +118,10 @@ public abstract class BaseRequest<T extends BaseResponse> {
return method; return method;
} }
/**
* 指定版本号
* @param version
*/
public void setVersion(String version) { public void setVersion(String version) {
this.version = version; this.version = version;
} }
@ -134,4 +141,12 @@ public abstract class BaseRequest<T extends BaseResponse> {
public Class<T> getResponseClass() { public Class<T> getResponseClass() {
return responseClass; return responseClass;
} }
/**
* 指定HTTP请求method,默认POST
* @return
*/
protected RequestMethod getRequestMethod() {
return RequestMethod.POST;
}
} }

@ -7,4 +7,5 @@ public class GetStoryRequest extends BaseRequest<GetStoryResponse> {
protected String method() { protected String method() {
return "alipay.story.find"; return "alipay.story.find";
} }
} }

Loading…
Cancel
Save