mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
可指定requestMethod
This commit is contained in:
@@ -36,6 +36,11 @@ public class OpenServiceConfig extends AlipayServiceConfiguration {
|
||||
protected String getDocTitle() {
|
||||
return "故事API";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean swaggerAccessProtected() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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<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);
|
||||
for (Map.Entry<String, String> 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<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 {
|
||||
// 创建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.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<String, String> header) {
|
||||
return this.doPost(url, requestForm, header);
|
||||
}
|
||||
|
||||
protected String doPost(String url, RequestForm requestForm, Map<String, String> header) {
|
||||
try {
|
||||
Map<String, String> form = requestForm.getForm();
|
||||
List<UploadFile> 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<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) {
|
||||
ErrorResponse result = new ErrorResponse();
|
||||
result.setCode(HTTP_ERROR_CODE);
|
||||
|
@@ -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<UploadFile> files;
|
||||
|
||||
private String charset;
|
||||
|
||||
private RequestMethod requestMethod = RequestMethod.POST;
|
||||
|
||||
public RequestForm(Map<String, String> 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.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<T extends BaseResponse> {
|
||||
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<T extends BaseResponse> {
|
||||
return method;
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定版本号
|
||||
* @param version
|
||||
*/
|
||||
public void setVersion(String version) {
|
||||
this.version = version;
|
||||
}
|
||||
@@ -134,4 +141,12 @@ public abstract class BaseRequest<T extends BaseResponse> {
|
||||
public Class<T> getResponseClass() {
|
||||
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() {
|
||||
return "alipay.story.find";
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user