springboot RestTemplate used to send post json string parameters (staple to send a message to the robot as an example)

Before using springboot, so we send http messages are implemented

We used an outdated class, although some feel uncomfortable, but for some reason, has not done processing, the company recently changed the framework of the project springboot, springboot there is a very convenient http request is sent to realize, is RestTemplate, and very simple to implement, the code is very clear.

Can be seen from the above code, the parameter is transmitted to a staple json string, generic HttpEntity required should be String, if the key-value pair, to declare MultiValueMap <String, String> map = new LinkedMultiValueMap < > () ;, which is passed to the constructor HttpEntity as the first parameter.

Many types are defined MediaType, we use here is APPLICATION_JSON_UTF8, into the source code can be seen that the values ​​are attribute APPLICATION_JSON_UTF8_VALUE field corresponding to the attribute value APPLICATION_JSON_UTF8_VALUE application / json; charset = UTF-8, which what we need.

This embodiment json string is sent, the address lookup nails relatively simple robot, specific steps "group setting - Robot Group - Settings - Copy" to, specific code implementation is very simple, as follows

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。

Guess you like

Origin www.cnblogs.com/qq931399960/p/11420121.html