zoukankan      html  css  js  c++  java
  • 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。

    javaweb--Rest访问(RestTemplate)

    RestFul几种方式

    工具类--Spring RestTemplate 工具封装

    RESTTEMPLATE 请求工具类

    远程调用工具类RestTemplateUtils

    RestTemplate使用教程

    RestTemplate的exchange()方法,解决put和delete请求拿不到返回值的问题

  • 相关阅读:
    swagger 兼容 docker 转发 配置
    rust 条件编译 Debug Release
    rust-must-know-crates-5ad8 100DayOfRust
    python C# DES 加密转换
    The Little Book of Rust Books
    swiper 禁止滑动
    uniapp自定义凸出的导航栏
    uniapp css实现双排菜单向左滑动
    uniapp开发公众号,微信设置字体大小后,禁止改变页面字体大小
    uniapp接口封装
  • 原文地址:https://www.cnblogs.com/edda/p/14089992.html
Copyright © 2011-2022 走看看