解决使用WebTestClient访问接口报[185c31bb] 500 Server Error for HTTP GET “/**“

文章讲述了在使用SpringWebFlux的WebTestClient进行接口测试时遇到500错误的问题,通过检查请求参数、依赖版本等排查,发现是由于WebFlux版本过低导致,升级到相同版本后问题得到解决。作者呼吁读者在遇到类似问题时交流讨论。
摘要由CSDN通过智能技术生成

解决使用WebTestClient访问接口报[185c31bb] 500 Server Error for HTTP GET "/**"

问题发现

WebTestClientSpring WebFlux 框架中提供的用于测试 Web 请求的客户端工具。它可以不用启动服务器,模拟发送 HTTP 请求并验证服务器的响应。

在学习spring-test中,依照文档要求进行编写后报错,完整实例代码如下:

@Controller
public class TestController {
    @GetMapping(value = "/test")
    public ResponseEntity<String> method(){
        return ResponseEntity.ok("Hello, world!");
    }
}
@WebFluxTest
public class MyServiceTest {
    @Test
    public void test() {
        WebTestClient webTestClient = WebTestClient.bindToController(new TestController())
                .configureClient()
                .build();
        WebTestClient.BodySpec<String, ?> response = webTestClient.get().uri("/test")
                .exchange()
                .expectStatus().isOk()//判断返回200
                .expectBody(String.class).isEqualTo("Hello, world!");//断言判断是否返回Hello, world!
    }
}

报错截图
在这里插入图片描述

由于没有启动服务看不到接受端到底是那个地方报错,因为这一个问题卡了三天之久,真的很难受!!!

问题解决

最开始包括,请求参数、返回参数、请求地址、请求方式、注解、等方面都检查(如果返回404更加要检查这些问题),发现都不是这些问题,最后尝试是不是依赖的版本导致的,结果成功解决。

原pom依赖代码:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
            <version>2.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.4.0</version>
            <scope>test</scope>
        </dependency>

发现webflux的版本要低些,尝试改成一样的代码试试,改完以后的依赖代码:

        <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>

然后再次执行,结果如图:
在这里插入图片描述
接口请求成功!!!

网上对WebTestClient遇到的一些错误解决方案比较少,欢迎大家评论区交流。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值