Spring RestTemplate post
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("auditParams",auditJob.getAuditParams());
JSONObject jsonTaskObj = restTemplatePost(map);
public JSONObject restTemplatePost(MultiValueMap<String, Object> params) throws JSONException {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
MediaType mediaType = MediaType.parseMediaType("application/json; charset=UTF-8");
String username = auditScriptConfig.getUsername();
String password = auditScriptConfig.getPassword();
String plainCredentials = username + ":" + password;
String base64Credentials = Base64.getEncoder().encodeToString(plainCredentials.getBytes());
httpHeaders.setContentType(mediaType);
httpHeaders.add("Accept", MediaType.APPLICATION_JSON.toString());
//身份验证
httpHeaders.add("Authorization", "Basic " + base64Credentials);
//如果使用HttpEntity<JSONObject>对象传入很容易出现no suitable HttpMessageConverter found for request type的错误,直接转成字符串,JSONObject.toString()
//HttpEntity<String> httpEntity = new HttpEntity<>(params, httpHeaders);
//ResponseEntity<String> response = restTemplate.exchange(auditScriptConfig.getUrl(), HttpMethod.POST, httpEntity, String.class);
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(params, httpHeaders);
ResponseEntity<String> response = restTemplate.postForEntity(auditScriptConfig.getUrl(), httpEntity, String.class);
HttpStatus status = response.getStatusCode();
System.out.println("HttpStatus: "+ status);
JSONObject result = new JSONObject(response.getBody());
return result;
}
Spring RestTemplate post的更多相关文章
- Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header
{ "Author": "tomcat and jerry", "url":"http://www.cnblogs.com/tom ...
- Spring RestTemplate介绍
http://www.cnblogs.com/rollenholt/p/3894117.html RestTemplate 这篇文章打算介绍一下Spring的RestTemplate.我这边以前设计到 ...
- How to Send an HTTP Header With Every Request With Spring RestTemplate
In Know Which Apps Are Hitting Your Web Service, I showed how to write a servlet filter that enforce ...
- Spring RestTemplate详解
Spring RestTemplate详解 1.什么是REST? REST(RepresentationalState Transfer)是Roy Fielding 提出的一个描述互联系统架构风格 ...
- Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求
Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939 版权声明 ...
- How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查)
How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查) **** ...
- Spring RestTemplate 小结
关于RestTemplate 首先,你可以把它理解为一个发起请求并接收响应的工具类(功能类似浏览器). 其次,它其实是一个壳,具体还是通过调用别的接口来实现(如jdk自带的连接,或者HttpClien ...
- spring RestTemplate用法详解
spring RestTemplate用法详解 spring 3.2.3 框架参考有说明 21.9 Accessing RESTful services on the Client
- 使用HttpClient4来构建Spring RestTemplate
Spring RestTemplate简单说明 现在REST服务已经很普及了,在我们的程序中,经常会需要调用REST API,这时候会有很多选择,原始一点的JDK自带的,再进一步点使用HttpClie ...
- spring restTemplate 用法
发出get请求,方式一 String url = serverUrl+"/path/path?id={id}"; int i = restTemplate.getForObject ...
随机推荐
- yum groupinstall报错,处理方法
http://www.cnblogs.com/xiaoluo501395377/archive/2013/05/21/3089970.html ===== 创建repo库 # createrepo - ...
- How vacuum template0
[pg@h1 ~]$ vacuumdb --freeze template0 vacuumdb: could not connect to database template0: FATAL: dat ...
- Android开发之Shortcuts, LiveFolder, Widget
2013-07-05 桌面组件包括:快捷方式(Shortcuts),实时文件夹(Live Folder),桌面插件(Widget). 快捷方式用于启动应用程序的某个组件,例如Activity, S ...
- windows 2003 如何实现远程桌面与本地桌面统一
最近在使用XP对2003服务器进行远程管理的时候,发现远程桌面与本地桌面不一致,本身在本地桌面开启的程序例如杀毒软件防火墙之类的,在远程桌面居然看不到,同时在远程桌面开启的程序,跑到服务器本地桌面也看 ...
- 雅虎天气API调用
雅虎天气API调用: 1.调用方法:http://weather.yahooapis.com/forecastrss?w=2502265&u=c,绿色字体为城市代号,u=c表示取摄氏度. 2. ...
- getconf命令【一天一个命令】
我们时常需要查询系统相关的信息,比如页面大小,整数大小之类,如果编写程序去计算会比较繁琐,这里有一个很有用的命令,可以用来获取系统相关信息.它就是getconf. $ getconf PAGE_S ...
- spawn-fcgi原理及源代码分析
spawn-fcgi是一个小程序,作用是管理fast-cgi进程,功能和php-fpm类似,简单小巧,原先是属于lighttpd的一部分.后来因为使用比較广泛.所以就迁移出来作为独立项目了.本文介绍的 ...
- location if (.....) #if与中括号之间要有空格
[root@web01 default]# /app/server/nginx/sbin/nginx -t nginx: [emerg] unknown directive nginx: config ...
- CentOS7 使用tab建补全命令
Centos7在使用最小化安装的时候,没有安装自动补全的包,需要自己手动安装,安装下面过滤出来的包 yum -y install bash-completion 安装完毕后退出bash重新登陆生效!
- 李洪强iOS经典面试题37-解释垃圾回收的原理
李洪强iOS经典面试题37-解释垃圾回收的原理 问题 我们知道,Android 手机通常使用 Java 来开发,而 Java 是使用垃圾回收这种内存管理方式. 那么,ARC 和垃圾回收对比,有什么 ...