Skip to content

Commit beb2b08

Browse files
authored
Merge pull request #21 from codingapi/dev
fix rest
2 parents 20fabd3 + d9979ac commit beb2b08

File tree

8 files changed

+64
-39
lines changed

8 files changed

+64
-39
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<groupId>com.codingapi.springboot</groupId>
1414
<artifactId>springboot-parent</artifactId>
15-
<version>1.5.3</version>
15+
<version>1.5.4</version>
1616

1717
<url>https://github.com/codingapi/springboot-framewrok</url>
1818
<name>springboot-parent</name>

springboot-starter-data-fast/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>1.5.3</version>
8+
<version>1.5.4</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-id-generator/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>1.5.3</version>
8+
<version>1.5.4</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-security-jwt/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>1.5.3</version>
9+
<version>1.5.4</version>
1010
</parent>
1111

1212
<artifactId>springboot-starter-security-jwt</artifactId>

springboot-starter/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.codingapi.springboot</groupId>
77
<artifactId>springboot-parent</artifactId>
8-
<version>1.5.3</version>
8+
<version>1.5.4</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/HttpClient.java

+34-15
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,20 @@
2323
@Slf4j
2424
public class HttpClient {
2525

26+
public interface IHttpRequestHandler{
27+
String handler(HttpClient client,String uri,HttpMethod method,HttpHeaders headers,HttpEntity<?> httpEntity);
28+
}
29+
2630
public interface IHttpResponseHandler{
27-
String toResponse(HttpClient client,String uri,ResponseEntity<String> response);
31+
String handler(HttpClient client,String uri,ResponseEntity<String> response);
2832
}
2933

3034
private final RestTemplate restTemplate;
3135

3236
private final IHttpResponseHandler responseHandler;
3337

38+
private final IHttpRequestHandler requestHandler;
39+
3440
private static final IHttpResponseHandler defaultResponseHandler = new IHttpResponseHandler() {
3541

3642
public HttpHeaders copyHeaders(HttpHeaders headers){
@@ -42,7 +48,7 @@ public HttpHeaders copyHeaders(HttpHeaders headers){
4248
}
4349

4450
@Override
45-
public String toResponse(HttpClient client, String url, ResponseEntity<String> response) {
51+
public String handler(HttpClient client, String url, ResponseEntity<String> response) {
4652
if(response.getStatusCode().equals(HttpStatus.OK)){
4753
return response.getBody();
4854
}
@@ -63,6 +69,14 @@ public String toResponse(HttpClient client, String url, ResponseEntity<String> r
6369
}
6470
};
6571

72+
private static final IHttpRequestHandler defaultRequestHandler = new IHttpRequestHandler() {
73+
74+
@Override
75+
public String handler(HttpClient client, String uri,HttpMethod method, HttpHeaders headers, HttpEntity<?> httpEntity) {
76+
return uri;
77+
}
78+
};
79+
6680
private static final ResponseErrorHandler defaultErrorHandler = new DefaultResponseErrorHandler() {
6781
@Override
6882
public boolean hasError(ClientHttpResponse response) throws IOException {
@@ -74,19 +88,20 @@ public boolean hasError(ClientHttpResponse response) throws IOException {
7488
};
7589

7690
public HttpClient() {
77-
this(null,defaultResponseHandler);
91+
this(null,defaultRequestHandler,defaultResponseHandler);
7892
}
7993

80-
public HttpClient(IHttpResponseHandler responseHandler) {
81-
this(null,responseHandler);
94+
public HttpClient(IHttpRequestHandler requestHandler,IHttpResponseHandler responseHandler) {
95+
this(null,requestHandler,responseHandler);
8296
}
8397

8498
public HttpClient(HttpProxyProperties properties) {
85-
this(properties,defaultResponseHandler);
99+
this(properties,defaultRequestHandler,defaultResponseHandler);
86100
}
87101

88-
public HttpClient(HttpProxyProperties properties,IHttpResponseHandler responseHandler) {
89-
this.responseHandler = responseHandler;
102+
public HttpClient(HttpProxyProperties properties,IHttpRequestHandler requestHandler,IHttpResponseHandler responseHandler) {
103+
this.requestHandler = requestHandler==null?defaultRequestHandler:requestHandler;
104+
this.responseHandler = responseHandler==null?defaultResponseHandler:responseHandler;
90105
this.restTemplate = RestTemplateContext.getInstance().getRestTemplate();
91106
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
92107
requestFactory.setConnectTimeout(3000);
@@ -106,14 +121,16 @@ public HttpClient(HttpProxyProperties properties,IHttpResponseHandler responseHa
106121

107122
public String post(String url, HttpHeaders headers, JSON jsonObject) {
108123
HttpEntity<String> httpEntity = new HttpEntity<>(jsonObject.toString(SerializerFeature.WriteMapNullValue), headers);
109-
ResponseEntity<String> httpResponse = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
110-
return responseHandler.toResponse(this,url,httpResponse);
124+
String requestUrl = requestHandler.handler(this,url, HttpMethod.POST,headers,httpEntity);
125+
ResponseEntity<String> httpResponse = restTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
126+
return responseHandler.handler(this,url,httpResponse);
111127
}
112128

113129
public String post(String url, HttpHeaders headers, MultiValueMap<String, String> formData) {
114130
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(formData, headers);
115-
ResponseEntity<String> httpResponse = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
116-
return responseHandler.toResponse(this,url,httpResponse);
131+
String requestUrl = requestHandler.handler(this,url ,HttpMethod.POST,headers,httpEntity);
132+
ResponseEntity<String> httpResponse = restTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
133+
return responseHandler.handler(this,url,httpResponse);
117134
}
118135

119136
public String get(String url, HttpHeaders headers, MultiValueMap<String, String> uriVariables) {
@@ -123,11 +140,13 @@ public String get(String url, HttpHeaders headers, MultiValueMap<String, String>
123140
URI uri = UriComponentsBuilder.fromHttpUrl(url)
124141
.queryParams(uriVariables)
125142
.build(true).toUri();
126-
httpResponse = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, String.class);
143+
String requestUrl = requestHandler.handler(this,uri.toString(), HttpMethod.GET,headers,httpEntity);
144+
httpResponse = restTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class);
127145
}else{
128-
httpResponse = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
146+
String requestUrl = requestHandler.handler(this,url, HttpMethod.GET,headers,httpEntity);
147+
httpResponse = restTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class);
129148
}
130-
return responseHandler.toResponse(this, url, httpResponse);
149+
return responseHandler.handler(this, url, httpResponse);
131150
}
132151

133152

springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/RestClient.java

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

3-
import com.alibaba.fastjson.JSONObject;
3+
import com.alibaba.fastjson.JSON;
44
import com.codingapi.springboot.framework.rest.param.RestParamBuilder;
55
import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties;
66
import lombok.extern.slf4j.Slf4j;
77
import org.springframework.http.HttpHeaders;
88
import org.springframework.http.MediaType;
9+
import org.springframework.util.MultiValueMap;
910

1011
@Slf4j
1112
public class RestClient {
@@ -20,17 +21,22 @@ public class RestClient {
2021

2122
private final String baseUrl;
2223

23-
public RestClient(HttpProxyProperties httpProxyProperties, String baseUrl,int retryCount,String emptyResponse) {
24+
public RestClient(HttpProxyProperties httpProxyProperties,
25+
String baseUrl,
26+
int retryCount,
27+
String emptyResponse,
28+
HttpClient.IHttpRequestHandler requestHandler,
29+
HttpClient.IHttpResponseHandler responseHandler) {
2430
this.baseUrl = baseUrl;
2531
this.retryCount = retryCount;
26-
this.httpClient = new HttpClient(httpProxyProperties);
32+
this.httpClient = new HttpClient(httpProxyProperties,requestHandler,responseHandler);
2733
this.httpHeaders = new HttpHeaders();
2834
this.emptyResponse = emptyResponse;
2935
this.initHeaders();
3036
}
3137

3238
public RestClient(String baseUrl) {
33-
this(null, baseUrl,5,"{}");
39+
this(null, baseUrl,5,"{}",null,null);
3440
}
3541

3642
private void initHeaders() {
@@ -41,14 +47,10 @@ private String toUrl(String api) {
4147
return baseUrl + api;
4248
}
4349

44-
private String _get(String api, HttpHeaders headers, RestParamBuilder paramBuilder) {
45-
return httpClient.get(toUrl(api), headers, paramBuilder != null ? paramBuilder.toFormRequest() : null);
46-
}
47-
48-
public String get(String api, HttpHeaders headers, RestParamBuilder paramBuilder) {
50+
public String get(String api, HttpHeaders headers, MultiValueMap<String, String> requestParams) {
4951
for (int i = 0; i < retryCount; i++) {
5052
try {
51-
return _get(api, headers, paramBuilder);
53+
return httpClient.get(toUrl(api), headers, requestParams);
5254
} catch (Exception e) {
5355
log.warn("api:{},error:{}", api, e.getMessage());
5456
sleep();
@@ -57,31 +59,35 @@ public String get(String api, HttpHeaders headers, RestParamBuilder paramBuilder
5759
return emptyResponse;
5860
}
5961

62+
public String get(String api, HttpHeaders headers, RestParamBuilder paramBuilder) {
63+
return get(api, headers,paramBuilder!=null?paramBuilder.toFormRequest():null);
64+
}
65+
6066
public String get(String api, RestParamBuilder paramBuilder) {
6167
return get(api, httpHeaders, paramBuilder);
6268
}
6369

6470
public String get(String api) {
65-
return get(api, httpHeaders, null);
71+
return get(api, httpHeaders, (RestParamBuilder) null);
6672
}
6773

6874
public String get(String api, HttpHeaders headers) {
69-
return get(api, headers, null);
75+
return get(api, headers, (RestParamBuilder) null);
7076
}
7177

72-
private String _post(String api, HttpHeaders headers, JSONObject requestBody) {
78+
private String _post(String api, HttpHeaders headers, JSON requestBody) {
7379
return httpClient.post(toUrl(api), headers, requestBody);
7480
}
7581

76-
public String post(String api, JSONObject requestBody) {
82+
public String post(String api, JSON requestBody) {
7783
return post(api, httpHeaders, requestBody);
7884
}
7985

8086
public String post(String api, RestParamBuilder paramBuilder) {
8187
return post(api, httpHeaders, paramBuilder.toJsonRequest());
8288
}
8389

84-
public String post(String api, HttpHeaders headers, JSONObject requestBody) {
90+
public String post(String api, HttpHeaders headers, JSON requestBody) {
8591
for (int i = 0; i < retryCount; i++) {
8692
try {
8793
return _post(api, headers, requestBody);

springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/SessionClient.java

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

3-
import com.alibaba.fastjson.JSONObject;
3+
import com.alibaba.fastjson.JSON;
44
import com.codingapi.springboot.framework.rest.param.RestParamBuilder;
55
import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties;
66
import lombok.Getter;
@@ -34,7 +34,7 @@ public HttpHeaders copyHeaders(HttpHeaders headers) {
3434
}
3535

3636
@Override
37-
public String toResponse(HttpClient client, String url, ResponseEntity<String> response) {
37+
public String handler(HttpClient client, String url, ResponseEntity<String> response) {
3838
if (response.getStatusCode().equals(HttpStatus.OK)) {
3939
return response.getBody();
4040
}
@@ -54,7 +54,7 @@ public String toResponse(HttpClient client, String url, ResponseEntity<String> r
5454
return response.getBody();
5555
}
5656
};
57-
this.httpClient = new HttpClient(properties, responseHandler);
57+
this.httpClient = new HttpClient(properties, null,responseHandler);
5858
this.httpHeaders = new HttpHeaders();
5959
}
6060

@@ -77,7 +77,7 @@ public String postJson(String url, RestParamBuilder restParam){
7777
return httpClient.post(url,httpHeaders,restParam.toJsonRequest());
7878
}
7979

80-
public String postJson(String url, JSONObject requestBody){
80+
public String postJson(String url, JSON requestBody){
8181
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
8282
return httpClient.post(url,httpHeaders,requestBody);
8383
}

0 commit comments

Comments
 (0)