日常问题随笔-RestTemplate发送Get请求通过body传参问题

问题出现

今天在对接其他部门的接口时,发现对方提供了一个GET请求,但是参数却要求通过body传递json格式的数据。这可难住我了,GET请求还能通过body传递参数吗?带着疑问,先试一下吧。我们项目用的RestTemplate,我想起了RestTemplate的exchange方法有个参数HttpEntity,会不会是这里可以传参数进去呢?迅速写了代码

1
2
3
4
5
6
public static String getForJson(String url, Map<String,Object> dataValue) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<String> httpEntity = new HttpEntity<>(JSON.toJSONString(dataValue),httpHeaders);
return SpringContextUtil.getBean(RestTemplate.class).exchange(url, HttpMethod.GET,httpEntity,String.class).getBody();
}

结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2020-06-18 17:08:37.544|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "GET /cross/appms-internal/v1/portal/app/config/search HTTP/1.1[\r][\n]"
2020-06-18 17:08:37.544|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "Accept: text/plain, application/json, application/*+json, */*[\r][\n]"
2020-06-18 17:08:37.544|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "Content-Type: application/json;charset=UTF-8[\r][\n]"
2020-06-18 17:08:37.544|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "Accept-Charset: big5, big5-hkscs, cesu-8, euc-jp, euc-kr, gb18030, gb2312, gbk, ibm-thai, ibm00858, ibm01140, ibm01141, ibm01142, ibm01143, ibm01144, ibm01145, ibm01146, ibm01147, ibm01148, ibm01149, ibm037, ibm1026, ibm1047, ibm273, ibm277, ibm278, ibm280, ibm284, ibm285, ibm290, ibm297, ibm420, ibm424, ibm437, ibm500, ibm775, ibm850, ibm852, ibm855, ibm857, ibm860, ibm861, ibm862, ibm863, ibm864, ibm865, ibm866, ibm868, ibm869, ibm870, ibm871, ibm918, iso-2022-cn, iso-2022-jp, iso-2022-jp-2, iso-2022-kr, iso-8859-1, iso-8859-13, iso-8859-15, iso-8859-2, iso-8859-3, iso-8859-4, iso-8859-5, iso-8859-6, iso-8859-7, iso-8859-8, iso-8859-9, jis_x0201, jis_x0212-1990, koi8-r, koi8-u, shift_jis, tis-620, us-ascii, utf-16, utf-16be, utf-16le, utf-32, utf-32be, utf-32le, utf-8, windows-1250, windows-1251, windows-1252, windows-1253, windows-1254, windows-1255, windows-1256, windows-1257, windows-1258, windows-31j, x-big5-hkscs-2001, x-big5-solaris, x-euc-jp-linux, x-euc-tw, x-eucjp-open, x-ibm1006, x-ibm1025, x-ibm1046, x-ibm1097, x-ibm1098, x-ibm1112, x-ibm1122, x-ibm1123, x-ibm1124, x-ibm1166, x-ibm1364, x-ibm1381, x-ibm1383, x-ibm300, x-ibm33722, x-ibm737, x-ibm833, x-ibm834, x-ibm856, x-ibm874, x-ibm875, x-ibm921, x-ibm922, x-ibm930, x-ibm933, x-ibm935, x-ibm937, x-ibm939, x-ibm942, x-ibm942c, x-ibm943, x-ibm943c, x-ibm948, x-ibm949, x-ibm949c, x-ibm950, x-ibm964, x-ibm970, x-iscii91, x-iso-2022-cn-cns, x-iso-2022-cn-gb, x-iso-8859-11, x-jis0208, x-jisautodetect, x-johab, x-macarabic, x-maccentraleurope, x-maccroatian, x-maccyrillic, x-macdingbat, x-macgreek, x-machebrew, x-maciceland, x-macroman, x-macromania, x-macsymbol, x-macthai, x-macturkish, x-macukraine, x-ms932_0213, x-ms950-hkscs, x-ms950-hkscs-xp, x-mswin-936, x-pck, x-sjis_0213, x-utf-16le-bom, x-utf-32be-bom, x-utf-32le-bom, x-windows-50220, x-windows-50221, x-windows-874, x-windows-949, x-windows-950, x-windows-iso2022jp[\r][\n]"
2020-06-18 17:08:37.547|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "Host: [\r][\n]"
2020-06-18 17:08:37.547|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
2020-06-18 17:08:37.547|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.10 (Java/1.8.0_201)[\r][\n]"
2020-06-18 17:08:37.547|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
2020-06-18 17:08:37.547|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "[\r][\n]"
2020-06-18 17:08:37.734|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 << "HTTP/1.1 400 [\r][\n]"
2020-06-18 17:08:37.735|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 << "Server: nginx[\r][\n]"
2020-06-18 17:08:37.735|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 << "Date: Thu, 18 Jun 2020 09:08:36 GMT[\r][\n]"
2020-06-18 17:08:37.736|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 << "Content-Type: application/json;charset=UTF-8[\r][\n]"
2020-06-18 17:08:37.736|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 << "Transfer-Encoding: chunked[\r][\n]"
2020-06-18 17:08:37.736|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 << "Connection: keep-alive[\r][\n]"
2020-06-18 17:08:37.737|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 << "X-Application-Context: cross-gateway:test:8080[\r][\n]"
2020-06-18 17:08:37.737|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 << "Set-Cookie: Track_id=rBCB8F7rLxRhG2WIFf7OAg==; expires=Fri, 18-Jun-21 09:08:36 GMT; domain=test.cn; path=/[\r][\n]"
2020-06-18 17:08:37.737|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 << "[\r][\n]"
2020-06-18 17:08:37.738|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 << "2b[\r][\n]"
2020-06-18 17:08:37.738|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 << "{"code":4004,"message":"parameter invalid"}[\r][\n]"
2020-06-18 17:08:37.738|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 << "0[\r][\n]"
2020-06-18 17:08:37.739|DEBUG|http-nio-8090-exec-2|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 << "[\r][\n]"

