44import com .codingapi .springboot .framework .rest .properties .HttpProxyProperties ;
55import lombok .extern .slf4j .Slf4j ;
66import org .springframework .http .*;
7+ import org .springframework .http .client .ClientHttpResponse ;
78import org .springframework .http .client .SimpleClientHttpRequestFactory ;
89import org .springframework .util .MultiValueMap ;
10+ import org .springframework .web .client .DefaultResponseErrorHandler ;
11+ import org .springframework .web .client .ResponseErrorHandler ;
912import org .springframework .web .client .RestTemplate ;
1013import org .springframework .web .util .UriComponentsBuilder ;
1114
15+ import java .io .IOException ;
1216import java .net .InetSocketAddress ;
1317import java .net .Proxy ;
1418import 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}
0 commit comments