本文已参与「新人创作礼」活动,一起开启掘金创作之路。 之前调用第三方接口,网上找的HttpClient太过于繁琐,需要手写一大堆代码,或者自己整理工具类。按照不要重复造轮子的思想,找到了Feign来代替完成调用,果然简单又快捷。 但是最近浏览帖子,Feign不是封装HttpClient来调用接口的。
Feign是Netflix开发的声明式、模板化的HTTP客户端, Feign可以帮助我们更快捷、优雅地调用HTTP API。
在Spring Cloud中,使用Feign非常简单——创建一个接口,并在接口上添加一些注解,代码就完成了。Feign支持多种注解,例如Feign自带的注解或者JAX-RS注解等。 Spring Cloud对Feign进行了增强,使Feign支持了Spring MVC注解,并整合了Ribbon和Eureka,从而让Feign的使用更加方便。 Spring Cloud Feign是基于Netflix feign实现,整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供这两者的强大功能外,还提供了一种声明式的Web服务客户端定义的方式。 Spring Cloud Feign帮助我们定义和实现依赖服务接口的定义。在Spring Cloud feign的实现下,只需要创建一个接口并用注解方式配置它,即可完成服务提供方的接口绑定,简化了在使用Spring Cloud Ribbon时自行封装服务调用客户端的开发量。 Spring Cloud Feign具备可插拔的注解支持,支持Feign注解、JAX-RS注解和Spring MVC的注解。 ————————————————
看样子Feign更多的用在服务器之间的调用,用来解决分布式架构的,统一代码格式
那么HttpClient就没得封装吗?有 那就是Forest
Forest代替HttpClient和Okhttp
1.引入坐标
<dependency>
<groupId>com.dtflys.forest</groupId>
<artifactId>spring-boot-starter-forest</artifactId>
<version>1.3.0</version>
</dependency>
2.定义自己的接口类
package com.jydw.restaurant.client;
import com.alibaba.fastjson.JSONObject;
import com.dtflys.forest.annotation.*;
import com.jydw.restaurant.pojo.OrderDetailJsonBean;
import org.springframework.stereotype.Component;
@Component
public interface MyClient {
/**
* 获取订单订单详情信息
* @return
*/
@Post( url = "#####",
headers = {
"MERCHANT-ID: ",
"APP-ID: "
})
OrderDetailJsonBean getOrderDetail(@JSONBody JSONObject json);
/**
* 从cps 根据userId获取人员信息
*/
@Get(url = "#####")
String getUserById(@Header("token") String token, @Query("id") String id);
}
因为公司内部接口,就用###代替
3.在启动类里配置代理接口类的扫描包@ForestScan
package com.jydw.restaurant;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.dtflys.forest.springboot.annotation.ForestScan;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableEurekaClient
@EnableScheduling
@MapperScan("com.jydw.restaurant.dao")
@ForestScan(basePackages = "com.jydw.restaurant.client" )
public class RestaurantApplication {
public static void main(String[] args) {
SpringApplication.run(RestaurantApplication.class, args);
}
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
搞定 这样就可以在任何地方调用需要的第三方接口,当然后面可以吧url和秘钥放在yml或者properties配置文件里
//封装content内容
JSONObject content = new JSONObject(true);
content.put("beginTime", new SimpleDateFormat("yyyy-MM-dd").format(new Date())+" 00:00:00"); //从凌晨0点开始查
content.put("endTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));//当前时间
//请求的json数据进行加密并格式化
JSONObject jsonBody = RequestJsonFormatUtils.getJsonBody(content);
//log.info("发送请求的json数据requestJson:{}", jsonBody);
//发送请求,获取订单数据
OrderDetailJsonBean responseOrderDetail = myClient.getOrderDetail(jsonBody);