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

springboot使用RestTemplate以post方式发送json字符串参数(以向钉钉机器人发送消息为例)的更多相关文章

  1. httpclient工具类,post请求发送json字符串参数,中文乱码处理

    在使用httpclient发送post请求的时候,接收端中文乱码问题解决. 正文: 我们都知道,一般情况下使用post请求是不会出现中文乱码的.可是在使用httpclient发送post请求报文含中文 ...

  2. 钉钉自定义机器人 发送文本 换行 \n无效果

    今天用php做钉钉自定义机器人 发送文本 换行 \n无效果,原来是我一直用单引号作为定义字符串,换成双引号就ok了.

  3. spring mvc各种常见类型参数绑定方式以及json字符串绑定对象

    在使用spring mvc作为框架的时候,为了规范,我们通常希望客户端的请求参数符合规范直接通过DTO的方式从客户端提交到服务端,以便保持规范的一致性,除了很简单的情况使用RequestParam映射 ...

  4. ajax post 请求发送 json 字符串

    $.ajax({ // 请求方式 type:"post", // contentType contentType:"application/json", // ...

  5. Springboot 通过FastJson实现bean对象和Json字符串互转

    Json格式在后台服务中的重要性就不多说了,直入正题.首先引入pom文件,这里使用的是1.2.83版本 1 <dependency> 2 <groupId>com.alibab ...

  6. springMVC 接收json字符串参数

    /** 前台js拼接了一个数组 myparam = [a,b,c]; 在ajax中直接 {"myparam":JSON.stringify(myparam)} 传入springMV ...

  7. json字符串参数

    jsp部分        json字符串的属性应该都是实体类的属性 function saveCashier(){ layer.closeAll(); var Reapply = document.g ...

  8. 【spring mvc】spring mvc POST方式接收单个字符串参数,不加注解,接收到的值为null,加上@RequestBody,接收到{"uid":"品牌分类大”},加上@RequestParam报错 ---- GET方式接收单个参数的方法

    spring mvc POST方式 接收单个参数,不加任何注解,参数名对应,接收到的值为null spring mvc POST方式 接收单个参数,加上@RequestBody,接收到参数格式:{&q ...

  9. httpclient的调用 发送json字符串

    public static String postHttp(JSONObject jsonObject, String jsonUrl){ String responseMsg="" ...

随机推荐

  1. 1 java 笔记

    第一java的版本: J2ME主要用于移动设备和信息家电 J2SE整个Java技术的核心 J2EE java技术应用最广泛的部分,主要应用与企业的开发 第二:基于java语言的开源框架 struts ...

  2. 查看PHP指定扩展的版本信息

    命令:php --ri 扩展名

  3. Linux 硬盘UUID相同处理方法

    OVF模板部署的linux虚拟机磁盘id是相同的,当同一个模板生成的虚拟机挂载虚拟机磁盘时就会遇到两个磁盘UUID相同的情况,导致系统启动后只能识别一个磁盘.这里介绍一下LVM分区的磁盘UUID相同的 ...

  4. 使用Response下载(支持任何格式)

    使用Response下载 下面代码: protected void Button2_Click(object sender, EventArgs e) { Response.ContentType = ...

  5. bitcoind搭建

    https://degreesofzero.com/article/installing-bitcoind-on-ubuntu.html1. sudo apt-get install python-s ...

  6. springboot项目下载文件功能中-切面-导致的下载文件失败的bug

    背景:使用spring提供的 ResponseEntity 和Resource结合,实现的下载文件功能 bug:Resource已经加载到了文件, 并且通过 ResponseEntity 构建了响应, ...

  7. mysql查询疯狂41例

    援引自 http://www.cnblogs.com/wupeiqi/articles/5748496.html 一.表关系请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生 ...

  8. Java中的集合List、ArrayList、Vector、Stack(三)

    List接口 List集合代表一个有序集合,集合中每一个元素都有其对应的顺序索引.List集合容许使用重复元素,可以通过索引来访问指定位置的集合对象. ArrayList和Vector实现类 Arra ...

  9. [ERROR ] The Salt Master has cached the public key for this node, this salt minion will wait for 10 seconds before attempting to re-authenticate

    2.salt master已缓存此节点的公钥,此salt minion将等待10秒,然后再尝试重新验证. [ERROR ] The Salt Master has cached the public ...

  10. jQuery+masonry实现瀑布流

    增加jQuery组件 <script src="//cdn.bootcss.com/jquery/2.2.1/jquery.min.js "></script&g ...