Skip to content

Commit c0d0bed

Browse files
committedAug 31, 2023
fix RestParam bug
1 parent 9675d9d commit c0d0bed

File tree

7 files changed

+94
-82
lines changed

7 files changed

+94
-82
lines changed
 

Diff for: ‎docs/wiki/springboot-starter.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ RestClient工具类提供了对Restful接口的调用能力
467467
```java
468468

469469
import com.alibaba.fastjson.JSONObject;
470-
import com.codingapi.springboot.framework.rest.param.RestParamBuilder;
470+
import com.codingapi.springboot.framework.rest.param.RestParam;
471471
import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties;
472472
import lombok.extern.slf4j.Slf4j;
473473
import org.junit.jupiter.api.Test;
@@ -489,7 +489,7 @@ class RestClientTest {
489489
proxyProperties.setProxyHost("127.0.0.1");
490490
proxyProperties.setProxyPort(7890);
491491
RestClient restClient = new RestClient(proxyProperties,baseUrl,5,"{}",null,null);
492-
String response = restClient.get("api/v5/market/candles", RestParamBuilder.create()
492+
String response = restClient.get("api/v5/market/candles", RestParam.create()
493493
.add("instId","BTC-USDT")
494494
.add("bar","1m")
495495
.add("limit","300")

Diff for: ‎springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/RestClient.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.codingapi.springboot.framework.rest;
22

33
import com.alibaba.fastjson.JSON;
4-
import com.codingapi.springboot.framework.rest.param.RestParamBuilder;
4+
import com.codingapi.springboot.framework.rest.param.RestParam;
55
import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties;
66
import lombok.extern.slf4j.Slf4j;
77
import org.springframework.http.HttpHeaders;
@@ -61,20 +61,20 @@ public String get(String api, HttpHeaders headers, MultiValueMap<String, String>
6161
return emptyResponse;
6262
}
6363

64-
public String get(String api, HttpHeaders headers, RestParamBuilder paramBuilder) {
64+
public String get(String api, HttpHeaders headers, RestParam paramBuilder) {
6565
return get(api, headers,paramBuilder!=null?paramBuilder.toFormRequest():null);
6666
}
6767

68-
public String get(String api, RestParamBuilder paramBuilder) {
68+
public String get(String api, RestParam paramBuilder) {
6969
return get(api, httpHeaders, paramBuilder);
7070
}
7171

7272
public String get(String api) {
73-
return get(api, httpHeaders, (RestParamBuilder) null);
73+
return get(api, httpHeaders, (RestParam) null);
7474
}
7575

7676
public String get(String api, HttpHeaders headers) {
77-
return get(api, headers, (RestParamBuilder) null);
77+
return get(api, headers, (RestParam) null);
7878
}
7979

8080
public Request getGetRequest(String api, HttpHeaders headers, MultiValueMap<String, String> requestParams) {
@@ -89,7 +89,7 @@ public String post(String api, JSON requestBody) {
8989
return post(api, httpHeaders, requestBody);
9090
}
9191

92-
public String post(String api, RestParamBuilder paramBuilder) {
92+
public String post(String api, RestParam paramBuilder) {
9393
return post(api, httpHeaders, paramBuilder.toJsonRequest());
9494
}
9595

@@ -107,7 +107,7 @@ public String post(String api, HttpHeaders headers, JSON requestBody) {
107107
return emptyResponse;
108108
}
109109

110-
public String post(String api, HttpHeaders headers, RestParamBuilder paramBuilder) {
110+
public String post(String api, HttpHeaders headers, RestParam paramBuilder) {
111111
return post(api, headers, paramBuilder.toJsonRequest());
112112
}
113113

Diff for: ‎springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/SessionClient.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.codingapi.springboot.framework.rest;
22

33
import com.alibaba.fastjson.JSON;
4-
import com.codingapi.springboot.framework.rest.param.RestParamBuilder;
4+
import com.codingapi.springboot.framework.rest.param.RestParam;
55
import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties;
66
import lombok.Getter;
77
import org.springframework.http.HttpHeaders;
@@ -67,12 +67,12 @@ public SessionClient addHeader(String key, String value){
6767
return this;
6868
}
6969

70-
public String postForm(String url, RestParamBuilder restParam){
70+
public String postForm(String url, RestParam restParam){
7171
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
7272
return httpClient.post(url,httpHeaders,restParam.toFormRequest());
7373
}
7474

75-
public String postJson(String url, RestParamBuilder restParam){
75+
public String postJson(String url, RestParam restParam){
7676
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
7777
return httpClient.post(url,httpHeaders,restParam.toJsonRequest());
7878
}
@@ -90,12 +90,12 @@ public String getHtml(String url){
9090
return getHtml(url,null);
9191
}
9292

93-
public String getHtml(String url,RestParamBuilder restParam){
93+
public String getHtml(String url, RestParam restParam){
9494
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
9595
return httpClient.get(url,httpHeaders,restParam!=null?restParam.toFormRequest():null);
9696
}
9797

98-
public String getJson(String url,RestParamBuilder restParam){
98+
public String getJson(String url, RestParam restParam){
9999
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
100100
return httpClient.get(url,httpHeaders,restParam!=null?restParam.toFormRequest():null);
101101
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.codingapi.springboot.framework.rest.param;
2+
3+
public interface IRestParam {
4+
5+
default RestParam toParameters() {
6+
return RestParam.parser(this);
7+
}
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,75 @@
11
package com.codingapi.springboot.framework.rest.param;
22

3-
public interface RestParam {
3+
import com.alibaba.fastjson.JSONArray;
4+
import com.alibaba.fastjson.JSONObject;
5+
import org.springframework.util.LinkedMultiValueMap;
6+
import org.springframework.util.MultiValueMap;
47

5-
default RestParamBuilder toParameters() {
6-
return RestParamBuilder.parser(this);
8+
import java.net.URLEncoder;
9+
import java.nio.charset.StandardCharsets;
10+
11+
public class RestParam {
12+
13+
private final JSONObject jsonBody;
14+
private final MultiValueMap<String, String> mapBody;
15+
16+
private RestParam() {
17+
this.jsonBody = new JSONObject();
18+
this.mapBody = new LinkedMultiValueMap<>();
19+
}
20+
21+
public static RestParam create() {
22+
return new RestParam();
23+
}
24+
25+
public static RestParam parser(Object obj) {
26+
RestParam builder = create();
27+
JSONObject object = (JSONObject) JSONObject.toJSON(obj);
28+
fetch(object, builder);
29+
return builder;
30+
}
31+
32+
33+
private static void fetch(JSONObject object, RestParam builder) {
34+
for (String key : object.keySet()) {
35+
Object value = object.getObject(key, Object.class);
36+
if (value != null) {
37+
builder.add(key, value);
38+
39+
if (value instanceof JSONObject) {
40+
JSONObject jsonObject = (JSONObject) value;
41+
fetch(jsonObject, builder);
42+
}
43+
44+
if (value instanceof JSONArray jsonArray) {
45+
for (Object o : jsonArray) {
46+
if (o instanceof JSONObject jsonObject) {
47+
fetch(jsonObject, builder);
48+
}
49+
}
50+
}
51+
}
52+
}
53+
}
54+
55+
public JSONObject toJsonRequest() {
56+
return jsonBody;
57+
}
58+
59+
public MultiValueMap<String, String> toFormRequest() {
60+
return mapBody;
61+
}
62+
63+
public RestParam add(String key, Object value) {
64+
return add(key, value, true);
65+
}
66+
67+
public RestParam add(String key, Object value, boolean encode) {
68+
String stringValue = value.toString();
69+
String encodeValue = encode ? URLEncoder.encode(stringValue, StandardCharsets.UTF_8) : value.toString();
70+
jsonBody.put(key, value);
71+
mapBody.add(key, encodeValue);
72+
return this;
773
}
874

975
}

Diff for: ‎springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/param/RestParamBuilder.java

-63
This file was deleted.

Diff for: ‎springboot-starter/src/test/java/com/codingapi/springboot/framework/rest/RestClientTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.codingapi.springboot.framework.rest;
22

33
import com.alibaba.fastjson.JSONObject;
4-
import com.codingapi.springboot.framework.rest.param.RestParamBuilder;
4+
import com.codingapi.springboot.framework.rest.param.RestParam;
55
import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties;
66
import lombok.extern.slf4j.Slf4j;
77
import org.junit.jupiter.api.Test;
@@ -22,7 +22,7 @@ void okxTest() {
2222
proxyProperties.setProxyHost("127.0.0.1");
2323
proxyProperties.setProxyPort(7890);
2424
RestClient restClient = new RestClient(proxyProperties,baseUrl,5,"{}",null,null);
25-
String response = restClient.get("api/v5/market/candles", RestParamBuilder.create()
25+
String response = restClient.get("api/v5/market/candles", RestParam.create()
2626
.add("instId","BTC-USDT")
2727
.add("bar","1m")
2828
.add("limit","300")

0 commit comments

Comments
 (0)
Please sign in to comment.