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的更多相关文章

  1. Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header

    { "Author": "tomcat and jerry", "url":"http://www.cnblogs.com/tom ...

  2. Spring RestTemplate介绍

    http://www.cnblogs.com/rollenholt/p/3894117.html RestTemplate 这篇文章打算介绍一下Spring的RestTemplate.我这边以前设计到 ...

  3. 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 ...

  4. Spring RestTemplate详解

    Spring RestTemplate详解   1.什么是REST? REST(RepresentationalState Transfer)是Roy Fielding 提出的一个描述互联系统架构风格 ...

  5. Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求

    Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939   版权声明 ...

  6. How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查)

    How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查) **** ...

  7. Spring RestTemplate 小结

    关于RestTemplate 首先,你可以把它理解为一个发起请求并接收响应的工具类(功能类似浏览器). 其次,它其实是一个壳,具体还是通过调用别的接口来实现(如jdk自带的连接,或者HttpClien ...

  8. spring RestTemplate用法详解

    spring RestTemplate用法详解 spring 3.2.3 框架参考有说明 21.9 Accessing RESTful services on the Client

  9. 使用HttpClient4来构建Spring RestTemplate

    Spring RestTemplate简单说明 现在REST服务已经很普及了,在我们的程序中,经常会需要调用REST API,这时候会有很多选择,原始一点的JDK自带的,再进一步点使用HttpClie ...

  10. spring restTemplate 用法

    发出get请求,方式一 String url = serverUrl+"/path/path?id={id}"; int i = restTemplate.getForObject ...

随机推荐

  1. Java微信扫描支付模式二Demo ,整合官网直接运行版本

    概述 场景介绍 用户使用微信“扫一扫”扫描二维码后,获取商品支付信息,引导用户完成支付. 详细 代码下载:http://www.demodashi.com/demo/13880.html 一.相关配置 ...

  2. Redis总结(六)Redis配置文件全解(转载)

    前面已经写了一些关于redis 的介绍,redis 的基本功能和用法,基本上都说了,有问题的可以去看看 http://www.cnblogs.com/zhangweizhong/category/77 ...

  3. C++:SQLServer字段赋值

    大前提: 1. 初始化环境 2. 创建连接实例 3. 创建记录集实例 注意点: 1.AddNews失败问题: (1)是否将CursorTypeEnum cursorType, LockTypeEnum ...

  4. linux 下安装 jdk tomcat 并配置

    linux下安装TOMCAT与JDK 第1步:新建boss用户,username:boss,password:boss 第2步:将安装用户路径设置成/app/boss [root@localhost] ...

  5. js方式实现页面加遮罩效果

    有时候在页面上执行查询的时候由于数据量很大,需要较长时间,所以就需要在等待结果期间不可以操作页面,那么可以使用如下代码给页面添加遮罩效果: $.messager.progress({ title: ' ...

  6. MySQL中多表删除方法

    如果您是才接触MySQL数据库的新人,那么MySQL中多表删除是您一定需要掌握的,下面就将为详细介绍MySQL中多表删除的方法,供您参考,希望对你学习掌握MySQL中多表删除能有所帮助. 1.从MyS ...

  7. Js动态添加复选框Checkbox

    Js动态添加复选框Checkbox的实例方法!!! 首先,使用JS动态产生Checkbox可以采用如下类似的语句: var checkBox=document.createElement(" ...

  8. Python中使用UUID

    import uuid ... ... print uuid.uuid1() 生成的方法还有uuid2..n,具体参见官网LINK,包括参数细则

  9. atitit.spring hibernate的事务机制 spring不能保存对象的解决

    atitit.spring hibernate的事务机制 spring不能保存对象的解决 sessionFactory.openSession() 不能..log黑头马sql语言.. sessionF ...

  10. Vim-复制选中内容至系统剪贴板,光标移动到指定行的行首和行尾

    1.全选并复制到系统剪贴板 ggVG或ggvG 然后 "+y gg 让光标移到首行,在vim才有效,vi中无效 V 是进入Visual(可视)模式 G 光标移到最后一行 "+y 复 ...