4
4
import com .codingapi .springboot .framework .rest .properties .HttpProxyProperties ;
5
5
import lombok .extern .slf4j .Slf4j ;
6
6
import org .springframework .http .*;
7
+ import org .springframework .http .client .ClientHttpResponse ;
7
8
import org .springframework .http .client .SimpleClientHttpRequestFactory ;
8
9
import org .springframework .util .MultiValueMap ;
10
+ import org .springframework .web .client .DefaultResponseErrorHandler ;
11
+ import org .springframework .web .client .ResponseErrorHandler ;
9
12
import org .springframework .web .client .RestTemplate ;
10
13
import org .springframework .web .util .UriComponentsBuilder ;
11
14
15
+ import java .io .IOException ;
12
16
import java .net .InetSocketAddress ;
13
17
import java .net .Proxy ;
14
18
import java .net .URI ;
@@ -40,17 +44,32 @@ public String toResponse(HttpClient client, URI uri, ResponseEntity<String> resp
40
44
if (response .getStatusCode ().equals (HttpStatus .OK )){
41
45
return response .getBody ();
42
46
}
47
+
48
+ if (response .getStatusCode ().equals (HttpStatus .NOT_FOUND )){
49
+ return response .getBody ();
50
+ }
51
+
43
52
if (response .getStatusCode ().equals (HttpStatus .FOUND )){
44
53
HttpHeaders headers = response .getHeaders ();
45
54
String location = Objects .requireNonNull (headers .getLocation ()).toString ();
46
55
String baseUrl = uri .getScheme () + "://" + uri .getHost ()+":" +uri .getPort ();
47
56
String url = baseUrl +location ;
48
- return client .get (url ,copyHeaders (headers ));
57
+ return client .get (url ,copyHeaders (headers ), null );
49
58
}
50
59
return response .getBody ();
51
60
}
52
61
};
53
62
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
+
54
73
public HttpClient () {
55
74
this (null ,defaultResponseHandler );
56
75
}
@@ -75,26 +94,20 @@ public HttpClient(HttpProxyProperties properties,IHttpResponseHandler responseHa
75
94
new InetSocketAddress (properties .getProxyHost (), properties .getProxyPort ())));
76
95
}
77
96
}
97
+ this .restTemplate .setErrorHandler (defaultErrorHandler );
78
98
this .restTemplate .setRequestFactory (requestFactory );
79
99
}
80
100
81
- public String post (String url , JSON jsonObject ) {
82
- return post (url , new HttpHeaders (), jsonObject );
83
- }
84
-
85
101
public String post (String url , HttpHeaders headers , JSON jsonObject ) {
86
- headers .setContentType (MediaType .APPLICATION_JSON );
87
102
HttpEntity <String > httpEntity = new HttpEntity <>(jsonObject .toString (), headers );
103
+ UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder .fromHttpUrl (url );
104
+ URI uri = uriComponentsBuilder .build ().toUri ();
88
105
ResponseEntity <String > httpResponse = restTemplate .exchange (url , HttpMethod .POST , httpEntity , String .class );
89
- return httpResponse . getBody ( );
106
+ return responseHandler . toResponse ( this , uri , httpResponse );
90
107
}
91
108
92
- public String post (String url , MultiValueMap <String , String > formData ) {
93
- return post (url ,new HttpHeaders (),formData );
94
- }
95
109
96
110
public String post (String url , HttpHeaders headers , MultiValueMap <String , String > formData ) {
97
- headers .setContentType (MediaType .APPLICATION_FORM_URLENCODED );
98
111
HttpEntity <MultiValueMap <String , String >> httpEntity = new HttpEntity <>(formData , headers );
99
112
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder .fromHttpUrl (url );
100
113
URI uri = uriComponentsBuilder .build ().toUri ();
@@ -103,7 +116,6 @@ public String post(String url, HttpHeaders headers, MultiValueMap<String, String
103
116
}
104
117
105
118
public String get (String url , HttpHeaders headers , MultiValueMap <String , String > uriVariables ) {
106
- headers .setContentType (MediaType .APPLICATION_JSON );
107
119
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder .fromHttpUrl (url );
108
120
if (uriVariables != null ) {
109
121
uriComponentsBuilder = uriComponentsBuilder .queryParams (uriVariables );
@@ -115,16 +127,4 @@ public String get(String url, HttpHeaders headers, MultiValueMap<String, String>
115
127
}
116
128
117
129
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
-
130
130
}
0 commit comments