通过日志可以看到请求失败了,对方接口返回了参数无效

1
{"code":4004,"message":"parameter invalid"}

解决问题

在HTTP1.1中GET请求已经可以在body里发送数据,但是既然用了RestTemplate,就要看一下RestTemplate是否提供了实现,这里我们用的httpclient,默认用的HttpComponentsClientHttpRequestFactory工厂,查看了HttpComponentsClientHttpRequestFactory的createHttpUriRequest方法,发现GET请求是HttpGet,HttpGet继承HttpRequestBase,HttpRequestBase是不支持传递body的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
switch (httpMethod) {
case GET:
return new HttpGet(uri);
case HEAD:
return new HttpHead(uri);
case POST:
return new HttpPost(uri);
case PUT:
return new HttpPut(uri);
case PATCH:
return new HttpPatch(uri);
case DELETE:
return new HttpDelete(uri);
case OPTIONS:
return new HttpOptions(uri);
case TRACE:
return new HttpTrace(uri);
default:
throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
}
}

查了资料发现HttpEntityEnclosingRequestBase是可以传递body的,那就自定义一个工厂继承HttpComponentsClientHttpRequestFactory,重写createHttpUriRequest方法,当发送的请求是GET请求时,创建自定义的继承HttpEntityEnclosingRequestBase的类,这样GET请求就可以传送body。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory clientHttpRequestFactory) {
return new RestTemplate(clientHttpRequestFactory);
}

@Bean
public ClientHttpRequestFactory httpRequestFactory(HttpClient httpClient) {

return new HttpComponentsClientHttpRequestCRMFactory(httpClient);

}

@Bean
public HttpClient httpClient() {
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
//设置整个连接池最大连接数 根据自己的场景决定
connectionManager.setMaxTotal(200);
//路由是对maxTotal的细分
connectionManager.setDefaultMaxPerRoute(100);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(10000) //服务器返回数据(response)的时间,超过该时间抛出read timeout
.setConnectTimeout(5000)//连接上服务器(握手成功)的时间,超出该时间抛出connect timeout
.setConnectionRequestTimeout(1000)//从连接池中获取连接的超时时间,超过该时间未拿到可用连接,会抛出org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
.build();
return HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(connectionManager)
.build();
}

private static final class HttpComponentsClientHttpRequestCRMFactory extends HttpComponentsClientHttpRequestFactory {
public HttpComponentsClientHttpRequestCRMFactory(HttpClient httpClient) {
super(httpClient);
}

@Override
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
if (httpMethod == HttpMethod.GET) {
return new HttpGetRequestForBody(uri);
}
return super.createHttpUriRequest(httpMethod, uri);
}
}

private static final class HttpGetRequestForBody extends HttpEntityEnclosingRequestBase {
public HttpGetRequestForBody(final URI uri) {
super.setURI(uri);
}

@Override
public String getMethod() {
return HttpMethod.GET.name();
}
}
}

