[英]In the context of a common Spring and Angular application, what is the API?
API 上有大量可用信息。 Guru99 上有一個很好的頁面。 不過,可用的信息非常一般。 我不確定它究竟如何應用於我熟悉的工作。 我的工作是我通常使用 Spring 和 Angular 開發 Web 應用程序。
在這種情況下,API 是什么? 它是用戶輸入文本和點擊按鈕的用戶界面嗎? 是 Spring DispatcherServlet 處理來自前端的傳入請求並將它們定向到正確的后端 Spring Controller 方法嗎? 是 Spring Controller 本身嗎? 前端服務類中的 API 是否將請求發送回后端? 這些服務類將包含使用 rxjs Observables 的方法,如下所示。
getCards(): Observable<Card[]> {
return this.http.get<Card[]>(this.baseUrl+'cards')
.pipe(catchError(this.handleError<Card[]>('getCards', [])));
}
雖然后端控制器方法如下所示:
@RestController
@RequestMapping("cards")
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class CardControl {
private final CardService cardService;
@GetMapping
public ResponseEntity<List<Card>> getCards(){
List<Card> cards = cardService.findAll();
return new ResponseEntity<>(cards, HttpStatus.OK);
}
}
如果我要對這樣的應用程序進行 API 測試,我到底要測試什么? 這種測試會在后端、前端還是兩者都進行?
通常,當我們談論 API(在 Web 應用程序的上下文中)時,我們通常談論后端。 所以在你的情況下,這將是你的彈簧控制器:
@RestController
@RequestMapping("cards")
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class CardControl {
private final CardService cardService;
@GetMapping
public ResponseEntity<List<Card>> getCards(){
List<Card> cards = cardService.findAll();
return new ResponseEntity<>(cards, HttpStatus.OK);
}
}
因此,如果您要進行 API 測試,您將向后端發送GET [URL]/cards
請求並確保它返回您期望的內容。
您可以使用多種工具在您的上下文中進行 API 測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.