使用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。