测试结果

再次测试结果,发现调用正确返回

1
2
3
4
5
6
7
8
9
10
11
2020-06-18 17:10:28.403|DEBUG|http-nio-8090-exec-1|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "GET /cross/appms-internal/v1/portal/app/config/search HTTP/1.1[\r][\n]"
2020-06-18 17:10:28.404|DEBUG|http-nio-8090-exec-1|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "Accept: text/plain, application/json, application/*+json, */*[\r][\n]"
2020-06-18 17:10:28.404|DEBUG|http-nio-8090-exec-1|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "Content-Type: application/json;charset=UTF-8[\r][\n]"
2020-06-18 17:10:28.407|DEBUG|http-nio-8090-exec-1|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "Accept-Charset: big5, big5-hkscs, cesu-8, euc-jp, euc-kr, gb18030, gb2312, gbk, ibm-thai, ibm00858, ibm01140, ibm01141, ibm01142, ibm01143, ibm01144, ibm01145, ibm01146, ibm01147, ibm01148, ibm01149, ibm037, ibm1026, ibm1047, ibm273, ibm277, ibm278, ibm280, ibm284, ibm285, ibm290, ibm297, ibm420, ibm424, ibm437, ibm500, ibm775, ibm850, ibm852, ibm855, ibm857, ibm860, ibm861, ibm862, ibm863, ibm864, ibm865, ibm866, ibm868, ibm869, ibm870, ibm871, ibm918, iso-2022-cn, iso-2022-jp, iso-2022-jp-2, iso-2022-kr, iso-8859-1, iso-8859-13, iso-8859-15, iso-8859-2, iso-8859-3, iso-8859-4, iso-8859-5, iso-8859-6, iso-8859-7, iso-8859-8, iso-8859-9, jis_x0201, jis_x0212-1990, koi8-r, koi8-u, shift_jis, tis-620, us-ascii, utf-16, utf-16be, utf-16le, utf-32, utf-32be, utf-32le, utf-8, windows-1250, windows-1251, windows-1252, windows-1253, windows-1254, windows-1255, windows-1256, windows-1257, windows-1258, windows-31j, x-big5-hkscs-2001, x-big5-solaris, x-euc-jp-linux, x-euc-tw, x-eucjp-open, x-ibm1006, x-ibm1025, x-ibm1046, x-ibm1097, x-ibm1098, x-ibm1112, x-ibm1122, x-ibm1123, x-ibm1124, x-ibm1166, x-ibm1364, x-ibm1381, x-ibm1383, x-ibm300, x-ibm33722, x-ibm737, x-ibm833, x-ibm834, x-ibm856, x-ibm874, x-ibm875, x-ibm921, x-ibm922, x-ibm930, x-ibm933, x-ibm935, x-ibm937, x-ibm939, x-ibm942, x-ibm942c, x-ibm943, x-ibm943c, x-ibm948, x-ibm949, x-ibm949c, x-ibm950, x-ibm964, x-ibm970, x-iscii91, x-iso-2022-cn-cns, x-iso-2022-cn-gb, x-iso-8859-11, x-jis0208, x-jisautodetect, x-johab, x-macarabic, x-maccentraleurope, x-maccroatian, x-maccyrillic, x-macdingbat, x-macgreek, x-machebrew, x-maciceland, x-macroman, x-macromania, x-macsymbol, x-macthai, x-macturkish, x-macukraine, x-ms932_0213, x-ms950-hkscs, x-ms950-hkscs-xp, x-mswin-936, x-pck, x-sjis_0213, x-utf-16le-bom, x-utf-32be-bom, x-utf-32le-bom, x-windows-50220, x-windows-50221, x-windows-874, x-windows-949, x-windows-950, x-windows-iso2022jp[\r][\n]"
2020-06-18 17:10:28.407|DEBUG|http-nio-8090-exec-1|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "Content-Length: 59[\r][\n]"
2020-06-18 17:10:28.407|DEBUG|http-nio-8090-exec-1|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "Host: dev-test.com[\r][\n]"
2020-06-18 17:10:28.407|DEBUG|http-nio-8090-exec-1|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
2020-06-18 17:10:28.407|DEBUG|http-nio-8090-exec-1|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.10 (Java/1.8.0_201)[\r][\n]"
2020-06-18 17:10:28.407|DEBUG|http-nio-8090-exec-1|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
2020-06-18 17:10:28.408|DEBUG|http-nio-8090-exec-1|org.apache.http.impl.conn.Wire|wire|73|http-outgoing-0 >> "[\r][\n]"
2020-06-18 17:10:28.408|DEBUG|http-nio-8090-exec-1|org.apache.http.impl.conn.Wire|wire|87|http-outgoing-0 >> "{"devIdList":[164359,164893],"pageSize":10,"currentPage":1}"

