Java 使用feign调用第三方API

一、添加Maven依赖

     	<dependency>
            <groupId>com.netflix.feign</groupId>
            <artifactId>feign-core</artifactId>
            <version>8.18.0</version>
        </dependency>
        <dependency>
            <groupId>com.netflix.feign</groupId>
            <artifactId>feign-jackson</artifactId>
            <version>8.18.0</version>
        </dependency>

二、添加配置文件

sewage:
  url: https://xxxxx.cn
  userId: xxxxxx
  userKey: xxxxx

三、添加对内调用接口

public interface SurfaceWaterApiDao {
    
    
    JSONArray getCycleNewDataByMn(String mn);
}

四、实现层(fegin初始化)

@Service
public class SurfaceWaterApiImpl implements SurfaceWaterApiDao{
    
    

    private static SurfaceWaterApi SURFACE_WATER_API;

    @Value("${surface-water.url}")
    protected String url;

    @Value("${surface-water.userId}")
    private String userId;

    @PostConstruct
    protected void init() {
    
    
        SURFACE_WATER_API = Feign.builder()
                .encoder(new JacksonEncoder()) // 编码方式
                .decoder(new JacksonDecoder())	// 解码方式
                .options(new Request.Options(3000, 3500))
                .retryer(new Retryer.Default(5000, 5000, 3))
                .target(SurfaceWaterApi.class, url);
    }


    @Override
    public JSONArray getCycleNewDataByMn(String mn) {
    
    
        return SURFACE_WATER_API.getCycleNewDataByMn(mn);
    }
}

五、使用Feign调用第三方API(GET、POST)

public interface SurfaceWaterApi {
    
    


    /**
     * 实时数据
     * @param mn
     * @return
     */

    @RequestLine("GET /BusinessService/MN_8051_MN/GetLatest8051?mn={mn}")
    @Headers({
    
    "Content-Type: application/json"})
    JSONArray getCycleNewDataByMn(@Param("mn") String mn);


    /**
     * 根据时段进行查询历史的周期数据
     * @param startTime
     * @param endTime
     * @return
     */

    @RequestLine("POST /BusinessService/MN_8051_MN/GetMN_8051_ExceptData?LHCodeID=&StartTime={startTime}&EndTime={endTime}&bException=true&bFilter=false&bFilterDel=false")
    @Headers({
    
    "Content-Type: application/json","Content-Length:18"})
    JSONArray getHistoryData(@Param("startTime") String startTime, @Param("endTime") String endTime,List<String> deviceList);

}

OK,齐活~

猜你喜欢

转载自blog.csdn.net/weixin_45444807/article/details/131827372