2015-11-02 15 views
0

我使用SpringBoot來構建spring mvc應用程序。 MyApplication和MyController位於不同的模塊中,並且向pom添加了依賴關係,並且在運行它時,下面定義的映射(/ greet)未映射到dispacherServlet,而如果同一個包中的兩個類都映射到端點。springboot沒有映射在另一個模塊中定義的端點

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"; 
     } 
+1

當它們位於不同的模塊中時,它們都具有相同的包名com.test.controllers,或者在同一模塊中測試它時是否獲得該包名,而在另一個模塊中它是不同的。 – Zergleb

回答

1

通過在intelliJ中添加模塊依賴關係到IntelliJ中的「項目結構」並添加所需的依賴關係,一切正常,解決了問題。一個簡單的被忽視的錯誤。

2

嘗試添加成分掃描註釋

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

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