我有一个 Controller ,它调用Web服务来启动批处理作业,当返回结果时,它应该根据此结果调用另一个REST API。然后它应该等待新的结果,并将第二个结果返回给用户:
@RestController
public class LaunchController {
@PostMapping(path = "/launch", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<LaunchResult> launch(@Valid @RequestBody LaunchParams params) {
// in launch() I call the first REST API
LaunchResult result = myService.launch(params);
// here I need to call another REST API
AnotherResult result2 = callAnotherWebAPIBasedOnThisResult(result);
return ResponseEntity.ok(result2);
}
现在我想知道像这样(同步)并在一个 Controller 中完成所有操作是否是一种好的做法?还存在其他方法吗?
最佳答案
您的 Controller 非常好,因为它内部没有任何应用程序逻辑,并且它实际上调用服务方法。但它缺乏异常处理。您可以使用 try catch block 或 throws 关键字捕获适当的异常。
关于java - Spring : call REST API after receiving response from another REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60946925/