同事提供了一个get请求的接口, 参数是在url传的。后来加了个批量id的参数,结果就加在body传一个json,至于为什么,我也不知道,反正我肯定不会这么写。。。。
resttemplate 是不支持get请求传json body这种,于是各种搜,解决方案并不适用,后来发现了还有一个坑。
首先要在resttemplate的配置类增加一些设置。如下。
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
//修改restTemplate的RequestFactory使其支持Get携带body参数
restTemplate.setRequestFactory(new HttpComponentsClientRestfulHttpRequestFactory());
return restTemplate;
}
private static final class HttpComponentsClientRestfulHttpRequestFactory extends HttpComponentsClientHttpRequestFactory {
@Override
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
if (httpMethod == HttpMethod.GET) {
return new HttpGetRequestWithEntity(ur