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="" ...
随机推荐
- jQuery 手写菜单(ing)
菜单支持多级 直接上代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- oracle的listagg函数
今天需要将 BDST_ID相同的PROJECT_ID用逗号分隔拼成一个字符串,于是想到了oracle的listagg函数 表名为PM_BDST_PROJECT select tt.BDST_ID, l ...
- 用Leangoo看板进行可视化的缺陷跟踪管理
转自:https://www.leangoo.com/10464.html 缺陷管理通常关注如下几个方面: 1. 缺陷的处理速度 2. 缺陷处理的状态 3. 缺陷的分布 4. 缺陷产生的原因 使用Le ...
- interrupt分析
转载自 https://blog.csdn.net/zhangliangzi/article/details/52485319 interrupt简述 interrupt() 方法只是改变中断状态而已 ...
- JAVA对ArrayList排序
ava如何对ArrayList中对象按照该对象某属性排序 增加排序功能,打印时:输出学生对象的时候,需要先按照年龄排序,如果年龄相同,则按照姓名排序,如果姓名也相同,则按照学号排序. Code hig ...
- Qt 中配置 c99的问题
Qt 5.3 版本 报错原因是c99标准问题的话,可以尝试下面方法 打开项目中xxx.pro工程文件 加入如下语句: QMAKE_CFLAGS += -std=c99
- functools.lru_cache装饰器
functools.lru_cache装饰器 functools.lru_cache是非常实用的装饰器,他实现了备忘功能它把耗时的函数的结果保存起来,避免传入相同的参数时重复计算.LRU是Least ...
- kubernetes之configmap
生成容器内的环境变量 1.创建一个名字叫nginx-config的configmap, 变量名nginx_port的值是80, 变量名server_name的值是www.test.com kubect ...
- 接口调用实现类&& 为什么Autowired定义在接口上
1.接口与回调 package edu.cqu.interfaceTest; import java.awt.Toolkit; import java.awt.event.ActionEvent; i ...
- prometheus部署
1.prometheus安装 软件下载: wget https://dl.grafana.com/oss/release/grafana-6.4.2-1.x86_64.rpm https://gith ...