惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
B
Blog
罗磊的独立博客
GbyAI
GbyAI
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
The GitHub Blog
The GitHub Blog
人人都是产品经理
人人都是产品经理
博客园 - Franky
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
MyScale Blog
MyScale Blog
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏

博客园 - 一路前行

ConditionalOnProperty fastjson序列化乱序问题 IE中的console.log spring boot 中添加mongodb支持 javacript onclick事件中传递对象参数 Java Lambda 表达式 对 Map 对象排序 比较两个list对象是否相同 ubuntu redis 自启动配置文件(关机有密码) spring中订阅redis键值过期消息通知 网站架构之性能优化(转) Json转Java Bean spring mvc 4 校验 java @ResponseBody返回值中去掉NULL字段 合并两个java bean对象非空属性(泛型) spring mvc 删除返回字符串中值为null的字段 ubuntu下postgreSQL安装配置 十大Intellij IDEA快捷键(转) C#封装好的Win32API IntelliJ Idea 常用快捷键列表
RestTemplate之GET和POST调用和异步回调
一路前行 · 2018-07-21 · via 博客园 - 一路前行

get方式

String url = "http://hostname:port/v1.0/data/data";

HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE);

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
        .queryParam("deviceSn", sensorAtom.getDeviceSn())
        .queryParam("sensorSn", sensorAtom.getSensorSn())
        .queryParam("metaData", metaData)
        .queryParam("apiKey", "abdb3876-9abb-4644-91c2-8fb3d33530bd");

HttpEntity<?> entity = new HttpEntity<>(headers);
ListenableFuture<ResponseEntity<Response>> forEntity = asyncTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, Response.class);

forEntity.addCallback(new ListenableFutureCallback<ResponseEntity<Response>>() {
    @Override
    public void onFailure(Throwable ex) {
        logger.error(String.format("回调GET请求 %s 失败,%s", url, ex.getMessage()));
    }

    @Override
    public void onSuccess(ResponseEntity<Response> result) {
        logger.info(String.format("回调GET请求 %s 成功,返回值为%s", url, result.getBody()));
    }
});

post方式

String url = "http://hostname:port/v1.0/data/report";

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE);

MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("deviceSn", sensorAtom.getDeviceSn());
params.add("sensorSn", sensorAtom.getSensorSn());
params.add("metaData", metaData);
params.add("apiKey", "abdb3876-9abb-4644-91c2-8fb3d33530bd");

HttpEntity<MultiValueMap> requestEntity = new HttpEntity<>(params, headers);
ListenableFuture<ResponseEntity<Response>> forEntity = asyncTemplate.postForEntity(url, requestEntity, Response.class);

forEntity.addCallback(new ListenableFutureCallback<ResponseEntity<Response>>() {
    @Override
    public void onFailure(Throwable ex) {
        logger.error(String.format("POST回调 %s 参数为 %s 请求失败,%s", url, JSON.toJSONString(params), ex.getMessage()));
    }

    @Override
    public void onSuccess(ResponseEntity<Response> result) {
        logger.info(String.format("POST回调 %s 参数为 %s 请求成功,返回值为 %s", url, JSON.toJSONString(params), JSON.toJSONString(result.getBody())));
    }
});