Spring Boot - 访问外部接口最全总结

作者:@pdai
本文为作者原创,转载请注明出处:https://www.cnblogs.com/pengdai/p/11764243.html


内容目录

 


Spring Boot - 访问外部接口

在 Spring-Boot 项目开发中,存在着本模块的代码需要访问外面模块接口,或外部 url 链接的需求, 比如调用外部的地图 API 或者天气 API。

最全的 Java 后端知识体系 https://www.pdai.tech, 每天更新中...

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

在代码中采用原生的 http 请求,代码参考如下:

@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 进行消费

1、在 maven 项目中添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
    <version>1.2.2.RELEASE</version>
</dependency>

2、编写接口,放置在 service 层

这里的 decisionEngine.url 是配置在 properties 中的 是 ip 地址和端口号
decisionEngine.url=http://10.2.1.148:3333/decision/person 是接口名字

@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);

}

3、在 Java 的启动类上加上 @EnableFeignClients

@EnableFeignClients // 参见此处
@EnableDiscoveryClient
@SpringBootApplication
@EnableResourceServer
public class Application   implements CommandLineRunner {
    private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
    @Autowired
    private AppMetricsExporter appMetricsExporter;
<span class="hljs-meta">@Autowired</span>
<span class="hljs-keyword">private</span> AddMonitorUnitService addMonitorUnitService;

<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> {
    <span class="hljs-keyword">new</span> <span class="hljs-title class_">SpringApplicationBuilder</span>(Application.class).web(<span class="hljs-literal">true</span>).run(args);
}    

}

4、在代码中调用接口即可

@Autowired
private DecisionEngineService decisionEngineService ;
// ...
decisionEngineService.getEngineMesasge("uid" ,  "productCode");

方案三: 采用 RestTemplate 方法

在 Spring-Boot 开发中,RestTemplate 同样提供了对外访问的接口 API,这里主要介绍 Get 和 Post 方法的使用。Get 请求提供了两种方式的接口 getForObject 和 getForEntity,getForEntity 提供如下三种方法的实现。

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

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

// 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) 引用。参考如下:

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

Get 请求之——getForObject

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

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 方法使用如下:

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

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

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 的参数大体相同在此不做介绍。

参考文章