diff --git a/pom.xml b/pom.xml index 65e11015..e863ed14 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ com.codingapi.springboot springboot-parent - 1.5.3 + 1.5.4 https://github.com/codingapi/springboot-framewrok springboot-parent diff --git a/springboot-starter-data-fast/pom.xml b/springboot-starter-data-fast/pom.xml index be63507d..81493be2 100644 --- a/springboot-starter-data-fast/pom.xml +++ b/springboot-starter-data-fast/pom.xml @@ -5,7 +5,7 @@ springboot-parent com.codingapi.springboot - 1.5.3 + 1.5.4 4.0.0 diff --git a/springboot-starter-id-generator/pom.xml b/springboot-starter-id-generator/pom.xml index 745d1eac..0a422559 100644 --- a/springboot-starter-id-generator/pom.xml +++ b/springboot-starter-id-generator/pom.xml @@ -5,7 +5,7 @@ springboot-parent com.codingapi.springboot - 1.5.3 + 1.5.4 4.0.0 diff --git a/springboot-starter-security-jwt/pom.xml b/springboot-starter-security-jwt/pom.xml index 509ec2a1..f022d91b 100644 --- a/springboot-starter-security-jwt/pom.xml +++ b/springboot-starter-security-jwt/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 1.5.3 + 1.5.4 springboot-starter-security-jwt diff --git a/springboot-starter/pom.xml b/springboot-starter/pom.xml index c856dbe1..0dcc08f7 100644 --- a/springboot-starter/pom.xml +++ b/springboot-starter/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot springboot-parent - 1.5.3 + 1.5.4 springboot-starter diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/HttpClient.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/HttpClient.java index 9e28d342..d508d197 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/HttpClient.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/HttpClient.java @@ -23,14 +23,20 @@ @Slf4j public class HttpClient { + public interface IHttpRequestHandler{ + String handler(HttpClient client,String uri,HttpMethod method,HttpHeaders headers,HttpEntity httpEntity); + } + public interface IHttpResponseHandler{ - String toResponse(HttpClient client,String uri,ResponseEntity response); + String handler(HttpClient client,String uri,ResponseEntity response); } private final RestTemplate restTemplate; private final IHttpResponseHandler responseHandler; + private final IHttpRequestHandler requestHandler; + private static final IHttpResponseHandler defaultResponseHandler = new IHttpResponseHandler() { public HttpHeaders copyHeaders(HttpHeaders headers){ @@ -42,7 +48,7 @@ public HttpHeaders copyHeaders(HttpHeaders headers){ } @Override - public String toResponse(HttpClient client, String url, ResponseEntity response) { + public String handler(HttpClient client, String url, ResponseEntity response) { if(response.getStatusCode().equals(HttpStatus.OK)){ return response.getBody(); } @@ -63,6 +69,14 @@ public String toResponse(HttpClient client, String url, ResponseEntity r } }; + private static final IHttpRequestHandler defaultRequestHandler = new IHttpRequestHandler() { + + @Override + public String handler(HttpClient client, String uri,HttpMethod method, HttpHeaders headers, HttpEntity httpEntity) { + return uri; + } + }; + private static final ResponseErrorHandler defaultErrorHandler = new DefaultResponseErrorHandler() { @Override public boolean hasError(ClientHttpResponse response) throws IOException { @@ -74,19 +88,20 @@ public boolean hasError(ClientHttpResponse response) throws IOException { }; public HttpClient() { - this(null,defaultResponseHandler); + this(null,defaultRequestHandler,defaultResponseHandler); } - public HttpClient(IHttpResponseHandler responseHandler) { - this(null,responseHandler); + public HttpClient(IHttpRequestHandler requestHandler,IHttpResponseHandler responseHandler) { + this(null,requestHandler,responseHandler); } public HttpClient(HttpProxyProperties properties) { - this(properties,defaultResponseHandler); + this(properties,defaultRequestHandler,defaultResponseHandler); } - public HttpClient(HttpProxyProperties properties,IHttpResponseHandler responseHandler) { - this.responseHandler = responseHandler; + public HttpClient(HttpProxyProperties properties,IHttpRequestHandler requestHandler,IHttpResponseHandler responseHandler) { + this.requestHandler = requestHandler==null?defaultRequestHandler:requestHandler; + this.responseHandler = responseHandler==null?defaultResponseHandler:responseHandler; this.restTemplate = RestTemplateContext.getInstance().getRestTemplate(); SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(3000); @@ -106,14 +121,16 @@ public HttpClient(HttpProxyProperties properties,IHttpResponseHandler responseHa public String post(String url, HttpHeaders headers, JSON jsonObject) { HttpEntity httpEntity = new HttpEntity<>(jsonObject.toString(SerializerFeature.WriteMapNullValue), headers); - ResponseEntity httpResponse = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class); - return responseHandler.toResponse(this,url,httpResponse); + String requestUrl = requestHandler.handler(this,url, HttpMethod.POST,headers,httpEntity); + ResponseEntity httpResponse = restTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class); + return responseHandler.handler(this,url,httpResponse); } public String post(String url, HttpHeaders headers, MultiValueMap formData) { HttpEntity> httpEntity = new HttpEntity<>(formData, headers); - ResponseEntity httpResponse = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class); - return responseHandler.toResponse(this,url,httpResponse); + String requestUrl = requestHandler.handler(this,url ,HttpMethod.POST,headers,httpEntity); + ResponseEntity httpResponse = restTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class); + return responseHandler.handler(this,url,httpResponse); } public String get(String url, HttpHeaders headers, MultiValueMap uriVariables) { @@ -123,11 +140,13 @@ public String get(String url, HttpHeaders headers, MultiValueMap URI uri = UriComponentsBuilder.fromHttpUrl(url) .queryParams(uriVariables) .build(true).toUri(); - httpResponse = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, String.class); + String requestUrl = requestHandler.handler(this,uri.toString(), HttpMethod.GET,headers,httpEntity); + httpResponse = restTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class); }else{ - httpResponse = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class); + String requestUrl = requestHandler.handler(this,url, HttpMethod.GET,headers,httpEntity); + httpResponse = restTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class); } - return responseHandler.toResponse(this, url, httpResponse); + return responseHandler.handler(this, url, httpResponse); } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/RestClient.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/RestClient.java index ad09d92d..0f15cf7a 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/RestClient.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/RestClient.java @@ -1,11 +1,12 @@ package com.codingapi.springboot.framework.rest; -import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.JSON; import com.codingapi.springboot.framework.rest.param.RestParamBuilder; import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; +import org.springframework.util.MultiValueMap; @Slf4j public class RestClient { @@ -20,17 +21,22 @@ public class RestClient { private final String baseUrl; - public RestClient(HttpProxyProperties httpProxyProperties, String baseUrl,int retryCount,String emptyResponse) { + public RestClient(HttpProxyProperties httpProxyProperties, + String baseUrl, + int retryCount, + String emptyResponse, + HttpClient.IHttpRequestHandler requestHandler, + HttpClient.IHttpResponseHandler responseHandler) { this.baseUrl = baseUrl; this.retryCount = retryCount; - this.httpClient = new HttpClient(httpProxyProperties); + this.httpClient = new HttpClient(httpProxyProperties,requestHandler,responseHandler); this.httpHeaders = new HttpHeaders(); this.emptyResponse = emptyResponse; this.initHeaders(); } public RestClient(String baseUrl) { - this(null, baseUrl,5,"{}"); + this(null, baseUrl,5,"{}",null,null); } private void initHeaders() { @@ -41,14 +47,10 @@ private String toUrl(String api) { return baseUrl + api; } - private String _get(String api, HttpHeaders headers, RestParamBuilder paramBuilder) { - return httpClient.get(toUrl(api), headers, paramBuilder != null ? paramBuilder.toFormRequest() : null); - } - - public String get(String api, HttpHeaders headers, RestParamBuilder paramBuilder) { + public String get(String api, HttpHeaders headers, MultiValueMap requestParams) { for (int i = 0; i < retryCount; i++) { try { - return _get(api, headers, paramBuilder); + return httpClient.get(toUrl(api), headers, requestParams); } catch (Exception e) { log.warn("api:{},error:{}", api, e.getMessage()); sleep(); @@ -57,23 +59,27 @@ public String get(String api, HttpHeaders headers, RestParamBuilder paramBuilder return emptyResponse; } + public String get(String api, HttpHeaders headers, RestParamBuilder paramBuilder) { + return get(api, headers,paramBuilder!=null?paramBuilder.toFormRequest():null); + } + public String get(String api, RestParamBuilder paramBuilder) { return get(api, httpHeaders, paramBuilder); } public String get(String api) { - return get(api, httpHeaders, null); + return get(api, httpHeaders, (RestParamBuilder) null); } public String get(String api, HttpHeaders headers) { - return get(api, headers, null); + return get(api, headers, (RestParamBuilder) null); } - private String _post(String api, HttpHeaders headers, JSONObject requestBody) { + private String _post(String api, HttpHeaders headers, JSON requestBody) { return httpClient.post(toUrl(api), headers, requestBody); } - public String post(String api, JSONObject requestBody) { + public String post(String api, JSON requestBody) { return post(api, httpHeaders, requestBody); } @@ -81,7 +87,7 @@ public String post(String api, RestParamBuilder paramBuilder) { return post(api, httpHeaders, paramBuilder.toJsonRequest()); } - public String post(String api, HttpHeaders headers, JSONObject requestBody) { + public String post(String api, HttpHeaders headers, JSON requestBody) { for (int i = 0; i < retryCount; i++) { try { return _post(api, headers, requestBody); diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/SessionClient.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/SessionClient.java index b6c354cb..eea1eb49 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/SessionClient.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/rest/SessionClient.java @@ -1,6 +1,6 @@ package com.codingapi.springboot.framework.rest; -import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.JSON; import com.codingapi.springboot.framework.rest.param.RestParamBuilder; import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties; import lombok.Getter; @@ -34,7 +34,7 @@ public HttpHeaders copyHeaders(HttpHeaders headers) { } @Override - public String toResponse(HttpClient client, String url, ResponseEntity response) { + public String handler(HttpClient client, String url, ResponseEntity response) { if (response.getStatusCode().equals(HttpStatus.OK)) { return response.getBody(); } @@ -54,7 +54,7 @@ public String toResponse(HttpClient client, String url, ResponseEntity r return response.getBody(); } }; - this.httpClient = new HttpClient(properties, responseHandler); + this.httpClient = new HttpClient(properties, null,responseHandler); this.httpHeaders = new HttpHeaders(); } @@ -77,7 +77,7 @@ public String postJson(String url, RestParamBuilder restParam){ return httpClient.post(url,httpHeaders,restParam.toJsonRequest()); } - public String postJson(String url, JSONObject requestBody){ + public String postJson(String url, JSON requestBody){ httpHeaders.setContentType(MediaType.APPLICATION_JSON); return httpClient.post(url,httpHeaders,requestBody); }