java RestTemplate请求

本文介绍了如何在Spring Boot中利用RestTemplate进行第三方API调用。详细讲解了当Content-type为'application/x-www-form-urlencoded'时,通过请求体和URL传递参数的方法,以及Content-type为'application/json'时请求体参数的传值方式,并提供了相关公共类的使用说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring boot 使用RestTemplate调用第三方

当Content-type为“application/x-www-form-urlencode”时

  1. 请求体参数传值方式
	HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type","application/x-www-form-urlencoded");
    MultiValueMap param = new LinkedMultiValueMap();
    param.add("abc","abc");
    HttpEntity httpEntity = new HttpEntity<>(param,headers);
    ResponseEntity<JSONObject> entity = RestTemplateUtil.post(url,httpEntity, JSONObject.class);
        
  1. 请求路径传值也就是url传值
GET

	HttpHeaders headers = new HttpHeaders();
	headers.add("Content-Type","application/x-www-form-urlencoded");
	UriComponentsBuilder builder = UriComponentsBuilder
			.fromUriString(url)
			.queryParam("pageNum", 2)
			.queryParam("pageSize",10);
	HttpEntity entity = new HttpEntity<>(headers);
	ResponseEntity<JSONObject> res = RestTemplateUtil.get(builder.toUriString(), headers, JSONObject.class);

POST	
	//格式一
	String url = "https://abc/{id}/{name}";
	HttpHeaders headers = new HttpHeaders();
	headers.add("Content-Type", "application/x-www-form-urlencoded");
	Map map = new HashMap();
	map.put("id",123);
	map.put("name","zhang");
	HttpEntity entity = new HttpEntity<>(headers);
	ResponseEntity<JSONObject> res = RestTemplateUtil.exchange(url,HttpMethod.POST,entity,JSONObject.class,map);
	
	//格式二
	String url = "https://abc?PageSize=10&pageNum=1&name=zhang";
	UriComponentsBuilder builder = UriComponentsBuilder
			.fromUriString(url)
			.queryParam("name","zhang")
			.queryParam("PageSize",10)
			.queryParam("CurPage",1);
	ResponseEntity<String> res = RestTemplateUtil.get(builder.toUriString(), String.class);
			

当Content-type为“application/json”时

  1. 请求体参数传值
String url = "https://abcde/authorize";
		Map headers = new HashMap();
		headers.put("Content-Type","application/json");
		//参数body json格式
		JSONObject jsonO = new JSONObject();
		jsonO.put("id",id);
		jsonO.put("name",name);
		jsonO.put("openId",openId);
		ResponseEntity<JSONObject> res = RestTemplateUtil.post(url,headers,jsonO,JSONObject.class);
		System.out.println(res.getBody());

公共类

package xxx.xxx.xxx.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Slf4j
public class RestTemplateUtil {
   
    private static final RestTemplate restTemplate = new RestTemplate();

    // ----------------------------------GET-------------------------------------------------------

    /**
     * GET请求调用方式
     *
     * @param url          请求URL
     * @param responseType 返回对象类型
     * @return ResponseEntity 响应对象封装类
     */
    public static <T> ResponseEntity<T> get(String url, Class<T> responseType) {
   
        return restTemplate.getForEntity(url, responseType);
    }

    /**
     * GET请求调用方式
     *
     * @param url          请求URL
     * @param responseType 返回对象类型
     * @param uriVariables URL中的变量,按顺序依次对应
     * @return ResponseEntity 响应对象封装类
     */
    public static <T> ResponseEntity<T> get(String url, Class<T> responseType, Object... uriVariables) {
   
        return restTemplate.getForEntity(url, responseType, uriVariables);
    }

    /**
     * GET请求调用方式
     *
     * @param url          请求URL
     * @param responseType 返回对象类型
     * @param uriVariables URL中的变量,与Map中的key对应
     * @return ResponseEntity 响应对象封装类
     */
    public static <T> ResponseEntity<T> get(String url, Class<T> responseType, Map<String, ?> uriVariables) {
   
        return restTemplate.getForEntity(url, responseType, uriVariables);
    }

    /**
     * 带请求头的GET请求调用方式
     *
     * @param url          请求URL
     * @param headers      请求头参数
     * @param responseType 返回对象类型
     * @param uriVariables URL中的变量,按顺序依次对应
     * @return ResponseEntity 响应对象封装类
     */
    public static <T> ResponseEntity<T> get(String url, Map
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值