【问题标题】:Calling REST Controller from maven dependency从 Maven 依赖项调用 REST 控制器
【发布时间】:2021-12-17 07:16:31
【问题描述】:

假设我有一个 SpringBoot 2.2.6 WebApp 调用 app-client 和一个 Maven 模块 调用 common-crud 我需要插入一些公共控制器。举个例子,假设 common-crud 只有一个包,其中只有以下控制器:

@GetMapping("/sayHelloFromModule")
public ResponseEntity<String> sayHello() {
  return new ResonseEntity<String>("Hello", HttpStatus.OK);
}

common-crud maven模块的.pom.xml如下:

<project..
  <groupId>com.common</groupId>
  <artifactId>common-crud</artifactId>
  <version>0.0.1</version>
  <packaging>jar</packaging>
  <name>common-crud</name>

  <properties>
   ...
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.6.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <scope>test</scope>
    </dependency>
  </dependencies>
</project>

app-client 是一个 SpringBoot 应用程序,并拥有它的主要功能等等。.pom.xml 是:

<project..
  <groupId>com.mainapp</groupId>
  <artifactId>app-client</artifactId>
  <version>0.0.1</version>
  <packaging>war</packaging>
  <name>app-client</name>

  <properties>
   ...
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.6.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
     <groupId>com.common</groupId>
     <artifactId>common-crud</artifactId>
     <version>0.0.1</version>
    </dependency>
    <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
  </build>
</project>

main-app 部署在带有 contextRoot mainapp 的外部 Tomcat 8.5.53 上。现在,如果我打开浏览器并调用:

http://localhost:8080/app-client/<some-controller-inside-mainapp>

它显然没有问题..如果我打电话:

http://localhost:8080/app-client/sayHelloFromModule

扔给我Error 404。我不知道我是否可以在某些方面意识到这一点,或者这只是一种不好的做法,在任何情况下都不起作用。

显然,实际场景更复杂.. 库模块应该用于分析不同类型的SpringSecurity 身份验证(SAML、JWT、Oauth2.. 等等)。

有没有办法做到这一点? 谢谢

【问题讨论】:

  • 你忘记/喜欢这个“/sayHelloFromModule”
  • 感谢您的回复,但显然是数字错误...
  • 不确定但值得一试,您能否检查一下您是否在属性文件中提供了任何可能导致问题的上下文路径。

标签: spring-boot maven dependencies spring-restcontroller


【解决方案1】:

这比看起来要容易。在mainappMain 类中

@SpringBootApplication
@ComponentScan({
  "com.client.oauth",
  "com.client.resources",
  "com.client.security.config",
  "com.auth0.core.common",
  "com.auth0.core.provider"
})
public class OauthClientApplication extends SpringBootServletInitializer {}

需要导入依赖包

【讨论】:

    猜你喜欢
    • 2020-04-08
    • 2013-08-01
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    相关资源
    最近更新 更多