【问题标题】:how can I consume rest api of one module from other module in same application using spring boot application without module dependency如何在没有模块依赖的情况下使用 Spring Boot 应用程序在同一应用程序中使用来自其他模块的一个模块的其余 api
【发布时间】:2021-07-04 05:07:15
【问题描述】:

我正在从事多模块项目。目前我面临以下问题:

  • 模块 A

  • 模块 B

  • 模块 A 依赖于 B

  • 模块 A 有以下方法 @GetMapping(path = "/getworkspaces")

      public Map<String, Object> getWorkSpaces()) {
      Map<String, Object> workspaces = null;
      workspaces = this.dataSetService.getWorkSpaces();
      return workspaces;
    

    }

  • 模块 B 有以下方法,我需要使用模块 A 提到的端点

    公共字符串consumeModuelAMethod() {

      RestTemplate restTemplate = new RestTemplate();
      String url = "http://localhost:8080/getworkspaces"
    
      try {
          ResponseEntity<String> responseMap = restTemplate.getForEntity(url, String.class);
          if (HttpStatus.OK.equals(responseMap.getStatusCode())){
              return responseMap.getBody();
          }else {
              return null;
          }
      } catch (RestClientException ex){ex.printStackTrace();}}
    
  • 当我通过模块 A 中的 RestTemplate 调用“http://localhost:8080/getworkspaces”时。

  • 它给我 404(未找到)

  • 我使用安全 api 机制意味着 SSO。那么如何在没有任何依赖的情况下,在同一个项目的另一个模块中调用一个模块的api。

  • 任何帮助将不胜感激

【问题讨论】:

  • 您可以使用 CURL 或 Postman 访问此端点吗?如果是,您可以尝试重新验证 RestTemplate 的配置或启用日志记录以确保请求正确。题外话:模块(甚至服务)之间的循环依赖关系不是好的做法。尽量避免这种情况。例如,以其他方式将代码拆分为模块。
  • @ZhenyaM:我通过邮递员访问它,但我也提供了我从 SSO 获得的 Bearer 令牌 ID。
  • 如果我使用 sso 作为访问令牌,那么如何在服务器端获取 csrf 令牌以在标头授权中与端点一起发送
  • 我已经在标头授权中设置了访问令牌及其对我的工作。

标签: java spring-boot


【解决方案1】:

我已经在标头授权中设置了访问令牌及其对我的工作。 – FaizUR 拉赫曼

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-20
    • 2015-11-11
    • 2020-06-13
    • 2021-11-19
    • 2017-06-10
    • 2017-04-26
    • 1970-01-01
    • 2021-01-01
    相关资源
    最近更新 更多