java使用RestTemplate发起各种httpt请求


场景描述

在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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值