Skip to content

Commit 69dfbfd

Browse files
committed
update Rest
1 parent d2f2465 commit 69dfbfd

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

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

+24-24
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
import com.codingapi.springboot.framework.rest.properties.HttpProxyProperties;
55
import lombok.extern.slf4j.Slf4j;
66
import org.springframework.http.*;
7+
import org.springframework.http.client.ClientHttpResponse;
78
import org.springframework.http.client.SimpleClientHttpRequestFactory;
89
import org.springframework.util.MultiValueMap;
10+
import org.springframework.web.client.DefaultResponseErrorHandler;
11+
import org.springframework.web.client.ResponseErrorHandler;
912
import org.springframework.web.client.RestTemplate;
1013
import org.springframework.web.util.UriComponentsBuilder;
1114

15+
import java.io.IOException;
1216
import java.net.InetSocketAddress;
1317
import java.net.Proxy;
1418
import java.net.URI;
@@ -40,17 +44,32 @@ public String toResponse(HttpClient client, URI uri, ResponseEntity<String> resp
4044
if(response.getStatusCode().equals(HttpStatus.OK)){
4145
return response.getBody();
4246
}
47+
48+
if(response.getStatusCode().equals(HttpStatus.NOT_FOUND)){
49+
return response.getBody();
50+
}
51+
4352
if(response.getStatusCode().equals(HttpStatus.FOUND)){
4453
HttpHeaders headers = response.getHeaders();
4554
String location = Objects.requireNonNull(headers.getLocation()).toString();
4655
String baseUrl = uri.getScheme() + "://" + uri.getHost()+":"+uri.getPort();
4756
String url = baseUrl+location;
48-
return client.get(url,copyHeaders(headers));
57+
return client.get(url,copyHeaders(headers),null);
4958
}
5059
return response.getBody();
5160
}
5261
};
5362

63+
private static final ResponseErrorHandler defaultErrorHandler = new DefaultResponseErrorHandler() {
64+
@Override
65+
public boolean hasError(ClientHttpResponse response) throws IOException {
66+
if(response.getStatusCode()==HttpStatus.NOT_FOUND){
67+
return false;
68+
}
69+
return super.hasError(response);
70+
}
71+
};
72+
5473
public HttpClient() {
5574
this(null,defaultResponseHandler);
5675
}
@@ -75,26 +94,20 @@ public HttpClient(HttpProxyProperties properties,IHttpResponseHandler responseHa
7594
new InetSocketAddress(properties.getProxyHost(), properties.getProxyPort())));
7695
}
7796
}
97+
this.restTemplate.setErrorHandler(defaultErrorHandler);
7898
this.restTemplate.setRequestFactory(requestFactory);
7999
}
80100

81-
public String post(String url, JSON jsonObject) {
82-
return post(url, new HttpHeaders(), jsonObject);
83-
}
84-
85101
public String post(String url, HttpHeaders headers, JSON jsonObject) {
86-
headers.setContentType(MediaType.APPLICATION_JSON);
87102
HttpEntity<String> httpEntity = new HttpEntity<>(jsonObject.toString(), headers);
103+
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(url);
104+
URI uri = uriComponentsBuilder.build().toUri();
88105
ResponseEntity<String> httpResponse = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
89-
return httpResponse.getBody();
106+
return responseHandler.toResponse(this,uri,httpResponse);
90107
}
91108

92-
public String post(String url, MultiValueMap<String, String> formData) {
93-
return post(url,new HttpHeaders(),formData);
94-
}
95109

96110
public String post(String url, HttpHeaders headers, MultiValueMap<String, String> formData) {
97-
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
98111
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(formData, headers);
99112
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(url);
100113
URI uri = uriComponentsBuilder.build().toUri();
@@ -103,7 +116,6 @@ public String post(String url, HttpHeaders headers, MultiValueMap<String, String
103116
}
104117

105118
public String get(String url, HttpHeaders headers, MultiValueMap<String, String> uriVariables) {
106-
headers.setContentType(MediaType.APPLICATION_JSON);
107119
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(url);
108120
if (uriVariables != null) {
109121
uriComponentsBuilder = uriComponentsBuilder.queryParams(uriVariables);
@@ -115,16 +127,4 @@ public String get(String url, HttpHeaders headers, MultiValueMap<String, String>
115127
}
116128

117129

118-
public String get(String url, MultiValueMap<String, String> uriVariables) {
119-
return get(url, new HttpHeaders(), uriVariables);
120-
}
121-
122-
public String get(String url, HttpHeaders headers) {
123-
return get(url,headers,null);
124-
}
125-
126-
public String get(String url) {
127-
return get(url, new HttpHeaders(), null);
128-
}
129-
130130
}

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ public String toResponse(HttpClient client, URI uri, ResponseEntity<String> resp
3030
if (response.getStatusCode().equals(HttpStatus.OK)) {
3131
return response.getBody();
3232
}
33+
34+
if(response.getStatusCode().equals(HttpStatus.NOT_FOUND)){
35+
return response.getBody();
36+
}
37+
3338
if (response.getStatusCode().equals(HttpStatus.FOUND)) {
3439
HttpHeaders headers = response.getHeaders();
3540
String location = Objects.requireNonNull(headers.getLocation()).toString();
3641
String baseUrl = uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort();
3742
String url = baseUrl + location;
38-
return client.get(url, copyHeaders(headers));
43+
return client.get(url, copyHeaders(headers),null);
3944
}
4045
return response.getBody();
4146
}
@@ -58,7 +63,7 @@ public String post(String url, RestParamBuilder restParam){
5863
}
5964

6065
public String get(String url){
61-
return httpClient.get(url,httpHeaders);
66+
return httpClient.get(url,httpHeaders,null);
6267
}
6368

6469
}

0 commit comments

Comments
 (0)