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。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了