SpringBoot + Retrofit 调用第三方远程接口服务

第一步: 添加依赖

        <dependency>
            <groupId>com.squareup.retrofit</groupId>
            <artifactId>retrofit</artifactId>
            <version>1.9.0</version>
        </dependency>
        

这里使用的是1.9.0

第二部:添加配置RestAdapterConfig.java

package com.lxj.thread.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import retrofit.RestAdapter;

@Configuration
public class RestAdapterConfig {
    /**
     * 获取RestAdapter单例Bean
     *
     * @return
     */
    @Bean
    public RestAdapter getRestAdapter() {
        /**
         * setEndpoint("http://127.0.0.1:31111"):指定基本的URL,
         * API接口中的URL是相对于该URL的路径的,
         * 不能少了协议名,例如写成:localhost:8081就不行
         */
        RestAdapter adapter = new RestAdapter.Builder()
                .setEndpoint("http://127.0.0.1:31111")
                .build();
        return adapter;
    }
}

说明:
这里的URL是指 你所要调用的第三方接口的URL 只需要ip和port。

第三步:添加配置RetrofitAPI,指定调用远程服务方法

package com.lxj.thread.apiservice;

import retrofit.http.GET;
import retrofit.http.POST;

import java.util.List;

public interface RetrofitAPI {
    /**
     * GET请求带查询参数
     */
    @GET("/API/getRetrofitAPI")
    String getRetrofitAPI();

    /**
     * POST请求
     */
    @POST("/API/postRetrofitAPI")
    List<String> postRetrofitAPI();
}

该类指定了第三方远程服务的方法的基本路径与参数以及返回值

第四步:添加配置RetrofitAPIConfig,构建RetrofitAPI

package com.lxj.thread.config;

import com.lxj.thread.apiservice.RetrofitAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import retrofit.RestAdapter;

@Configuration
public class RetrofitAPIConfig {
    @Autowired
    private RestAdapter adapter;

    @Bean
    public RetrofitAPI getRetrofitAPI() {
        return adapter.create(RetrofitAPI.class);
    }
}

之后的五六两步就是创建controller层和service层

第五步:创建Controller层

 @Autowired
    private RetrofitService retrofitService;

    @GetMapping("/getRetrofitAPI")
    public String getRetrofitAPI() {
        return retrofitService.getRetrofitAPI();
    }

    @PostMapping("/postRetrofitAPI")
    public List<String> postRetrofitAPI() {
        return retrofitService.postRetrofitAPI();
    }

第六步:创建Service 层

package com.lxj.thread.apiservice.impl;

import com.lxj.thread.apiservice.RetrofitAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RetrofitService {
    @Autowired
    private RetrofitAPI retrofitAPI;

    public String getRetrofitAPI() {
        return String.getRetrofitAPI();
    }

    public List<String> postRetrofitAPI() {
        return String.postRetrofitAPI();//测试post请求
    }

}
在Java Spring Boot项目中调用第三方接口通常涉及以下几个步骤: 1. **添加依赖**:首先,你需要在项目的pom.xml文件中添加对第三方API库的依赖。例如,如果你要调用的是HTTP RESTful API,可以使用`spring-boot-starter-web`或者`com.squareup.retrofit2:retrofit`等库。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 或者使用Retrofit --> <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>retrofit</artifactId> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> </dependency> ``` 2. **配置RestTemplate或Retrofit**:Spring Boot提供了`RestTemplate`用于发送HTTP请求,而Retrofit则是一个强大的客户端库。以下是配置Retrofit的例子: ```java @Configuration public class ApiServiceConfig { @Bean public OkHttpClient httpClient() { return new OkHttpClient(); } @Bean public Retrofit retrofit(OkHttpClient client) { return new Retrofit.Builder() .baseUrl("https://api.example.com/") .addConverterFactory(GsonConverterFactory.create()) .client(client) .build(); } @Bean public YourApiService yourApiService(Retrofit retrofit) { return retrofit.create(YourApiService.class); } } ``` 其中,`YourApiService`是你自定义的接口类,对应第三方API的公共方法。 3. **调用接口**:有了服务实例后,你可以通过注入到需要使用的类中,然后像操作本地方法一样调用它: ```java @Service public class YourService { private final YourApiService apiService; @Autowired public YourService(YourApiService apiService) { this.apiService = apiService; } public void fetchData() { Call<ApiResponse> call = apiService.getSomeData(); call.enqueue(new Callback<ApiResponse>() { // 处理响应... }); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值