注冊網(wǎng)站后如何注銷賬號(hào)網(wǎng)絡(luò)seo首頁
解決使用WebTestClient訪問接口報(bào)[185c31bb] 500 Server Error for HTTP GET "/**"
- 問題發(fā)現(xiàn)
- 問題解決
問題發(fā)現(xiàn)
WebTestClient 是 Spring WebFlux 框架中提供的用于測試 Web 請求的客戶端工具。它可以不用啟動(dòng)服務(wù)器,模擬發(fā)送 HTTP 請求并驗(yàn)證服務(wù)器的響應(yīng)。
在學(xué)習(xí)spring-test中,依照文檔要求進(jìn)行編寫后報(bào)錯(cuò),完整實(shí)例代碼如下:
@Controller
public class TestController {@GetMapping(value = "/test")public ResponseEntity<String> method(){return ResponseEntity.ok("Hello, world!");}
}
@WebFluxTest
public class MyServiceTest {@Testpublic 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!}
}
報(bào)錯(cuò)截圖
由于沒有啟動(dòng)服務(wù)看不到接受端到底是那個(gè)地方報(bào)錯(cuò),因?yàn)檫@一個(gè)問題卡了三天之久,真的很難受!!!
問題解決
最開始包括,請求參數(shù)、返回參數(shù)、請求地址、請求方式、注解、等方面都檢查(如果返回404更加要檢查這些問題),發(fā)現(xiàn)都不是這些問題,最后嘗試是不是依賴的版本導(dǎo)致的,結(jié)果成功解決。
原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>
發(fā)現(xiàn)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>
然后再次執(zhí)行,結(jié)果如圖:
接口請求成功!!!
網(wǎng)上對WebTestClient遇到的一些錯(cuò)誤解決方案比較少,歡迎大家評(píng)論區(qū)交流。