ic商城网站建设,网站备案关闭影响排名,世界做火的游戏视频网站,单页式网站 seo解决使用WebTestClient访问接口报[185c31bb] 500 Server Error for HTTP GET /** 问题发现问题解决 问题发现
WebTestClient 是 Spring WebFlux 框架中提供的用于测试 Web 请求的客户端工具。它可以不用启动服务器#xff0c;模拟发送 HTTP 请求并验证服务器的响… 解决使用WebTestClient访问接口报[185c31bb] 500 Server Error for HTTP GET /** 问题发现问题解决 问题发现
WebTestClient 是 Spring WebFlux 框架中提供的用于测试 Web 请求的客户端工具。它可以不用启动服务器模拟发送 HTTP 请求并验证服务器的响应。
在学习spring-test中依照文档要求进行编写后报错完整实例代码如下
Controller
public class TestController {GetMapping(value /test)public ResponseEntityString method(){return ResponseEntity.ok(Hello, world!);}
}
WebFluxTest
public class MyServiceTest {Testpublic void test() {WebTestClient webTestClient WebTestClient.bindToController(new TestController()).configureClient().build();WebTestClient.BodySpecString, ? response webTestClient.get().uri(/test).exchange().expectStatus().isOk()//判断返回200.expectBody(String.class).isEqualTo(Hello, world!);//断言判断是否返回Hello, world!}
}报错截图
由于没有启动服务看不到接受端到底是那个地方报错因为这一个问题卡了三天之久真的很难受
问题解决
最开始包括请求参数、返回参数、请求地址、请求方式、注解、等方面都检查如果返回404更加要检查这些问题发现都不是这些问题最后尝试是不是依赖的版本导致的结果成功解决。
原pom依赖代码 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-webflux/artifactIdversion2.0.6.RELEASE/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdversion2.4.0/versionscopetest/scope/dependency发现webflux的版本要低些尝试改成一样的代码试试改完以后的依赖代码 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-webflux/artifactIdversion2.4.0/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdversion2.4.0/versionscopetest/scope/dependency然后再次执行结果如图 接口请求成功
网上对WebTestClient遇到的一些错误解决方案比较少欢迎大家评论区交流。