Springboot RestTemplate 简单使用解析

网友投稿 311 2022-12-31


Springboot RestTemplate 简单使用解析

前言

spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接, 我们只需要传入url及返回值类型即可。

相较于之前常用的HttpClient,RestTemplate是一种更优雅的调用RESTful服务的方式。该类主要用到的函数有:exchange、getForEntity、postForEntity等。我主要用的是后面两个函数,来执行发送get跟post请求。

首先是RestTemplateUtil类

package cn.eangaie.demo.util;

import com.alibaba.fastjson.JSONObject;

import org.springframework.http.*;

import org.springframework.stereotype.Component;

import org.springframework.util.MultiValueMap;

import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**

* @author Eangaie

* @date 2018/10/12 0012 下午 14:53

* 网络请求,RestTemplate工具类

*/

@Component

public class RestTemplateUtil {

private RestTemplate restTemplate;

/**

* 发送GET请求

* @param url

* @param param

* @return

*/

public String GetData(String url, Map param){

restTemplate=new RestTemplate();

// 请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交

HttpHeaders headers = new HttpHeaders();

headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

return restTemplate.getForEntity(url,String.class,param).getBody();

}

/**

* 发送POST-JSON请求

* @param url

* @param param

* @return

*/

public String PostJsonData(String url,JSONObject param){

restTemplate=new RestTemplate();

HttpHeaders headers = new HttpHeaders();

headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

headers.add("Accept", MediaType.APPLICATION_JSON.toString());

HttpEntity requestEntity = new HttpEntity(param, headers);

return restTemplate.postForEntity(url,param,String.class).getBody();

}

/**

* 发送POST 表单请求

* @param url

* @param param

* @return

*/

public String PostFormData(String url,MultiValueMap param){

restTemplate=new RestTemplate();

// 请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交

HttpHeaders headers = new HttpHeaders();

headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

return restTemplate.postForEntity(url,param,String.class).getBody();

}

}

这里编写了三个函数,一个是GetData(), 用来发送GET请求,使用方法是问号传参,并把参数的key作为替代,在map中填入。

PostJsonData ()是用来发送json类型数据的POST请求。需要传入url链接,和一个JSONObject对象。PostFormData()函数是用来发送表单类型

的POST请求。 使用方式我也编写了一些简单的控制器。代码如下。

@GetMapping("testGetData")

public String testGetData(){

String url="http://localhost:81/sample/GetData?msg={msg}&author={author}";

Map param=new HashMap<>();

param.put("msg","Hello");

param.put("author","彦杰");

return restTemplateUtil.GetData(url,param);

}

@GetMapping("testPostJsonData")

public String testPostJsonData(){

String url="http://localhost:81/sample/PostData";

JSONObject jsonObject=new JSONObject();

jsonObject.put("msg","hello");

jsonObject.put("author","彦杰");

return restTemplateUtil.PostJsonData(url,jsonObject);

}

@GetMapping("testPostFormData")

public String testPostFormData(){

String url="http://localhost:81/sample/PostFormData";

MultiValueMap param=new LinkedMultiValueMap<>();

param.add("msg","Hello");

param.add("author","彦杰");

return restTemplateUtil.PostFormData(url,param);

}

@GetMapping("GetData")

public String getData(String msg, String author){

return msg+" "+author;

}

@PostMapping("PostData")

public String postData(@RequestBody JSONObject jsonObject){

String msg=jsonObject.getString("msg");

String author=jsonObject.getString("author");

return msg+" "+author;

}

@PostMapping("PostFormData")

public String PostFormData(String msg,String author){

return msg+" "+author;

}

@GetMapping("testGetData")

public String testGetData(){

String url="http://localhost:81/sample/GetData?msg={msg}&author={author}";

Map param=new HashMap<>();

param.put("msg","Hello");

param.put("author","彦杰");

return restTemplateUtil.GetData(url,param);

}

@GetMapping("testPostJsonData")

public String testPostJsonData(){

String url="http://localhost:81/sample/PostData";

JSONObject jsonObject=new JSONObject();

jsonObject.put("msg","hello");

jsonObject.put("author","彦杰");

return restTemplateUtil.PostJsonData(url,jsonObject);

}

@GetMapping("testPostFormData")

public String testPostFormData(){

String url="http://localhost:81/sample/PostFormData";

MultiValueMap param=new LinkedMultiValueMap<>();

param.add("msg","Hello");

param.add("author","彦杰");

return restTemplateUtil.PostFormData(url,param);

}

@GetMapping("GetData")

public String getData(String msg, String author){

return msg+" "+author;

}

@PostMapping("PostData")

public String postData(@RequestBnfETCody JSONObject jsonObject){

String msg=jsonObject.getString("msg");

String author=jsonObject.getString("author");

return msg+" "+author;

}

@PostMapping("PostFormData")

public String PostFormData(String msg,String author){

return msg+" "+author;

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微服务网关有啥用(微服务网关高可用)
下一篇:关于跨系统接口测试工具是什么的信息
相关文章

 发表评论

暂时没有评论,来抢沙发吧~