java - Spring : call REST API after receiving response from another REST API

标签 java spring rest api

我有一个 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/

相关文章:

java - 拖放 SWT TableColumn 标题?

java - TreeMap 中的空指针异常

java - 在 netbeans 中运行主项目时未显示 GUI

java - java中两个字符串的字符串长度错误?

java - 使用 Gson 解析 JSON(或 java 的替代方案)

java - 找出哪个端点发送了 REST 调用

java - 如何在 QuartzJobBean 的子类中获取 Session/Request 对象

java - 使用 Junit 测试 Hibernate DAO

java - 如何检查Spring中@RequestParam类型的Integer变量是否为 "empty"?

rest - JIRA REST API 获取所有用户