springboot不映射另一个模块中定义的端点

时间:2015-11-02 17:42:38

标签: java spring-mvc intellij-idea spring-boot

我正在使用SpringBoot构建一个spring mvc应用程序。 MyApplication和MyController在不同的模块中,依赖项添加到pom,当我运行它时,下面定义的映射(/ greet)没有映射到dispacherServlet,而如果同一个包中的两个类,则端点被映射

MyApplication.java

package com.test;

@SpringBootApplication
public class MyApplication {

public static void main(String[] args) {
    SpringApplication.run(MyApplication .class, args);
}}

的pom.xml

<groupId>com.test</groupId>
    <artifactId>com.test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

 <modules>
 <module>controller</module>
</modules>

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <!-- The main class to start by executing java -jar -->
    <start-class>com.test.MyApplication</start-class>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

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

MyController.java

package com.test.controllers;

@RestController         
public class MyController {
    @RequestMapping("/greet")
        public String greeting()
        {
            return "HELLO THERE";
        }

2 个答案:

答案 0 :(得分:2)

尝试添加组件扫描注释

@ComponentScan(basePackages={"com.test.controllers"})
@SpringBootApplication
public class MyApplication {

public static void main(String[] args) {
    SpringApplication.run(MyApplication .class, args);
}}

答案 1 :(得分:1)

通过进入&#34;项目结构&#34;将模块依赖性添加到所需项目中,解决了该问题。在intelliJ中并添加所需的依赖项,一切正常。一个简单的被忽视的错误。