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

推荐订阅源

B
Blog
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
Latest news
Latest news
博客园 - 三生石上(FineUI控件)
美团技术团队
aimingoo的专栏
aimingoo的专栏
Google Online Security Blog
Google Online Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threatpost
Y
Y Combinator Blog
T
Tailwind CSS Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
Arctic Wolf
C
Cyber Attacks, Cyber Crime and Cyber Security
小众软件
小众软件
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
Tenable Blog
W
WeLiveSecurity
L
LINUX DO - 热门话题
D
Docker
Cyberwarzone
Cyberwarzone
量子位
A
About on SuperTechFans
The Last Watchdog
The Last Watchdog
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
P
Palo Alto Networks Blog
The Hacker News
The Hacker News
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Full Disclosure
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
T
The Exploit Database - CXSecurity.com
Engineering at Meta
Engineering at Meta
O
OpenAI News
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
IT之家
IT之家
S
Secure Thoughts
MongoDB | Blog
MongoDB | Blog
L
Lohrmann on Cybersecurity
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News

博客园 - 一路前行

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