SpringBoot 集成retrofit httpclient

导入

复制代码
<dependency>
    <groupId>com.github.lianjiatech</groupId>
    <artifactId>retrofit-spring-boot-starter</artifactId>
    <version>3.2.0</version>
</dependency>

官网 https://github.com/square/retrofit这个比官网更适合于springboot,零配置

写个接口

复制代码
package com.example.demo;

import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClient;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

@RetrofitClient(baseUrl = "http://localhost:8080/")
public interface Bd {

    @GET("add/{id}")
    public Call<String> search(@Path("id") Integer id);
}

写个测试方法

复制代码
 @Autowired
    private Bd bd;

    @Test
    @SneakyThrows
    void test2() {
        String body = bd.search(1).execute().body();
        System.out.println(body);

    }


com.github.lianjiatech.retrofit.spring.boot.exception.RetrofitIOException: Failed to connect to localhost/[0:0:0:0:0:0:0:1]:8080, request=Request{method=GET, url=http://localhost:8080/add/1, tags={class retrofit2.Invocation=com.example.demo.Bd.search() [1]}}

	at com.github.lianjiatech.retrofit.spring.boot.exception.RetrofitException.errorExecuting(RetrofitException.java:42)
	at com.github.lianjiatech.retrofit.spring.boot.core.ErrorDecoder.ioExceptionDecode(ErrorDecoder.java:48)
	at com.github.lianjiatech.retrofit.spring.boot.interceptor.ErrorDecoderInterceptor.intercept(ErrorDecoderInterceptor.java:53)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
	at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154)
	at retrofit2.OkHttpCall.execute(OkHttpCall.java:207)
	at com.example.demo.Demo1ApplicationTests.test2(Demo1ApplicationTests.java:68)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Caused by: java.net.ConnectException: Failed to connect to localhost/[0:0:0:0:0:0:0:1]:8080
	at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:297)
	at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:207)
	at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226)
	at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
	at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
	at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
	at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.kt:221)
	at com.github.lianjiatech.retrofit.spring.boot.log.LoggingInterceptor.intercept(LoggingInterceptor.java:59)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at com.github.lianjiatech.retrofit.spring.boot.retry.RetryInterceptor.intercept(RetryInterceptor.java:38)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at com.github.lianjiatech.retrofit.spring.boot.interceptor.ErrorDecoderInterceptor.intercept(ErrorDecoderInterceptor.java:39)
	... 8 more
	Suppressed: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:8080
		... 28 more
	Caused by: java.net.ConnectException: Connection refused: no further information
		at java.base/sun.nio.ch.Net.pollConnect(Native Method)
		at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:682)
		at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542)
		at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:592)
		at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
		at java.base/java.net.Socket.connect(Socket.java:751)
		at okhttp3.internal.platform.Platform.connectSocket(Platform.kt:128)
		at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:295)
		... 27 more
Caused by: java.net.ConnectException: Connection refused: no further information
	at java.base/sun.nio.ch.Net.pollConnect(Native Method)
	at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:682)
	at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542)
	at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:592)
	at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
	at java.base/java.net.Socket.connect(Socket.java:751)
	at okhttp3.internal.platform.Platform.connectSocket(Platform.kt:128)
	at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:295)
	... 27 more


Process finished with exit code -1

查看日志,成功!

相关推荐
吾日三省Java2 小时前
SpringBoot整合Canal:实现MySQL数据实时同步的终极解决方案
spring boot·系统架构
talenteddriver2 小时前
web: jwt令牌构成、创建的基本流程及原理
java·开发语言·python·网络协议·web
码农水水2 小时前
宇树科技Java被问:数据库连接池的工作原理
java·数据库·后端·oracle
Seven972 小时前
回溯算法总结
java
小鸡脚来咯2 小时前
软链接的作用和用途
java·ide·eclipse
这周也會开心2 小时前
双栈实现队列以及双队列实现栈
java·开发语言
廋到被风吹走2 小时前
【Spring】Spring Batch 详细介绍
java·spring·batch
北极糊的狐2 小时前
MQTT报错:Exception in thread main java.lang.at io.github.pnoker.common.sdk.utils.ParseUtils.decodeHex
java·开发语言
weixin199701080162 小时前
TikTokitem_search_video关键词视频列表接口对接全攻略:从入门到精通
java·服务器·音视频