和第一次的请求相比,这一次请求日志中多了一句

1
2020-06-18 17:10:28.408|DEBUG|http-nio-8090-exec-1|org.apache.http.impl.conn.Wire|wire|87|http-outgoing-0 >> "{"devIdList":[164359,164893],"pageSize":10,"currentPage":1}"

如果还没看出来,我在整理一下日志

1
2
3
4
5
6
7
8
9
10
11
12
GET /cross/appms-internal/v1/portal/app/config/search HTTP/1.1[\r][\n]
Accept: text/plain, application/json, application/*+json, */*[\r][\n]
Content-Type: application/json;charset=UTF-8[\r][\n]
Accept-Charset: big5, big5-hkscs, cesu-8, euc-jp, euc-kr, gb18030, gb2312, gbk, ibm-thai, ibm00858, ibm01140, ibm01141, ibm01142, ibm01143, ibm01144, ibm01145, ibm01146, ibm01147, ibm01148, ibm01149, ibm037, ibm1026, ibm1047, ibm273, ibm277, ibm278, ibm280, ibm284, ibm285, ibm290, ibm297, ibm420, ibm424, ibm437, ibm500, ibm775, ibm850, ibm852, ibm855, ibm857, ibm860, ibm861, ibm862, ibm863, ibm864, ibm865, ibm866, ibm868, ibm869, ibm870, ibm871, ibm918, iso-2022-cn, iso-2022-jp, iso-2022-jp-2, iso-2022-kr, iso-8859-1, iso-8859-13, iso-8859-15, iso-8859-2, iso-8859-3, iso-8859-4, iso-8859-5, iso-8859-6, iso-8859-7, iso-8859-8, iso-8859-9, jis_x0201, jis_x0212-1990, koi8-r, koi8-u, shift_jis, tis-620, us-ascii, utf-16, utf-16be, utf-16le, utf-32, utf-32be, utf-32le, utf-8, windows-1250, windows-1251, windows-1252, windows-1253, windows-1254, windows-1255, windows-1256, windows-1257, windows-1258, windows-31j, x-big5-hkscs-2001, x-big5-solaris, x-euc-jp-linux, x-euc-tw, x-eucjp-open, x-ibm1006, x-ibm1025, x-ibm1046, x-ibm1097, x-ibm1098, x-ibm1112, x-ibm1122, x-ibm1123, x-ibm1124, x-ibm1166, x-ibm1364, x-ibm1381, x-ibm1383, x-ibm300, x-ibm33722, x-ibm737, x-ibm833, x-ibm834, x-ibm856, x-ibm874, x-ibm875, x-ibm921, x-ibm922, x-ibm930, x-ibm933, x-ibm935, x-ibm937, x-ibm939, x-ibm942, x-ibm942c, x-ibm943, x-ibm943c, x-ibm948, x-ibm949, x-ibm949c, x-ibm950, x-ibm964, x-ibm970, x-iscii91, x-iso-2022-cn-cns, x-iso-2022-cn-gb, x-iso-8859-11, x-jis0208, x-jisautodetect, x-johab, x-macarabic, x-maccentraleurope, x-maccroatian, x-maccyrillic, x-macdingbat, x-macgreek, x-machebrew, x-maciceland, x-macroman, x-macromania, x-macsymbol, x-macthai, x-macturkish, x-macukraine, x-ms932_0213, x-ms950-hkscs, x-ms950-hkscs-xp, x-mswin-936, x-pck, x-sjis_0213, x-utf-16le-bom, x-utf-32be-bom, x-utf-32le-bom, x-windows-50220, x-windows-50221, x-windows-874, x-windows-949, x-windows-950, x-windows-iso2022jp[\r][\n]
Content-Length: 59[\r][\n]
Host: dev-test.com[\r][\n]
Connection: Keep-Alive[\r][\n]
User-Agent: Apache-HttpClient/4.5.10 (Java/1.8.0_201)[\r][\n]
Accept-Encoding: gzip,deflate[\r][\n]
[\r][\n]

{"devIdList":[164359,164893],"pageSize":10,"currentPage":1}

这个就是HTTP协议,可以看到最后一行就是body里的数据,问题也解决了。