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

推荐订阅源

Y
Y Combinator Blog
腾讯CDC
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
博客园_首页
D
DataBreaches.Net
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
月光博客
月光博客
Jina AI
Jina AI
Stack Overflow Blog
Stack Overflow Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
Vercel News
Vercel News
WordPress大学
WordPress大学
J
Java Code Geeks
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42

博客园 - 一路前行

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())));
    }
});