2015-10-21 133 views
2

我有以下代碼春REST API控制器不叫

控制器

@RestController 
@RequestMapping("/") 
public class RequestHandler { 
    @RequestMapping(value = "/demo", method = RequestMethod.POST) 
    @ResponseBody 
    public Object showDemo() { 

     return "Post method"; 
    } 


    @RequestMapping(value = { "/getdemo" }, method = RequestMethod.GET) 
     @ResponseBody 
    public Object showgetDemo() { 
    System.out.println("hello"); 
     return "get Method"; 
    } 
} 

我POM XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>Website2015</groupId> 
    <artifactId>Website2015</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.2.1.RELEASE</version> 
     <relativePath /> 
    </parent> 
    <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> 
    <sourceDirectory>src</sourceDirectory> 
    <plugins> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.3</version> 
     <configuration> 
      <source>1.8</source> 
      <target>1.8</target> 
     </configuration> 
     </plugin> 
     <plugin> 
     <artifactId>maven-war-plugin</artifactId> 
     <version>2.6</version> 
     <configuration> 
      <warSourceDirectory>WebContent</warSourceDirectory> 
      <failOnMissingWebXml>false</failOnMissingWebXml> 
     </configuration> 
     </plugin> 
    </plugins> 

    </build> 
</project> 

並啓動春季啓動應用程序:

@Configuration 
@EnableWebMvc 
@SpringBootApplication 
public class AppLauncher { 

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

    } 

    private static Class<AppLauncher> applicationClass = AppLauncher.class; 

} 

服務器日誌

2015-10-21 16:07:59.314 INFO 12892 --- [   main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 
2015-10-21 16:07:59.470 INFO 12892 --- [   main] o.apache.catalina.core.StandardService : Starting service Tomcat 
2015-10-21 16:07:59.471 INFO 12892 --- [   main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.15 
2015-10-21 16:07:59.561 INFO 12892 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]  : Initializing Spring embedded WebApplicationContext 
2015-10-21 16:07:59.561 INFO 12892 --- [ost-startStop-1] o.s.web.context.ContextLoader   : Root WebApplicationContext: initialization completed in 1158 ms 
2015-10-21 16:07:59.716 INFO 12892 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/] 
2015-10-21 16:07:59.719 INFO 12892 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 
2015-10-21 16:07:59.921 INFO 12892 --- [   main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 
2015-10-21 16:07:59.921 INFO 12892 --- [   main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest) 
2015-10-21 16:08:00.119 INFO 12892 --- [   main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]25af5db5: startup date [Wed Oct 21 16:07:58 CEST 2015]; root of context hierarchy 
2015-10-21 16:08:00.275 INFO 12892 --- [   main] o.s.j.e.a.AnnotationMBeanExporter  : Registering beans for JMX exposure on startup 
2015-10-21 16:08:00.322 INFO 12892 --- [   main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 
2015-10-21 16:08:00.323 INFO 12892 --- [   main] c.gatcbiotech.website.misc.AppLauncher : Started AppLauncher in 2.16 seconds (JVM running for 2.496) 

我使用郵差REST客戶端以上的通過率我得到是

{ 「時間戳」:1445436550585, 「狀態」:404, 「錯誤」: 「未找到」, 「消息」:「沒有可用信息」, 「路徑」:「/獲取」}

我無法找到服務器日誌映射爲好。 花費大量時間後,我最好的猜測是錯誤的項目結構。

我創建了新的動態web項目,並將其轉換爲Maven,當然,我嘗試了它與簡單的Java項目,然後將其轉換爲maven。

請給我一些指示,如果需要更多的信息,請讓我知道謝謝。

+0

@assylias是的,我嘗試使用的瀏覽器,以及訪問它,我打電話正確的網址。如果你會看到日誌,那麼沒有映射到這個控制器。這就是問題。 – Roxy

+0

你的項目結構是什麼樣的? – dunni

+0

@dunni。我不知道如何在這裏上傳/發佈圖片,我會盡力解釋它。項目 - > Java資源,構建,src,目標,WebContent和pom.xml。 Java資源有我的軟件包/代碼,如果細節不夠,那麼我很抱歉難以解釋。 – Roxy

回答

4

默認@SpringBootApplication僅對應用程序類和所有子包的包進行組件掃描。由於AppLauncher包是com.demo.misc,而RequestHandler包是com.demo.controller,所以組件掃描將不會檢測到該bean類。

解決方案:
如果您在使用Spring引導1.2.x的:

以下任一註釋添加到您的TestApp類:

@ComponentScan({"com.demo.controller", "com.demo.misc"}) 

或移動RequestHandler對COM的子包.demo.misc,例如com.demo.misc.controller

如果您已經使用了Spring啓動的1.3.x:

以下參數添加到您的@SpringBootApplication註釋:

@SpringBootApplication(scanBasePackages = {"com.demo.controller", "com.demo.misc"})  
+0

它的工作感謝解釋以及。 – Roxy