Skip to content

Commit a6fe782

Browse files
committed
add rest client.
add Arithmetic.parse(string) method
1 parent ac71d93 commit a6fe782

File tree

15 files changed

+346
-5
lines changed

15 files changed

+346
-5
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.4.8</version>
15+
<version>1.4.9.dev</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.4.8</version>
8+
<version>1.4.9.dev</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.4.8</version>
8+
<version>1.4.9.dev</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.4.8</version>
9+
<version>1.4.9.dev</version>
1010
</parent>
1111

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

springboot-starter/pom.xml

+12-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.4.8</version>
8+
<version>1.4.9.dev</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

@@ -21,6 +21,17 @@
2121
<artifactId>spring-data-commons</artifactId>
2222
</dependency>
2323

24+
<dependency>
25+
<groupId>org.springframework</groupId>
26+
<artifactId>spring-web</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>commons-beanutils</groupId>
31+
<artifactId>commons-beanutils</artifactId>
32+
<version>1.9.4</version>
33+
</dependency>
34+
2435
<dependency>
2536
<groupId>com.alibaba</groupId>
2637
<artifactId>fastjson</artifactId>

springboot-starter/src/main/java/com/codingapi/springboot/framework/math/Arithmetic.java

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public static Arithmetic parse(long value){
3636
return new Arithmetic(value);
3737
}
3838

39+
public static Arithmetic parse(String value){
40+
return new Arithmetic(value);
41+
}
42+
3943

