SpringBoot 访问外部接口

在SpringBoot接口开发中,有很大的概率需要访问外面模块接口或外部url 链接的需求,SpringBoot中有哪些方式可以调用外部接口呢?

访问外部接口的常见方案

方案一: 采用原生的Http请求

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
@RequestMapping("/doPostGetJson")
public String doPostGetJson() throws ParseException {
//此处将要发送的数据转换为json格式字符串
String jsonText = "{id: 1}";
JSONObject json = (JSONObject) JSONObject.parse(jsonText);
JSONObject sr = this.doPost(json);
System.out.println("返回参数: " + sr);
return sr.toString();
}

public static JSONObject doPost(JSONObject date) {
HttpClient client = HttpClients.createDefault();
// 要调用的接口方法
String url = "http://192.168.1.101:8080/getJson";
HttpPost post = new HttpPost(url);
JSONObject jsonObject = null;
try {
StringEntity s = new StringEntity(date.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
post.setEntity(s);
post.addHeader("content-type", "text/xml");
HttpResponse res = client.execute(post);
String response1 = EntityUtils.toString(res.getEntity());
System.out.println(response1);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
jsonObject = JSONObject.parseObject(result);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return jsonObject;
}

方案二: 采用Feign进行消费

pom

1
2
3
4
 <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

编写接口,放置在service层

这里的decisionEngine.url 是配置在properties中的 是ip地址和端口号

1
2
3
4
5
6
7
@FeignClient(url = "${decisionEngine.url}",name="engine")
public interface DecisionEngineService {
  @RequestMapping(value="/decision/person",method= RequestMethod.POST)
  public JSONObject getEngineMesasge(@RequestParam("uid") String uid,@RequestParam("productCode") String productCode);

}

在Java的启动类上加上@EnableFeignClients

1
2
3
4
5
6
7
8
@SpringBootApplication
@ComponentScan({"com.chen.*", "com.chen.……"})
@EnableFeignClients
public class ServiceSmsApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceSmsApplication.class, args);
}
}

方案三: 采用RestTemplate方法

Get请求

Get请求之——getForEntity(Stringurl,Class responseType,Object…urlVariables)

该方法提供了三个参数,其中url为请求的地址,responseType为请求响应body的包装类型,urlVariables为url中的参数绑定,该方法的参考调用如下:

1
2
3
4
5
// http://USER-SERVICE/user?name={name)
RestTemplate restTemplate=new RestTemplate();
Map<String,String> params=new HashMap<>();
params.put("name","dada"); //
ResponseEntity<String> responseEntity=restTemplate.getForEntity("http://USERSERVICE/user?name={name}",String.class,params);

Get请求之——getForEntity(URI url,Class responseType)

该方法使用URI对象来替代之前的 url 和 urlVariables 参数来指定访问地址和参数绑定。URI 是 JDK java.net 包下的一个类,表示一个统一资源标识符(Uniform Resource Identifier)引用。参考如下:

1
2
3
4
5
6
7
RestTemplate restTemplate=new RestTemplate();
UriComponents uriComponents=UriComponentsBuilder.fromUriString("http://USER-SERVICE/user?name={name}")
.build()
.expand("czk")
.encode();
URI uri=uriComponents.toUri();
ResponseEntity<String> responseEntity=restTemplate.getForEntity(uri,String.class).getBody();

Get请求之——getForObject

getForObject方法可以理解为对getForEntity的进一步封装,它通过HttpMessageConverterExtractor对HTTP的请求响应体body内容进行对象转换,实现请求直接返回包装好的对象内容。getForObject方法有如下:

1
2
3
getForObject(String url,Class responseType,Object...urlVariables)
getForObject(String url,Class responseType,Map urlVariables)
getForObject(URI url,Class responseType)

Post 请求

Post请求提供有三种方法,postForEntity、postForObject和postForLocation。其中每种方法都存在三种方法,postForEntity方法使用如下:

1
2
3
4
RestTemplate restTemplate=new RestTemplate();
User user=newUser("czk",18);
ResponseEntity<String> responseEntity=restTemplate.postForEntity("http://USER-SERVICE/user",user,String.class); //提交的body内容为user对象,请求的返回的body类型为String
String body=responseEntity.getBody();

postForEntity存在如下三种方法的重载

1
2
3
postForEntity(String url,Object request,Class responseType,Object... uriVariables)
postForEntity(String url,Object request,Class responseType,Map uriVariables)
postForEntity(URI url,Object request,Class responseType)

postForEntity中的其它参数和getForEntity的参数大体相同在此不做介绍。

在接口调用中需要注意

需要注意两点:

  1. 需要设置超时时间
  2. 需要自行处理异常