java使用RestTemplate发起各种httpt请求

文章目录

  • 场景描述
  • 一、RestTemplate是什么?
  • 二、使用实例
    • 1.引入库
    • 2.带附件post请求(multipart/form-data)
    • 3.无附件发起post请求
    • 4.发起get请求
  • 三、RestTemplate提供的各种方法


场景描述

在springboot项目中使用RestTemplate发起http请求访问第三方接口


一、RestTemplate是什么?

RestTemplate是Spring提供的用于访问Rest服务的客户端,它提供了很多可以方便访问远程http服务的方法,这些方法可以帮助开发人员减少编写客户端代码的工作量。

二、使用实例

1.引入库

代码如下(示例):

import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

2.带附件post请求(multipart/form-data)

代码如下(示例):

public static String httpPostFileUrl(String postAnnexIdUrl, String token, AnnexFile annexFile){
        //处理流文件
        FileOutputStream fos = null;
        String name = annexFile.getName() + "." + annexFile.getExtension();
        try {
            // 将文件存入输出流:将输出流的数据写入到文件
            fos = new FileOutputStream(name);
            byte[] b = annexFile.getContent();
            fos.write(b);
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        //发送请求
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        FileSystemResource fileSystemResource = new FileSystemResource(new File(name));
        MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
        httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
        requestBody.add("file", fileSystemResource);
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(requestBody, httpHeaders);
        String url = postAnnexIdUrl + "?token=" + token;
        String res = restTemplate.postForObject(url, httpEntity, String.class);

        //请求后删除生成的临时文件
        Path path = Paths.get(name);
        try {
            Files.delete(path);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //解析返回的json
        return res;
    }

该处使用的url网络请求的数据。

3.无附件发起post请求

代码如下(示例):

 public static Result httpPostResult(String postResultUrl,String token,String xml) {
        //发送请求
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_XML);
        HttpEntity<String> httpEntity = new HttpEntity<>(xml,httpHeaders);
        String url = postResultUrl + "?token=" + token;
        String res = restTemplate.postForObject(url,httpEntity,String.class);
        //解析请求结果
        return res ;
    }

4.发起get请求

代码如下(示例):

public static String httpGetTokenId(String tokenIdUrl, String userName) {
        RestTemplate restTemplate = new RestTemplate();
        String url = tokenIdUrl+ "?loginName=" + userName;
        String response = restTemplate.getForObject(url, String.class);
        return response;
    }

三、RestTemplate提供的各种方法

postForLocation
postForLocation
postForLocation
postForObject
postForObject
postForObject
postForEntity
postForEntity
postForEntity
put
put
put
patchForObject
patchForObject
patchForObject
delete
delete
delete
optionsForAllow
optionsForAllow
optionsForAllow
exchange
exchange
exchange
exchange
exchange
exchange
exchange
exchange
execute
execute
execute
doExecute

你可能感兴趣的:(java,spring,java,spring,开发语言,spring,boot)