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请求
    }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值