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