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 ...
随机推荐
- dubbo初探(转载)
1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需 ...
- 【转帖】Service Discovery: 6 questions to 4 experts
https://highops.com/insights/service-discovery-6-questions-to-4-experts/ What’s Service Discovery? I ...
- Python 以指定的概率选取元素
Python 以指定的概率选取元素 Problem You want to pick an item at random from a list, just about as random.choic ...
- 视频中的DTS与PTS的理解
原文地址:http://blog.itpub.net/30004768/viewspace-1338882/ DTS(解码时间戳)和PTS(显示时间戳)分别是解码器进行解码和显示帧时相对于SCR(系 ...
- mosquitto ---SSL/TLS 单向认证+双向认证
生成证书 # * Redistributions in binary form must reproduce the above copyright # notice, this list of ...
- Java Persistence with MyBatis 小结2
MyBatis 最关键的组成部分是 SqlSessionFactory,我们可以从中获取 SqlSession,并执行映射的 SQL 语句.SqlSessionFactory 对象可以通过基于 XML ...
- hdoj4864 Task (贪心)
题目来源: 2014 Multi-University Training Contest 1--by FZU 题意:有N个机器和m个工作.机器和工作都有一个时间xi和价值yi,一个工作仅仅有满足xi和 ...
- IronPython使用
C#: class Program { static void Main(string[] args) { ScriptEngine engine = Python.CreateEngine(); S ...
- spring 发布 Jax-Ws Service (一)
1.maven依赖: <dependency> <groupId>org.springframework.ws</groupId> <artifactId&g ...
- Nginx - rewrite 不区分大小写进行匹配
Nginx - rewrite 不区分大小写进行匹配 分类: Nginx2014-05-28 19:25 1046人阅读 评论(0) 收藏 举报 Use (?i) to match case-inse ...