springboot使用RestTemplate以post方式发送json字符串参数(以向钉钉机器人发送消息为例)
使用springboot之前,我们发送http消息是这么实现的

我们用了一个过时的类,虽然感觉有些不爽,但是出于一些原因,一直也没有做处理,最近公司项目框架改为了springboot,springboot中有一种很方便的发送http请求的实现,就是RestTemplate,而且实现起来非常简单,代码也很清晰。
从上面代码可以看到,向钉钉发送的参数为一个json字符串,所以需要的HttpEntity的泛型应该是String,如果是键值对,就需要声明MultiValueMap<String, String> map = new LinkedMultiValueMap<>();,将其作为第一个参数传递到HttpEntity构造方法中。
MediaType中定义了很多类型,我们这里使用的为APPLICATION_JSON_UTF8,进入源码,可以看到,该字段对应的值为属性APPLICATION_JSON_UTF8_VALUE的值,而属性APPLICATION_JSON_UTF8_VALUE的值为application/json;charset=UTF-8,这也正是我们需要的。

本例发送的json字符串,查找钉钉机器人的地址比较简单,具体步骤为"群设置 -- 群机器人 -- 设置 -- 复制"即可,
需要的maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
如果只是这样引入依赖,则程序启动后,会启动一个内嵌的tomcat,程序将一直运行下去,如果程序是一次性的,执行完就想让其自动结束,并且不需要启动一个web项目,那么可以在上述依赖中去除对内嵌tocmat的依赖。在本例中,发送了钉钉消息之后就会结束程序,故去除了内嵌tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
具体代码实现也很简单,如下
package com.demo.webboot; import javax.annotation.Resource; import org.springframework.boot.CommandLineRunner;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; @Component
public class RestCommandLine implements CommandLineRunner { @Resource
private RestTemplate restTemplate; @Override
public void run(String... args) throws Exception { HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8); String content = "{ \"msgtype\": \"text\", \"text\": {\"content\": \"This is a test case.\"}, \"at\": {\"atMobiles\": [phone num], \"isAtAll\": false}}"; HttpEntity<String> request = new HttpEntity<>(content, headers); String url = "https://oapi.dingtalk.com/robot/send?access_token=65eff73abfd26a3e5e11dc87c2c8bcbf359f15b65cd1d3bcb60443307fba675a1";
ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, request, String.class); String body = postForEntity.getBody(); System.out.println(body);
} }
在启动类中配置RestTemplate,没有对RestTemplate做较多的处理,直接调用build方法创建。
package com.demo.webboot; import javax.annotation.Resource; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
public class WebbootApplication { @Resource
private RestTemplateBuilder builder; public static void main(String[] args) {
SpringApplication.run(WebbootApplication.class, args);
} @Bean
public RestTemplate restTemplate() {
return builder.build();
} }
运行结果如下

通过以上简单代码,就可以想钉钉机器人发送消息了,当然,这里只是一个demo。
springboot使用RestTemplate以post方式发送json字符串参数(以向钉钉机器人发送消息为例)的更多相关文章
- httpclient工具类,post请求发送json字符串参数,中文乱码处理
在使用httpclient发送post请求的时候,接收端中文乱码问题解决. 正文: 我们都知道,一般情况下使用post请求是不会出现中文乱码的.可是在使用httpclient发送post请求报文含中文 ...
- 钉钉自定义机器人 发送文本 换行 \n无效果
今天用php做钉钉自定义机器人 发送文本 换行 \n无效果,原来是我一直用单引号作为定义字符串,换成双引号就ok了.
- spring mvc各种常见类型参数绑定方式以及json字符串绑定对象
在使用spring mvc作为框架的时候,为了规范,我们通常希望客户端的请求参数符合规范直接通过DTO的方式从客户端提交到服务端,以便保持规范的一致性,除了很简单的情况使用RequestParam映射 ...
- ajax post 请求发送 json 字符串
$.ajax({ // 请求方式 type:"post", // contentType contentType:"application/json", // ...
- Springboot 通过FastJson实现bean对象和Json字符串互转
Json格式在后台服务中的重要性就不多说了,直入正题.首先引入pom文件,这里使用的是1.2.83版本 1 <dependency> 2 <groupId>com.alibab ...
- springMVC 接收json字符串参数
/** 前台js拼接了一个数组 myparam = [a,b,c]; 在ajax中直接 {"myparam":JSON.stringify(myparam)} 传入springMV ...
- json字符串参数
jsp部分 json字符串的属性应该都是实体类的属性 function saveCashier(){ layer.closeAll(); var Reapply = document.g ...
- 【spring mvc】spring mvc POST方式接收单个字符串参数,不加注解,接收到的值为null,加上@RequestBody,接收到{"uid":"品牌分类大”},加上@RequestParam报错 ---- GET方式接收单个参数的方法
spring mvc POST方式 接收单个参数,不加任何注解,参数名对应,接收到的值为null spring mvc POST方式 接收单个参数,加上@RequestBody,接收到参数格式:{&q ...
- httpclient的调用 发送json字符串
public static String postHttp(JSONObject jsonObject, String jsonUrl){ String responseMsg="" ...
随机推荐
- splice与slice区别
共同点:均是删除数组元素并返回结果. 区别:splice会改变原数组,而slice不会.并且splice会导致数组塌陷. 数组塌陷:使用splice删除元素时,剩余的数组元素索引的顺讯会改变. let ...
- About Spring MVC
一.简介 1.Springmvc是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解 ...
- Lab1 Report
Lab1 report A) The brief description that you install junit, hamcrest and eclemma. a) install junit ...
- BootStrap【二、样式】
H5文档类型 由于使用了H5和CSS熟悉,需要在文件头引入 移动设备优先 为了对移动设备友好,需要使用标签viewport width=device-width 宽度为设备宽度 height 高度 i ...
- Spark的Shuffle
0. Shuffle概述 要理解什么是Shuffle,首先介绍大数据与分布式.我们知道大数据的存储是分布式存储,大数据的计算框架是分布式的计算框架.分布式必然存在数据的交互传输,简言之Shuffle就 ...
- Image Processing and Analysis_8_Edge Detection:Theory of Edge Detection ——1980
此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...
- 微信小程序开发(四)页面跳转
承接上篇博客. 通过点击按钮跳转到新的页面. 先创建新页面home: 代码如下: // home.js Page({}) // 注册页面 // home.json {} // home.wxml &l ...
- docker HealthCheck健康检查
需求 最近遇到的问题:线上跑的一个 Node 镜像是在运行的,状态为 up ,但是访问报 502 ,重启镜像无效,重新拉了个镜像运行才恢复正常.于是想研究下如何从应用层面去检查容器的状态 为什么 do ...
- SVN建立分支和合并代码
1.SVN建立分支正确SVN服务器上会有两个目录:trunk和branches.trunk目录下面代码就是所谓的主版本,而branches文件夹主要是用来放置分支版本.分支版本是依赖于主版本的,因此建 ...
- okhttp拦截器之CallServerInterceptor解析
今天来学习OkHttp的最后一个拦截器,如下: 看一下它的javadoc说明: 其作用有两个:发起网络请求和接收服务器响应,下面具体来看一下它的intercept(): 下面具体来看一下: 接着就是读 ...