1、springboot调用外部接口的几种方式
- 原始httpclient
- resttemplate
- 第三方库,例如 okhttp,hutool等
本文着重介绍resttemplate方式
2、什么是resttemplate
resttemplate是spring 框架提供的 ,可用于在应用中调用 rest 服务,它简化了与 http 服务的通信方式,统一了 restful 的标准,封装了 http 链接, 我们只需要传入url及返回值类型即可。相较于之前常用的 httpclient,resttemplate 是一种更优雅的调用 restful 服务的方式。
3、springboot中使用resttmplate
3.1、前置准备
1、创建一个普通的springboot项目
2、创建一个配置类,
package com.eric.springbootresttemplate.config;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.web.client.resttemplate;
/**
* @author eric
* @date 2023-08-03 9:37
*/
@configuration
public class resttemplateconfig {
@bean
public resttemplate resttemplate() {
return new resttemplate();
}
}
3、准备一个实体类
package com.eric.springbootresttemplate.entity;
import lombok.builder;
import lombok.data;
/**
* @author eric
* @date 2023-08-03 9:33
*/
@data
@builder
public class user {
private long id;
private string name;
}
4、创建两个被请求的接口(模拟其他服务接口)
@getmapping("/getuserinfo")
public user getuserlist(long id, string name) {
user user = user.builder().id(id).name(name).build();
return user;
}
@postmapping("/postuserinfo")
public user postuserinfo(@requestbody jsonobject jsonobject) {
long id = jsonobject.getlong("id");
string name = jsonobject.getstring("name");
user user = user.builder().id(id).name(name).build();
return user;
}
3.2、get请求方式
get有两种请求方式,一种正常的在url拼接参数,一种是使用占位符的方式
方式一:参数拼接
@resource
private resttemplate resttemplate;
/**
* get请求方式一:参数拼接
* @param id
* @param name
*/
@getmapping("/getuserinforest")
public void getuserinforest(long id, string name) {
string url = "http://localhost:8080/rest/getuserinfo?id=" + id + "&name=" + name;
responseentity<string> result = resttemplate.getforentity(url, string.class);
system.out.println(result.getstatuscode());
system.out.println(result.getbody());
system.out.println(result.getheaders());
}
方式二:占位符方式
直接在方法中指定参数:
@resource
private resttemplate resttemplate;
/**
* get请求方式二:占位符方式
* @param id
* @param name
*/
@getmapping("/getuserinforest2")
public void getuserinforest2(long id, string name) {
string url = "http://localhost:8080/rest/getuserinfo?id={id}&name={name}";
responseentity<string> result = resttemplate.getforentity(url, string.class,id,name);
system.out.println(result.getstatuscode());
system.out.println(result.getbody());
system.out.println(result.getheaders());
}
两种方式的执行结果如下:

3.3、post请求
/**
* post请求方式:带@requestbody
*/
@postmapping("/postuserinforest")
public void postuserinforest() {
map<string, object> requestbody = new hashmap<>();
requestbody.put("id", 1l);
requestbody.put("name", "eric");
httpheaders requestheaders = new httpheaders();
requestheaders.setcontenttype(mediatype.application_json);
httpentity<map<string, object>> r = new httpentity<map<string, object>>(requestbody, requestheaders);
string url = "http://localhost:8080/rest/postuserinfo";
string result = resttemplate.postforobject(url, r, string.class);
system.out.println(result);
}
结果如下:

3.4、解决中文乱码
上述请求可能会存在中文乱码,只需要额外设置下即可
resttemplate resttemplate = new resttemplate(); resttemplate.getmessageconverters().set(1, new stringhttpmessageconverter(standardcharsets.utf_8));
3.5、封装工具类
在日常中,我们可以直接将get和post简单的封装为一个工具类, 方便我们直接调用(这里我只是简单的封装了下,大家如果可以根据自己的使用场景再次封装~)
package com.wzhy.smart.common.common.utils;
package com.eric.springbootresttemplate.utils;
import lombok.extern.slf4j.slf4j;
import org.springframework.http.httpentity;
import org.springframework.http.httpheaders;
import org.springframework.http.mediatype;
import org.springframework.http.converter.stringhttpmessageconverter;
import org.springframework.web.client.resttemplate;
import java.nio.charset.standardcharsets;
import java.util.map;
/**
* resttemplate请求工具类
*
* @author eric
* @date 2023-08-03 10:36
*/
@slf4j
public class resttemplateutil {
/**
* post请求
*
* @param url 请求路径
* @param parameter 请求参数
* @return 返回值
*/
public static string post(string url, map<string, object> parameter) {
resttemplate resttemplate = new resttemplate();
resttemplate.getmessageconverters().set(1, new stringhttpmessageconverter(standardcharsets.utf_8));
httpheaders requestheaders = new httpheaders();
requestheaders.setcontenttype(mediatype.application_json);
httpentity<map<string, object>> r = new httpentity<map<string, object>>(parameter, requestheaders);
string body = resttemplate.postforobject(url, r, string.class);
// log.info("远程调用结果,body为:{}", body);
return body;
}
/**
* get请求
*
* @param url 请求路径
* @param parameter 请求参数
* @return 返回值
*/
public static string get(string url, map<string, object> parameter) {
if (!parameter.isempty()) {
url = url + "?";
for (string key : parameter.keyset()) {
url = url + key + "=" + parameter.get(key) + "&";
}
url = url.substring(0, url.length() - 1);
}
resttemplate resttemplate = new resttemplate();
resttemplate.getmessageconverters().set(1, new stringhttpmessageconverter(standardcharsets.utf_8));
string body = resttemplate.getforentity(url, string.class, parameter).getbody();
// log.info("远程调用结果,body为:{}", body);
return body;
}
}
总结
到此这篇关于springboot使用resttemplate发送http请求的实操演示的文章就介绍到这了,更多相关springboot resttemplate发送http请求内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论