4044
public Arithmetic(String value) {
4145
this.value = new BigDecimal(value);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.codingapi.springboot.framework.rest;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.codingapi.springboot.framework.rest.properties.RestApiProperties;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.http.*;
7+
import org.springframework.http.client.SimpleClientHttpRequestFactory;
8+
import org.springframework.util.MultiValueMap;
9+
import org.springframework.web.client.RestTemplate;
10+
import org.springframework.web.util.UriComponentsBuilder;
11+
12+
import java.net.InetSocketAddress;
13+
import java.net.Proxy;
14+
import java.net.URI;
15+
16+
@Slf4j
17+
class HttpClient {
18+
19+
private final RestTemplate restTemplate;
20+
21+
private final String baseUrl;
22+
23+
public HttpClient(RestApiProperties properties, String baseUrl) {
24+
this.baseUrl = baseUrl;
25+
this.restTemplate = RestTemplateContext.getInstance().getRestTemplate();
26+
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
27+
requestFactory.setConnectTimeout(3000);
28+
if(properties!=null) {
29+
if (properties.isEnableProxy()) {
30+
log.info("enable proxy {}//:{}:{}", properties.getProxyType(), properties.getProxyHost(), properties.getProxyPort());
31+
requestFactory.setProxy(new Proxy(properties.getProxyType(),
32+
new InetSocketAddress(properties.getProxyHost(), properties.getProxyPort())));
33+
}
34+
}
35+
this.restTemplate.setRequestFactory(requestFactory);
36+
}
37+
38+
39+
private String buildUrl(String api) {
40+
return baseUrl+api;
41+
}
42+
43+
public String post(String api, JSON jsonObject) {
44+
HttpHeaders headers = new HttpHeaders();
45+
headers.setContentType(MediaType.APPLICATION_JSON);
46+
String url = buildUrl(api);
47+
HttpEntity<String> httpEntity = new HttpEntity<>(jsonObject.toString(), headers);
48+
ResponseEntity<String> httpResponse = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
49+
return httpResponse.getBody();
50+
}
51+
52+
53+
public String get(String api, MultiValueMap<String, String> uriVariables) {
54+
HttpHeaders headers = new HttpHeaders();
55+
headers.setContentType(MediaType.APPLICATION_JSON);
56+
String url = buildUrl(api);
57+
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(url);
58+
if(uriVariables!=null){
59+
uriComponentsBuilder = uriComponentsBuilder.queryParams(uriVariables);
60+
}
61+
URI uri = uriComponentsBuilder.build().toUri();
62+
HttpEntity<String> httpEntity = new HttpEntity<>(headers);
63+
ResponseEntity<String> httpResponse = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, String.class);
64+
return httpResponse.getBody();
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.codingapi.springboot.framework.rest;
2+
3+
import com.codingapi.springboot.framework.rest.param.ApiGetParamBuilder;
4+
import com.codingapi.springboot.framework.rest.param.ApiPostParamBuilder;
5+
import com.codingapi.springboot.framework.rest.properties.RestApiProperties;
6+
import lombok.extern.slf4j.Slf4j;
7+
8+
@Slf4j
9+
public class RestClient {
10+
11+
private final HttpClient httpClient;
12+
13+
private final static int RETRY_COUNT = 5;
14+
15+
private final static String EMPTY = "{}";
16+
17+
public RestClient(RestApiProperties restApiProperties, String baseUrl) {
18+
this.httpClient = new HttpClient(restApiProperties,baseUrl);
19+
}
20+
21+
public RestClient(String baseUrl) {
22+
this.httpClient = new HttpClient(null,baseUrl);
23+
}
24+
25+
private String _get(String api, ApiGetParamBuilder paramBuilder) {
26+
return httpClient.get(api, paramBuilder!=null?paramBuilder.build():null);
27+
}
28+
29+
public String get(String api, ApiGetParamBuilder paramBuilder) {
30+
for(int i=0; i< RETRY_COUNT; i++){
31+
try {
32+
return _get(api, paramBuilder);
33+
}catch (Exception e){
34+
log.warn("api:{},error:{}",api,e.getMessage());
35+
sleep();
36+
}
37+
}
38+
return EMPTY;
39+
}
40+
41+
public String get(String api) {
42+
return get(api,null);
43+
}
44+
45+
private String _post(String api, ApiPostParamBuilder paramBuilder) {
46+
return httpClient.post(api, paramBuilder.build());
47+
}
48+
49+
50+
public String post(String api, ApiPostParamBuilder paramBuilder) {
51+
for (int i=0;i< RETRY_COUNT;i++){
52+
try {
53+
return _post(api, paramBuilder);
54+
}catch (Exception e){
55+
log.warn("api:{},error:{}",api,e.getMessage());
56+
sleep();
57+
}
58+
}
59+
return EMPTY;
60+
}
61+
62+
63+
private void sleep(){
64+
try {
65+
Thread.sleep(1000);
66+
} catch (InterruptedException ex) {
67+
throw new RuntimeException(ex);
68+
}
69+
}
70+
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.codingapi.springboot.framework.rest;
2+
3+
4+
import com.codingapi.springboot.framework.utils.TrustAnyHttpClientFactory;
5+
import lombok.Getter;
6+
import org.springframework.http.client.ClientHttpRequestFactory;
7+
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
8+
import org.springframework.web.client.RestTemplate;
9+
10+
class RestTemplateContext {
11+
12+
private static RestTemplateContext instance;
13+
@Getter
14+
private final RestTemplate restTemplate;
15+
16+
private RestTemplateContext(){
17+
this.restTemplate = restTemplate(new HttpComponentsClientHttpRequestFactory(
18+
TrustAnyHttpClientFactory.createTrustAnyHttpClient()));
19+
}
20+
21+
public static RestTemplateContext getInstance() {
22+
if (instance == null) {
23+
synchronized (RestTemplateContext.class) {
24+
if (instance == null) {
25+
try {
26+
instance = new RestTemplateContext();
27+
} catch (Exception e) {
28+
throw new RuntimeException(e);
29+
}
30+
}
31+
}
32+
}
33+
return instance;
34+
}
35+
36+
RestTemplate restTemplate(ClientHttpRequestFactory factory) {
37+
return new RestTemplate(factory);
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.codingapi.springboot.framework.rest.param;
2+
3+
public interface ApiGetParam {
4+
5+
default ApiGetParamBuilder getParameters() {
6+
return ApiGetParamBuilder.parser(this);
7+
}
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.codingapi.springboot.framework.rest.param;
2+
3+
import lombok.SneakyThrows;
4+
import org.apache.commons.beanutils.BeanUtils;
5+
import org.apache.commons.beanutils.PropertyUtils;
6+
import org.springframework.util.LinkedMultiValueMap;
7+
import org.springframework.util.MultiValueMap;
8+
9+
import java.beans.PropertyDescriptor;
10+
11+
public class ApiGetParamBuilder {
12+
13+
private final MultiValueMap<String, String> uriVariables;
14+
15+
private ApiGetParamBuilder() {
16+
this.uriVariables = new LinkedMultiValueMap<>();
17+
}
18+
19+
public static ApiGetParamBuilder create() {
20+
return new ApiGetParamBuilder();
21+
}
22+
23+
@SneakyThrows
24+
public static ApiGetParamBuilder parser(Object obj) {
25+
PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(obj.getClass());
26+
ApiGetParamBuilder builder = create();
27+
for (PropertyDescriptor descriptor : descriptors) {
28+
String name = descriptor.getName();
29+
;
30+
String value = BeanUtils.getProperty(obj, name);
31+
if (value != null) {
32+
builder.add(name, value);
33+
}
34+
}
35+
return builder;
36+
}
37+
38+
public MultiValueMap<String, String> build() {
39+
return uriVariables;
40+
}
41+
42+
public ApiGetParamBuilder add(String key, String value) {
43+
uriVariables.add(key, value);
44+
return this;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.codingapi.springboot.framework.rest.param;
2+
3+
public interface ApiPostParam {
4+
5+
default ApiPostParamBuilder getParameters() {
6+
return ApiPostParamBuilder.parser(this);
7+
}
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.codingapi.springboot.framework.rest.param;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import lombok.SneakyThrows;
5+
import org.apache.commons.beanutils.PropertyUtils;
6+
7+
import java.beans.PropertyDescriptor;
8+
9+
public class ApiPostParamBuilder {
10+
11+
private final JSONObject jsonObject;
12+
13+
private ApiPostParamBuilder() {
14+
this.jsonObject = new JSONObject();
15+
}
16+
17+
public static ApiPostParamBuilder create() {
18+
return new ApiPostParamBuilder();
19+
}
20+
21+
@SneakyThrows
22+
public static ApiPostParamBuilder parser(Object obj) {
23+
PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(obj.getClass());
24+
ApiPostParamBuilder builder = create();
25+
for (PropertyDescriptor descriptor : descriptors) {
26+
String name = descriptor.getName();
27+
Object value = PropertyUtils.getProperty(obj, name);
28+
if (value != null) {
29+
builder.add(name, value);
30+
}
31+
}
32+
return builder;
33+
}
34+
35+
public JSONObject build() {
36+
return jsonObject;
37+
}
38+
39+
public ApiPostParamBuilder add(String key, Object value) {
40+
jsonObject.put(key, value);
41+
return this;
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codingapi.springboot.framework.rest.properties;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
import java.net.Proxy;
7+
8+
@Setter
9+
@Getter
10+
public class RestApiProperties {
11+
12+
private boolean enableProxy;
13+
private String proxyHost;
14+
private int proxyPort;
15+
private Proxy.Type proxyType;
16+
17+
}

0 commit comments

Comments
 (0)