RestTemplate的请求参数传递问题之RestTemplate发送Get请求通过body传递json参数

2年前 (2022) 程序员胖胖胖虎阿
238 0 0

你知道的越多,你不知道的越多
点赞再看,养成习惯
如果您有疑问或者见解,或者没有积分想获取项目和定制项目,欢迎指教:
企鹅:869192208

目前遇到一个对接需求,对方公司提供了一个接口,请求方法为GET,传参是在body中的json格式数据。

针对这个需求,在postman中进行测试,请求成功,后续需要用java进行接口调用。
RestTemplate的请求参数传递问题之RestTemplate发送Get请求通过body传递json参数

首先,我们要了解 RestTemplate 请求方法和 HTTP 请求方法的对应关系。

HTTP method RestTemplate methods
DELETE delete
GET getForObject / getForEntity
HEAD headForHeaders
OPTIONS optionsForAllow
POST postForLocation / postForObject
PUT put
any exchange / execute

当使用get请求需要携带 body中的参数的时候 需要重写Http 版本号必须是4.3以上版本

定义HttpGetRequestWithEntity实现HttpEntityEnclosingRequestBase抽象类,以支持GET请求携带body数据

  1. pom 文件引入
<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.3</version>
</dependency>
  1. 定义 HttpGetRequestWithEntity 实现 HttpEntityEnclosingRequestBase 抽象类
    创建 HttpComponentsClientRestfulHttpRequestFactory.java
public class HttpComponentsClientRestfulHttpRequestFactory extends HttpComponentsClientHttpRequestFactory {
    @Override
    protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {

        if (httpMethod == HttpMethod.GET) {
            return new HttpGetRequestWithEntity(uri);
        }
        return super.createHttpUriRequest(httpMethod, uri);
    }

    /**
     * 定义HttpGetRequestWithEntity实现HttpEntityEnclosingRequestBase抽象类,以支持GET请求携带body数据
     */

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

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

        }
    }
}
  1. 配置 RestTemplateConfig,将步骤 2 的 HttpComponentsClientRestfulHttpRequestFactory 类注入
@Configuration
public class RestTemplateConfig {

    @Bean(name="remoteRestTemplate")
    public RestTemplate restTemplate(ClientHttpRequestFactory simpleClientHttpRequestFactory){
        RestTemplate restTemplate = new RestTemplate(simpleClientHttpRequestFactory);
        restTemplate.setRequestFactory(new HttpComponentsClientRestfulHttpRequestFactory());
        return restTemplate;
    }
    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(60000);
        factory.setReadTimeout(20000);
        return factory;
    }
}
  1. 发起 body 带 json 参数的 GET 请求
public ResponseEntity<Object> createCodeByNumber(String url, String boxSignerId) {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put(BOXSIGNERID, boxSignerId);
    jsonObject.put(SECRET, secret);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<String> httpEntity = new HttpEntity<>(JSON.toJSONString(jsonObject), headers);
    log.info("url:" + url + ",httpEntity:" + JSON.toJSONString(httpEntity));
    ResponseEntity<Object> response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, Object.class);
    log.info("{}请求的返回内容:{}", url, JSON.parseObject(JSON.toJSONString(response.getBody())));
    return response;
}

参考资料:
RestTemplate的请求参数传递问题 RestTemplate发送Get请求通过body传参问题

相关文章

暂无评论

暂无评论...