首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >okhhtp3 mockserver:连接被拒绝

okhhtp3 mockserver:连接被拒绝
EN

Stack Overflow用户
提问于 2021-06-10 14:51:18
回答 2查看 1.2K关注 0票数 1

由于我已经将spring-boot-dependencies版本从2.2.5.RELEASE升级到了2.4.0,所有使用MockWebServer的测试都失败了,出现了以下错误:

org.springframework.web.reactive.function.client.WebClientRequestException: Connection refused: localhost/0:0:0:0:0:0:0:1:56338; nested exception is io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:56338

有线索吗?错误的退步使用/废弃的东西?

消肿

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.4.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <scope>test</scope>
    <version>5.7.0</version>
</dependency>
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>mockwebserver</artifactId>
    <scope>test</scope>
    <version>3.14.9</version>
</dependency>
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <scope>test</scope>
    <version>3.14.9</version>
</dependency>

我写了一个简单的测试用例,但我总是遇到同样的问题.(多亏了https://stackoverflow.com/a/62528728/1638483)

代码语言:javascript
复制
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.web.reactive.function.client.WebClient;

public class QuoteClient {

    private final WebClient webClient;

    public QuoteClient(WebClient.Builder builder, String baseUrl) {
        this.webClient = builder.baseUrl(baseUrl).build();
    }

    public JsonNode getData() {
        return this.webClient
                .get()
                .retrieve()
                .bodyToMono(JsonNode.class)
                .block();
    }
}



import com.fasterxml.jackson.databind.JsonNode;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.web.reactive.function.client.WebClient;

import static org.junit.Assert.assertNotNull;

public class QuotesClientTest {
    private QuoteClient quotesClient;
    private MockWebServer server;

    @BeforeEach
    public void setup() {
        this.server = new MockWebServer();
        this.quotesClient = new QuoteClient(WebClient.builder(), server.url("/").toString());
    }

    @Test
    public void test() {
        server.enqueue(new MockResponse()
                .setStatus("HTTP/1.1 200")
                .setBody("{\"bar\":\"barbar\",\"foo\":\"foofoo\"}")
                .addHeader("Content-Type", "application/json"));

        JsonNode data = quotesClient.getData();
        assertNotNull(data);

        System.out.println(data);

    }
}

StackTrace

代码语言:javascript
复制
org.springframework.web.reactive.function.client.WebClientRequestException: Connection refused: localhost/0:0:0:0:0:0:0:1:56382; nested exception is io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:56382

       at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:137)
       Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
   |_ checkpoint ⇢ Request to GET null [DefaultWebClient]
Stack trace:
       at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:137)
       at reactor.core.publisher.MonoErrorSupplied.subscribe(MonoErrorSupplied.java:70)
       at reactor.core.publisher.Mono.subscribe(Mono.java:3987)
       at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:103)
       at reactor.core.publisher.FluxPeek$PeekSubscriber.onError(FluxPeek.java:221)
       at reactor.core.publisher.FluxPeek$PeekSubscriber.onError(FluxPeek.java:221)
       at reactor.core.publisher.FluxPeek$PeekSubscriber.onError(FluxPeek.java:221)
//...
       at reactor.netty.transport.TransportConnector$MonoChannelPromise.tryFailure(TransportConnector.java:464)
       at reactor.netty.transport.TransportConnector$MonoChannelPromise$1.tryFailure(TransportConnector.java:515)
       at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.fulfillConnectPromise(AbstractNioChannel.java:321)
       at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:337)
       at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707)
       at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
       at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
       at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
       at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
       at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
       at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
       at java.lang.Thread.run(Thread.java:748)
   Suppressed: java.lang.Exception: #block terminated with an error
       at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:99)
       at reactor.core.publisher.Mono.block(Mono.java:1679)
       at QuoteClient.getData(QuoteClient.java:23)
       at QuotesClientTest.test(QuotesClientTest.java:30)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:498)
       at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
       at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
       at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
//...
       at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
       at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
       at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
       at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
       at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
       at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
       at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
   Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:56382
   Caused by: java.net.ConnectException: Connection refused
       at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
       at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
       at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330)
       at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
       at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707)
       at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
       at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
       at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
       at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
       at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
       at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
       at java.lang.Thread.run(Thread.java:748)
EN

回答 2

Stack Overflow用户

发布于 2021-06-11 08:06:50

2.4.5版本(我没有测试中间版本)以来,这个问题似乎已经通过使用spring启动依赖项解决了。

更新:

注意你的本地主机文件..。

我通过添加.local作为localhost & <myHostname>的后缀来更新我的问题,因为这个问题今天早上再次出现(没有任何更改-_-)

代码语言:javascript
复制
127.0.0.1   localhost.local,<myHostname>.local
255.255.255.255 broadcasthost
::1             localhost.local,<myHostname>.local
票数 0
EN

Stack Overflow用户

发布于 2022-10-25 23:26:11

在我的例子中,我在我的主机文件中注释了这一行。

代码语言:javascript
复制
127.0.0.1 localhost

取消评论解决了我的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67923626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档