转载

restTemplate的get post请求不同的header设置及传参方式

当前项目里对外暴露的接口基本都是一个rest的api,与其他项目对接时,通常也是调用对方暴露出来的rest api【http的接口】。这就涉及到要在自己项目里发送http请求。

调用http请求的方式:可以自己使用httpclient或者其他原生方式构建http请求。spring当然也提供了调用模板,那就是restTemplate。本文就是罗列restTemplate的调用及不同的传参方式。

1.restTemplate的引入

spring中引入(引入就由spring托管了。直接@Autowired注解引用就行)
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"></bean>

springboot中直接@Autowired引入就行

2.get请求

@Autowired
private RestTemplate restTemplate

String url="https://restapi.amap.com/v3/weather/weatherInfo?city=110101&key=3ff9482454cb60bcb73f65c8c48d4209](https://restapi.amap.com/v3/weather/weatherInfo?city=110101&key=3ff9482454cb60bcb73f65c8c48d4209)";
//无参 不用指定header的请求
JSONObect jsonobject=restTemplate.getForObject(url,JSONObject.class);

//无参指定header的请求
        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept", "application/json");
        HttpEntity<JSONObject> httpEntity = new HttpEntity<>( headers);
JSONObject jsonobject=restTemplate.getForObject(url,JSONObject.class,httpEntity);

//有参,param方式的参数【参数是拼接在url路径上】
Map<String, Object> params = new HashMap<>();
params.put("param1", "value1");
params.put("param2", "value2");
JSONObject jsonobject=restTemplate.getForObject(url,JSONObject.class,params);

//有参,请求体body中json【较少见,调用方式如下:】
因为restTemplate默认使用的jdk的requestfactory,这种方式不支持get请求这样的传参,故需要将restTemplate的requestfactory设置为自定义的requestfactory继承HttpEntityEnclosingRequestBase具体如下:
private static final class HttpComponentsClientHttpRequestWithBodyFactory extends HttpComponentsClientHttpRequestFactory {
        @Override
        protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
            if (httpMethod == HttpMethod.GET) {
                return new HttpGetRequestWithEntity(uri);
            }
            return super.createHttpUriRequest(httpMethod, uri);
        }
    }
    private static final class HttpGetRequestWithEntity extends HttpEntityEnclosingRequestBase {
        public HttpGetRequestWithEntity(final URI uri) {
            super.setURI(uri);
        }

        @Override
        public String getMethod() {
            return HttpMethod.GET.name();
        }
    }
此种方式下的调用
        String temp="";//参数的json串
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Accept", "application/json");
        HttpEntity<JSONObject> httpEntity = new HttpEntity(temp, headers);
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestWithBodyFactory());//或者在注入之前set该requestfactory
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
        String body = exchange.getBody();

3.post请求

//有参  请求体【body体中】json参数
JSONObject params=new JSONObject();
params.put("param1","value1");
JSONObject jsonobject=restTemplate.postForTemplate(url,params,JSONObject.class);

//有参 请求体中json参数 设置header头
        JSONObject param = new JSONObject();
        param.put("param1","value1");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(param, headers);
JSONObject jsonObject = restTemplate.postForObject(url, httpEntity, JSONObject.class);

以上是resttemplate根据不同传参方式发送http请求的使用实例~~~~

原文  https://segmentfault.com/a/1190000021123356
正文到此结束
Loading...