springboot使用RestTemplate以post方式发送json字符串参数(以向钉钉机器人发送消息为例)

使用springboot以前,咱们发送http消息是这么实现的java

咱们用了一个过期的类,虽然感受有些不爽,可是出于一些缘由,一直也没有作处理,最近公司项目框架改成了springboot,springboot中有一种很方便的发送http请求的实现,就是RestTemplate,并且实现起来很是简单,代码也很清晰。web

从上面代码能够看到,向钉钉发送的参数为一个json字符串,因此须要的HttpEntity的泛型应该是String,若是是键值对,就须要声明MultiValueMap<String, String> map = new LinkedMultiValueMap<>();,将其做为第一个参数传递到HttpEntity构造方法中。spring

MediaType中定义了不少类型,咱们这里使用的为APPLICATION_JSON_UTF8,进入源码,能够看到,该字段对应的值为属性APPLICATION_JSON_UTF8_VALUE的值,而属性APPLICATION_JSON_UTF8_VALUE的值为application/json;charset=UTF-8,这也正是咱们须要的。json

本例发送的json字符串,查找钉钉机器人的地址比较简单,具体步骤为"群设置 -- 群机器人 -- 设置  -- 复制"便可,api

须要的maven依赖tomcat

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>

若是只是这样引入依赖,则程序启动后,会启动一个内嵌的tomcat,程序将一直运行下去,若是程序是一次性的,执行完就想让其自动结束,而且不须要启动一个web项目,那么能够在上述依赖中去除对内嵌tocmat的依赖。在本例中,发送了钉钉消息以后就会结束程序,故去除了内嵌tomcatspringboot

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

具体代码实现也很简单,以下app

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(); } }

运行结果以下maven

经过以上简单代码,就能够想钉钉机器人发送消息了,固然,这里只是一个demo。

相关文章
相关标签/搜索