springboot调用外部接口常用的方式
在系统开发过程中,免不了要去调用第三方接口,springboot为我们提供了几种调用第三方接口的方法
1、使用原始httpClient请求
1
2
3
4
5
|
< dependency > < groupId >org.apache.httpcomponents</ groupId > < artifactId >httpclient</ artifactId > < version >4.4.1</ version > </ dependency > |
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
|
public static JSONObject doPost(JSONObject date) { String assessToken= "" ; CloseableHttpClient client = HttpClients.createDefault(); // 要调用的接口url String url = "http://127.0.0.1:8080 /submit" ; HttpPost post = new HttpPost(url); JSONObject jsonObject = null ; try { //创建请求体并添加数据 StringEntity s = new StringEntity(date.toString()); //此处相当于在header里头添加请求头等参数 s.setContentType( "application/json" ); s.setContentEncoding( "UTF-8" ); post.setEntity(s); //此处相当于在Authorization里头添加Bear token参数信息 post.addHeader( "Authorization" , "Bearer " +assessToken); HttpResponse res = client.execute(post); String response1 = EntityUtils.toString(res.getEntity()); if (res.getStatusLine() .getStatusCode() == HttpStatus.SC_OK) { // 返回json格式: String result = EntityUtils.toString(res.getEntity()); jsonObject = JSONObject.parseObject(result); } } catch (Exception e) { throw new RuntimeException(e); } return jsonObject; } |
2、使用RestTemplate请求
1、get请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/** ** 入参 请求的url、响应体类型 */ RestTemplate restTemplate= new RestTemplate(); UriComponents uriComponents= UriComponentsBuilder.fromUriString( "http://127.0.0.1:8080/user?name={name}" ) .build() .expand( "jpl" ) .encode(); URI uri=uriComponents.toUri(); String body = restTemplate.getForEntity(uri, String. class ).getBody(); /** ** 入参 请求的url、响应体类型、请求体的参数 */ RestTemplate restTemplate= new RestTemplate(); Map params= new HashMap<>(); params.put( "name" , "dada" ); // ResponseEntity responseEntity = restTemplate.getForEntity( "http://127.0.0.1:8080/user?name={name}" , String. class , params); |
2、post请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public String submit(String documentId){ String assessToken= "" ; RestTemplate restTemplate = new RestTemplate(); //创建请求头 HttpHeaders httpHeaders = new HttpHeaders(); //此处相当于在Authorization里头添加Bear token参数信息 httpHeaders.add(HttpHeaders.AUTHORIZATION, "Bearer " + assessToken); //此处相当于在header里头添加content-type等参数 httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json" ); Map<String, Object> map = (Map<String, Object>) new HashMap<>().put( "documentId" , JSONObject.parse(documentId)); String jsonStr = JSON.toJSONString(map); //创建请求体并添加数据 HttpEntity<Map> httpEntity = new HttpEntity<Map>(map, httpHeaders); String url = "http://127.0.0.1:8080/submit" ; ResponseEntity<String> forEntity = restTemplate.postForEntity(url,httpEntity,String. class ); //此处三个参数分别是请求地址、请求体以及返回参数类型 return forEntity.toString(); } |
3、使用Feign请求
fegin常见咋微服务中使用,这里就不过多介绍
1
2
3
4
5
|
< dependency > < groupId >org.springframework.cloud</ groupId > < artifactId >spring-cloud-starter-feign</ artifactId > < version >1.2.2.RELEASE</ version > </ dependency > |
简单介绍一下如果使用feign遇到需要添加请求头等信息时,遇到重写feign的apply方法
1
2
3
4
5
6
7
8
|
@Configuration public class FeignConfig implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { //添加token requestTemplate.header( "token" , "" ); } } |
4、小结
RestTemplate、HTTPClient 和 Feign 都是 Java 中用于发送 HTTP 请求的工具,它们都可以用于构建 HTTPRestful API。以下是它们的优缺点:
RESTTemplate
优点:
RestTemplate 简单易用,支持自定义配置,可以更好地满足业务需求。
RestTemplate 可以处理 HTTP 请求和响应,实现对 HTTP 请求和响应的拦截、代理、拦截等功能。
缺点:
RestTemplate 的功能较为复杂,需要手动实现请求体、请求头、响应体、响应头等自定义配置,因此需要一定的编程经验和技能。
RestTemplate 默认情况下使用 Apache HTTP 库,因此在处理 HTTP 请求时,可能对某些业务场景不太友好,需要手动配置。
HTTPClient
优点:
HTTPClient 简单易用,支持多种 HTTP 库,可以更好地满足不同的业务需求。
HTTPClient 可以处理 HTTP 请求和响应,实现对 HTTP 请求和响应的拦截、代理、拦截等功能。
缺点:
HTTPClient 默认情况下使用 Apache HTTP 库,因此在处理 HTTP 请求时,可能对某些业务场景不太友好,需要手动配置。
HTTPClient 的性能可能不如 RestTemplate 。
Feign
优点:
Feign 是 Java 中的 HTTP客户端,可以支持多种 HTTP 库,可以更好地满足不同的业务需求。
Feign 可以实现自定义的请求拦截、响应拦截、请求体拦截、响应体拦截等功能,因此可以更好地满足业务需求。
Feign 内置了丰富的验证和认证功能,可以支持自定义的验证和认证规则。
缺点:
Feign 的功能较为复杂,需要手动实现请求拦截、响应拦截、请求体拦截、响应体拦截、验证和认证等功能,因此需要一定的编程经验
到此这篇关于常用的Spring Boot调用外部接口方式实现数据交互的文章就介绍到这了,更多相关Spring Boot调用外部接口内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_46657663/article/details/130164225