SpringBoot调用外部接口的几种方式
- 使用FeignClient调用
- 1、在使用方引入依赖
- 2、服务接口调用方
- 2.1、在启动类上加上@EnableFeigncliens注解
- 2.2、编写Feign接口调用服务controller层
- 2.3、服务接口调用service层
- 3、服务接口提供者
- 4、说明
- 使用RestTemplate调用
- 1、引入依赖
- 2、RestTemplateConfig配置类
- 3、接口调用
- 使用WebClient调用
- 1、引入依赖
- 2、接口调用示例
- 使用Apache HttpClient调用
- 使用HttpURLConnection调用
- 使用OkHttp调用
- 1、引入依赖
- 2、示例代码
- 使用AsyncHttpClient调用
- 1、引入依赖
- 2、示例代码
使用FeignClient调用
1、在使用方引入依赖
2、服务接口调用方
2.1、在启动类上加上@EnableFeigncliens注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class StudyfeignApplication {
public static void main(String[] args) {
SpringApplication.run(StudyfeignApplication.class, args);
System.out.println("项目启动成功");
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
2.2、编写Feign接口调用服务controller层
import com.hysoft.studyfeign.service.SysUserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("feign")
public class SysUserController {
@Autowired
private SysUserClient sysUserClient;
@PostMapping("getUserId")
public void getUserId(String userId){
this.sysUserClient.getUserById(userId);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
2.3、服务接口调用service层
application.yml中的配置如下
3、服务接口提供者
4、说明
使用RestTemplate调用
1、引入依赖
2、RestTemplateConfig配置类
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);//单位为ms
factory.setConnectTimeout(5000);//单位为ms
return factory;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
3、接口调用
@RestController
public class TestRestTemplate {
@Resource
private RestTemplate restTemplate;
@GetMapping(value = "/saveUser")
public void saveUser(String userId) {
String url = "http://127.0.0.1:8080/master/test";
Map map = new HashMap<>();
map.put("userId", "hy001");
String results = restTemplate.postForObject(url, map, String.class);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
使用WebClient调用
1、引入依赖
2、接口调用示例
public class TestWebClient {
@Test
public void doGet() {
String userId = "郭郭";
String url = "http://127.0.0.1:8080/master/test/getSysUserById?userId={userId}";
Mono<String> mono = WebClient
//创建WebClient实例
.create()
//方法调用,WebClient中提供了多种方法
.get()
//请求url
.uri(url, userId)
//获取响应结果
.retrieve()
//将结果转换为指定类型
.bodyToMono(String.class);
//返回最终结果:block是阻塞的/subscribe()非阻塞式获取响应结果
System.out.println("响应结果:" + mono.block());
}
@Test
public void doPost() {
Map map = new HashMap<>();
map.put("name", "郭郭");
String requestBody = JSON.toJSONString(map);
String url = "http://127.0.0.1:8080/master/test/saveUser";
Mono<String> mono = WebClient
//创建WebClient实例
.create()
//方法调用,WebClient中提供了多种方法
.post()
//请求url
.uri(url)
//指定请求的Content-Type为JSON
.contentType(MediaType.APPLICATION_JSON)
//使用bodyValue方法传递请求体
.bodyValue(requestBody)
//获取响应结果
.retrieve()
//将结果转换为指定类型
.bodyToMono(String.class);
//返回最终结果:block是阻塞的/subscribe()非阻塞式获取响应结果
System.out.println("响应结果:" + mono.block());
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
使用Apache HttpClient调用
public class TestHttpClient {
@Test
public void doGet() throws IOException {
//步骤一:创建httpClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
//步骤二:创建HTTP请求
HttpGet httpGet = new HttpGet("http://127.0.0.1:8094/masterdata/sysUser/getSysUserById?userId=郭郭");
//步骤三:发送请求并获取响应数据
CloseableHttpResponse response = httpClient.execute(httpGet);
//步骤四:处理响应数据
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
//步骤五:关闭httpClient和response
response.close();
httpClient.close();
}
@Test
public void doPost() throws IOException {
//步骤一:创建httpClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
//步骤二:创建HTTP请求
HttpPost httpPost = new HttpPost("http://127.0.0.1:8094/masterdata/sysUser/saveUser");
//步骤三:设置请求体数据,使用JSON格式
Map map = new HashMap<>();
map.put("name", "郭郭");
String requestBody = JSON.toJSONString(map);
StringEntity stringEntity = new StringEntity(requestBody, "UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
//步骤四:发送请求并获取响应数据
CloseableHttpResponse response = httpClient.execute(httpPost);
//步骤五:处理响应数据
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
//步骤五:关闭httpClient和response
response.close();
httpClient.close();
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
使用HttpURLConnection调用
public class TestHttpURLConnection {
@Test
public void doGet() throws IOException {
String userId = "郭郭"; // 参数值
userId = URLEncoder.encode(userId, "UTF-8"); // 对参数值进行URL编码
//步骤一:创建URL对象
URL url = new URL("http://127.0.0.1:8094/masterdata/sysUser/getSysUserById?userId=" + userId);
//步骤二:打开连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//步骤三:设置请求方式
conn.setRequestMethod("GET");
//步骤四:读取响应内容
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
System.out.println(sb.toString());
}
@Test
public void doPost() throws IOException {
//创建URL对象
URL url = new URL("http://127.0.0.1:8094/masterdata/sysUser/saveUser");
//打开连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置请求方式
conn.setRequestMethod("POST");
// 设置请求头
conn.setRequestProperty("Content-Type", "application/json");
//启用输出流
conn.setDoOutput(true);
//设置请求体数据
Map map = new HashMap<>();
map.put("name", "郭郭");
String requestBody = JSON.toJSONString(map);
//发送请求体数据
try (DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream())) {
outputStream.write(requestBody.getBytes(StandardCharsets.UTF_8));
}
//读取响应内容
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
System.out.println(sb.toString());
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
使用OkHttp调用
1、引入依赖
2、示例代码
public class TestOkHttp {
@Test
public void doGet() throws IOException {
OkHttpClient client = new OkHttpClient();
String url = "http://127.0.0.1:8080/master/test/getSysUserById?userId=郭郭";
Request request = new Request.Builder().url(url).build();
try (Response response = client.newCall(request).execute()) {
ResponseBody body = response.body();
System.out.println(body.string());
}
}
@Test
public void doPost() throws IOException{
OkHttpClient client = new OkHttpClient();
String url = "http://127.0.0.1:8080/master/test/saveUser";
MediaType mediaType = MediaType.get("application/json; charset=utf-8");
//requestBody请求入参
Map map = new HashMap<>();
map.put("name", "admin");
RequestBody requestBody = RequestBody.create(mediaType, JSON.toJSONString(map));
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
try (Response response = client.newCall(request).execute()) {
ResponseBody body = response.body();
System.out.println(body.string());
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
使用AsyncHttpClient调用
1、引入依赖
2、示例代码
public class TestAsyncHttpClient {
@Test
public void doGet() throws IOException {
try (AsyncHttpClient client = new DefaultAsyncHttpClient();) {
BoundRequestBuilder requestBuilder = client.prepareGet("http://127.0.0.1:8080/master/test/getSysUserById?userId=hy001");
CompletableFuture<String> future = requestBuilder.execute()
.toCompletableFuture()
.thenApply(Response::getResponseBody);
//使用join等待响应完成
String responseBody = future.join();
System.out.println(responseBody);
}
}
@Test
public void doPost() throws IOException {
try (AsyncHttpClient client = new DefaultAsyncHttpClient();) {
BoundRequestBuilder requestBuilder = client.preparePost("http://127.0.0.1:8094/8080/master/test/saveUser");
//requestBody请求入参
Map map = new HashMap<>();
map.put("name", "admin");
String requestBody = JSON.toJSONString(map);
requestBuilder.addHeader("Content-Type", "application/json");
requestBuilder.setBody(requestBody);
CompletableFuture<String> future = requestBuilder.execute()
.toCompletableFuture()
.thenApply(Response::getResponseBody);
//使用join等待响应完成
String responseBody = future.join();
System.out.println(responseBody);
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.