Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用SpringBoot应用程序从同一应用程序中的另一个模块使用一个模块的RESTAPI,而不依赖于模块_Java_Spring Boot - Fatal编程技术网

Java 如何使用SpringBoot应用程序从同一应用程序中的另一个模块使用一个模块的RESTAPI,而不依赖于模块

Java 如何使用SpringBoot应用程序从同一应用程序中的另一个模块使用一个模块的RESTAPI,而不依赖于模块,java,spring-boot,Java,Spring Boot,我在多模块项目中工作。目前我面临以下问题: 模块A 模块B 模块A依赖于模块B 模块A具有以下方法 @GetMapping(路径=“/GetWorkspace”) publicmap getworkspace()){ 映射工作空间=空; 工作空间=this.dataSetService.getworkspace(); 返回工作区; } 模块B有以下方法,其中我需要使用前面提到的端点模块A 公共字符串ConsumerModueLaMethod() { RestTemplate Rest

我在多模块项目中工作。目前我面临以下问题:

  • 模块A

  • 模块B

  • 模块A依赖于模块B

  • 模块A具有以下方法 @GetMapping(路径=“/GetWorkspace”)

    publicmap getworkspace()){
    映射工作空间=空;
    工作空间=this.dataSetService.getworkspace();
    返回工作区;
    
    }

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

    公共字符串ConsumerModueLaMethod() {

    RestTemplate RestTemplate=new RestTemplate();
    字符串url=”http://localhost:8080/getworkspaces"
    试一试{
    ResponseEntity responseMap=restTemplate.getForEntity(url,String.class);
    if(HttpStatus.OK.equals(responseMap.getStatusCode())){
    返回responseMap.getBody();
    }否则{
    返回null;
    }
    }catch(RestClientException ex){ex.printStackTrace();}
    
  • 当我打电话时“http://localhost:8080/getworkspaces“通过模块A中的RestTemplate

  • 它给我404(未找到)

  • 我使用的安全api机制意味着SSO。那么,如何在同一项目的另一个模块中调用一个模块的api,而不存在任何依赖关系呢

  • 任何帮助都将不胜感激


我已在标头授权中设置了访问令牌及其工作方式FaizUR Rahman

您可以使用CURL或Postman访问此端点吗?如果是,您可以尝试重新验证RestTemplate的配置或启用日志记录以确保请求正确。离题:模块(甚至服务)之间的循环依赖关系不是好的实践。尽量避免这种情况。例如以其他方式将您的代码拆分为模块。@ZhenyaM:我通过邮递员访问此代码,但我也提供了从SSO获得的承载令牌id。如果我使用SSO作为访问令牌,那么如何在服务器端获取csrf令牌以在标头授权中使用端点发送我已经在标头授权中设置了访问令牌及其工作。
  public Map<String, Object> getWorkSpaces()) {
  Map<String, Object> workspaces = null;
  workspaces = this.dataSetService.getWorkSpaces();
  return workspaces;
  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();}}