Skip to content

Commit 25ed94e

Browse files
committed
fix RestTemplate uriBuilderFactory EncodingMode to NONE
1 parent d0b48ac commit 25ed94e

File tree

7 files changed

+36
-28
lines changed

7 files changed

+36
-28
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.5.0</version>
15+
<version>1.5.1</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.5.0</version>
8+
<version>1.5.1</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.5.0</version>
8+
<version>1.5.1</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.5.0</version>
9+
<version>1.5.1</version>
1010
</parent>
1111

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

springboot-starter/pom.xml

+1-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.5.0</version>
8+
<version>1.5.1</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

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

+22-19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.web.client.DefaultResponseErrorHandler;
1111
import org.springframework.web.client.ResponseErrorHandler;
1212
import org.springframework.web.client.RestTemplate;
13+
import org.springframework.web.util.DefaultUriBuilderFactory;
1314
import org.springframework.web.util.UriComponentsBuilder;
1415

1516
import java.io.IOException;
@@ -22,7 +23,7 @@
2223
public class HttpClient {
2324

2425
public interface IHttpResponseHandler{
25-
String toResponse(HttpClient client,URI uri,ResponseEntity<String> response);
26+
String toResponse(HttpClient client,String uri,ResponseEntity<String> response);
2627
}
2728

2829
private final RestTemplate restTemplate;
@@ -40,7 +41,7 @@ public HttpHeaders copyHeaders(HttpHeaders headers){
4041
}
4142

4243
@Override
43-
public String toResponse(HttpClient client, URI uri, ResponseEntity<String> response) {
44+
public String toResponse(HttpClient client, String url, ResponseEntity<String> response) {
4445
if(response.getStatusCode().equals(HttpStatus.OK)){
4546
return response.getBody();
4647
}
@@ -50,11 +51,12 @@ public String toResponse(HttpClient client, URI uri, ResponseEntity<String> resp
5051
}
5152

5253
if(response.getStatusCode().equals(HttpStatus.FOUND)){
54+
URI uri = URI.create(url);
5355
HttpHeaders headers = response.getHeaders();
5456
String location = Objects.requireNonNull(headers.getLocation()).toString();
5557
String baseUrl = uri.getScheme() + "://" + uri.getHost()+":"+uri.getPort();
56-
String url = baseUrl+location;
57-
return client.get(url,copyHeaders(headers),null);
58+
String locationUrl = baseUrl+location;
59+
return client.get(locationUrl,copyHeaders(headers),null);
5860
}
5961
return response.getBody();
6062
}
@@ -96,34 +98,35 @@ public HttpClient(HttpProxyProperties properties,IHttpResponseHandler responseHa
9698
}
9799
this.restTemplate.setErrorHandler(defaultErrorHandler);
98100
this.restTemplate.setRequestFactory(requestFactory);
101+
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory();
102+
uriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
103+
this.restTemplate.setUriTemplateHandler(uriBuilderFactory);
99104
}
100105

101106
public String post(String url, HttpHeaders headers, JSON jsonObject) {
102107
HttpEntity<String> httpEntity = new HttpEntity<>(jsonObject.toString(), headers);
103-
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(url);
104-
URI uri = uriComponentsBuilder.build().toUri();
105108
ResponseEntity<String> httpResponse = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
106-
return responseHandler.toResponse(this,uri,httpResponse);
109+
return responseHandler.toResponse(this,url,httpResponse);
107110
}
108111

109-
110112
public String post(String url, HttpHeaders headers, MultiValueMap<String, String> formData) {
111113
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(formData, headers);
112-
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(url);
113-
URI uri = uriComponentsBuilder.build().toUri();
114-
ResponseEntity<String> httpResponse = restTemplate.exchange(uri, HttpMethod.POST, httpEntity, String.class);
115-
return responseHandler.toResponse(this,uri,httpResponse);
114+
ResponseEntity<String> httpResponse = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
115+
return responseHandler.toResponse(this,url,httpResponse);
116116
}
117117

118118
public String get(String url, HttpHeaders headers, MultiValueMap<String, String> uriVariables) {
119-
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(url);
120-
if (uriVariables != null) {
121-
uriComponentsBuilder = uriComponentsBuilder.queryParams(uriVariables);
122-
}
123-
URI uri = uriComponentsBuilder.build().toUri();
124119
HttpEntity<String> httpEntity = new HttpEntity<>(headers);
125-
ResponseEntity<String> httpResponse = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, String.class);
126-
return responseHandler.toResponse(this,uri,httpResponse);
120+
ResponseEntity<String> httpResponse;
121+
if(uriVariables!=null&&!uriVariables.isEmpty()) {
122+
URI uri = UriComponentsBuilder.fromHttpUrl(url)
123+
.queryParams(uriVariables)
124+
.build(true).toUri();
125+
httpResponse = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, String.class);
126+
}else{
127+
httpResponse = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
128+
}
129+
return responseHandler.toResponse(this, url, httpResponse);
127130
}
128131

129132

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public HttpHeaders copyHeaders(HttpHeaders headers) {
3030
}
3131

3232
@Override
33-
public String toResponse(HttpClient client, URI uri, ResponseEntity<String> response) {
33+
public String toResponse(HttpClient client, String url, ResponseEntity<String> response) {
3434
if (response.getStatusCode().equals(HttpStatus.OK)) {
3535
return response.getBody();
3636
}
@@ -40,11 +40,12 @@ public String toResponse(HttpClient client, URI uri, ResponseEntity<String> resp
4040
}
4141

4242
if (response.getStatusCode().equals(HttpStatus.FOUND)) {
43+
URI uri = URI.create(url);
4344
HttpHeaders headers = response.getHeaders();
4445
String location = Objects.requireNonNull(headers.getLocation()).toString();
4546
String baseUrl = uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort();
46-
String url = baseUrl + location;
47-
return client.get(url, copyHeaders(headers),null);
47+
String locationUrl = baseUrl + location;
48+
return client.get(locationUrl, copyHeaders(headers),null);
4849
}
4950
return response.getBody();
5051
}
@@ -67,7 +68,11 @@ public String post(String url, RestParamBuilder restParam){
6768
}
6869

6970
public String get(String url){
70-
return httpClient.get(url,httpHeaders,null);
71+
return get(url,null);
72+
}
73+
74+
public String get(String url,RestParamBuilder restParam){
75+
return httpClient.get(url,httpHeaders,restParam!=null?restParam.toFormRequest():null);
7176
}
7277

7378
}

0 commit comments

Comments
 (0)