用RestTemplate碰到的问题
给请求加上头信息
Request request = new Request(); HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("accept", "application/json");
requestHeaders.add("content-type", "application/json");
requestHeaders.add("content-length", "256");
requestHeaders.add("authorization", authorization);
requestHeaders.setContentType(MediaType.APPLICATION_JSON)
HttpEntity<?> httpEntity = new HttpEntity<Object>(request, requestHeaders);
ResponseEntity<Response> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Response.class);
return response.getBody();
有些请求是键值对的形式,用request的方法或封装HttpEntity也传不进去参数,可以尝试用下面的方法:
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("shortcut", shortcut);
map.add("_api_key", apikey);
DefaultResponse response = restTemplate.postForObject(url, map, DefaultResponse.class);
从远程获取的list用resttemplate转成相应的对象
ObjectMapper mapper = new ObjectMapper();
DefaultResponse defaultResponse = rehabilitationProxy.getServiceDate();
List<Resource> resources =
(List<Resource>) defaultResponse.getData();
for (int i = 0; i < resources.size(); i++) {
Resource resource = mapper.convertValue(resources.get(i), Resource.class);
}
用RestTemplate碰到的问题的更多相关文章
- Spring Boot使用RestTemplate消费REST服务的几个问题记录
我们可以通过Spring Boot快速开发REST接口,同时也可能需要在实现接口的过程中,通过Spring Boot调用内外部REST接口完成业务逻辑. 在Spring Boot中,调用REST Ap ...
- restTemplate.postForObject上传文件中文乱码(???.xls)
一.问题描述 项目中, 使用restTemplate上传文件时, 文件名中文乱码, 一串问号, 源文件名为: 测试中文乱码哦哦哦.zip, 通过restTemplate.postForObject调用 ...
- RestTemplate最详解
目录 1. RestTemplate简单使用 2. 一些其他设置 3. 简单总结 在项目中,当我们需要远程调用一个HTTP接口时,我们经常会用到RestTemplate这个类.这个类是Spring框架 ...
- RestTemplate 使用中的几个问题
Spring Boot使用RestTemplate消费REST服务的几个问题记录 我们可以通过Spring Boot快速开发REST接口,同时也可能需要在实现接口的过程中,通过Spring Boot调 ...
- 通过`RestTemplate`上传文件(InputStreamResource详解)
通过RestTemplate上传文件 1.上传文件File 碰到一个需求,在代码中通过HTTP方式做一个验证的请求,请求的参数包含了文件类型.想想其实很简单,直接使用定义好的MultiValueMap ...
- HttpClient的替代者 - RestTemplate
需要的包 ,除了Spring的基础包外还用到json的包,这里的数据传输使用json格式 客户端和服务端都用到一下的包 <!-- Spring --> <dependency> ...
- RestTemplate发送请求并携带header信息
1.使用restTemplate的postForObject方法 注:目前没有发现发送携带header信息的getForObject方法. HttpHeaders headers = new Http ...
- zk 起别名时候碰到的问题
第一次搭建时候都是用的ip,没什么问题,看到别人都是用的别名,于是也想试试把ip改成别名.然而 其中碰到的问题 ,快一周了才解决,现在记录下: 1.改主机别名 一直以为 修改 /etc/hosts 里 ...
- 【Spring-web】RestTemplate源码学习——梳理内部实现过程
2016-12-28 by 安静的下雪天 http://www.cnblogs.com/quiet-snowy-day/p/6228198.html 提示:使用手机浏览时请注意,图多费流量. 本篇 ...
随机推荐
- Github for Windows安装
下载软件:https://desktop.github.com/ 安装之前要求系统先要有安装.net framework 4.5,不然软件安装的时候会自动上网下载安装,这软件下载起来非常慢. 第一次操 ...
- Codeforces Round #372 (Div. 2) A
Description ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecut ...
- 2016年12月13日 星期二 --出埃及记 Exodus 21:8
2016年12月13日 星期二 --出埃及记 Exodus 21:8 If she does not please the master who has selected her for himsel ...
- ADO.NET增删改-------跟查不一样
建立数据库 create database ren go use database go create table user ( code nvarchar(20) primary key,--编号 ...
- Nginx架构的企业级应用
Nginx架构的企业级应用 ==================================================== 实现HA高可用集群 实现LB负载均衡集群 Nginx实现反向代理 ...
- CentOS 7.0安装配置LAMP服务器(Apache+PHP+MariaDB)
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service #停止fir ...
- 【leetcode❤python】 205. Isomorphic Strings
#-*- coding: UTF-8 -*- #转换法class Solution(object): def isIsomorphic(self, s, t): "&qu ...
- 取消mod_sofia的呼叫鉴权
FreeSWITCH中默认的SIP呼叫是要鉴权的,流程如下. 终端 FreeSWITCH A -----Invite------> FS A <----Trying------ FS A ...
- missing sdkl in .NET Core 1.0.1 - VS 2015 Tooling Preview 2
打开项目的时候,提示缺少sdk 在C:\Program Files\dotnet\sdk找不到对应的版本 解决方法: https://github.com/aspnet/Tooling/blob/ma ...
- printf()函数
printf()函数是格式化输出函数, 一般用于向标准输出设备按规定格式输出信息. printf()函数的调用格式为: printf("<格式化字符串>", <参 ...