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

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

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

针对这个需求,在postman中进行测试,请求成功,后续需要用java进行接口调用。
postman发送测试请求

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

HTTP methodRestTemplate methods
DELETEdelete
GETgetForObject / getForEntity
HEADheadForHeaders
OPTIONSoptionsForAllow
POSTpostForLocation / postForObject
PUTput
anyexchange / 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传参问题

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值