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

  • 相关阅读:
    android动画坐标定义
    Android animation 动画背景图自动播放的实现
    松开手,你可以拥有更多
    Serializable继承和用途
    程序员必备的国外IT网站
    android 解析json数据格式
    免费的天气预报API谷歌,雅虎,中央气象台
    关于Lockfree Hash Table的一些链接资料
    字符串匹配算法之Brute force algorithm
    java 处理高精度计算
  • 原文地址:https://www.cnblogs.com/qq931399960/p/11420121.html
Copyright © 2020-2023  润新知