spring调用第三方接口

post请求

  • content-type:json
userData.put("accountid", "-1");
//设置请求头
HttpHeaders headers = new HttpHeaders();      
headers.add("Accept", "application/json");
headers.add("Content-Type", contentType);
//userData 是请求体body
HttpEntity<Object> entity = new HttpEntity<>(userData, headers);

ResponseEntity resp = HttpUtil.restTemplate.exchange(请求url, HttpMethod.POST, entity, Object.class);
						
  • content-type:application/x-www-form-urlencoded
MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<>();
postParameters.put("param1", Collections.singletonList(param1));

HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
HttpEntity<Object> entity = new HttpEntity<>(postParameters, headers);

ResponseEntity responseEntity = HttpUtil.restTemplate.exchange(hangupUrl, HttpMethod.POST, entity, Object.class);
  • postForEntity、postForObject、postForLocation同getForEntity。

get请求

  • getForEntity
//第一种
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://uri?name={1}", String.class, "张三");

//第二种
Map<String, String> map = new HashMap<>();
map.put("name", "李四");
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://uri?name={name}", String.class, map);
//第三种
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://uri?name={name}").build().expand("王五").encode();
URI uri = uriComponents.toUri();
ResponseEntity<String> responseEntity = restTemplate.getForEntity(uri, String.class);
  • getForObject同getForEntity

put请求

restTemplate.put("http://uri/{1}", book, 99);

delete请求

restTemplate.delete("http://uri/{1}", 100);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Spring Boot 调用第三方接口通常涉及以下几个步骤: 1. 添加依赖:首先,在你的Spring Boot项目中,需要添加对第三方API库的依赖,这通常是通过Maven或Gradle的`dependencies`块来完成。例如,如果你使用的是RestTemplate、Feign或者Retrofit等客户端库,就需要相应的依赖声明。 ```xml // Maven <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>retrofit</artifactId> <version>2.x.x</version> </dependency> // Gradle (Kotlin) implementation 'com.squareup.retrofit2:retrofit:2.x.x' ``` 2. 创建接口:按照第三方API的文档,创建一个或者多个模拟服务接口(Service Interface),这个接口通常会包含你将要调用的所有方法的定义。 ```java public interface ExternalApi { @GetMapping("api-url") Call<String> getData(); } ``` 3. 实现客户端:使用`@Autowired`注入`RestTemplate`、`FeignClient`或`Retrofit`实例,并配置连接细节(如URL、认证信息等)。然后你可以直接调用接口方法来发送请求。 ```java @Autowired private RestTemplate restTemplate; // 或者 @Service @FeignClient(name = "external-api", url = "http://example.com/api") public interface ExternalApiFeign { @GetMapping("/data") String getData(); } ``` 4. 发送请求并处理响应:在业务逻辑中,使用创建的客户端实例来发起请求,并处理返回的数据。 ```java String response = restTemplate.getForObject(externalApi.getData().request(), String.class); ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值