[英]Spring Boot Controller not mapping
我使用過 STS,現在我使用的是 IntelliJ Ultimate Edition,但我仍然得到相同的 output。 我的 controller 沒有被映射,因此顯示 404 錯誤。 我對 Spring 框架完全陌生。
DemoApplication.java
package com.webservice.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
HelloController.java
package com.webservice.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String sayHello(){
return "Hey";
}
}
控制台 Output
com.webservice.demo.DemoApplication : Starting DemoApplication on XFT000159365001 with PID 11708 (started by Mayank Khursija in C:\Users\Mayank Khursija\IdeaProjects\demo)
2017-07-19 12:59:46.150 INFO 11708 --- [ main] com.webservice.demo.DemoApplication : No active profile set, falling back to default profiles: default
2017-07-19 12:59:46.218 INFO 11708 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@238e3f: startup date [Wed Jul 19 12:59:46 IST 2017]; root of context hierarchy
2017-07-19 12:59:47.821 INFO 11708 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8211 (http)
2017-07-19 12:59:47.832 INFO 11708 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-07-19 12:59:47.832 INFO 11708 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-07-19 12:59:47.944 INFO 11708 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-07-19 12:59:47.944 INFO 11708 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1728 ms
2017-07-19 12:59:47.987 INFO 11708 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-07-19 12:59:48.510 INFO 11708 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-07-19 12:59:48.519 INFO 11708 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2017-07-19 12:59:48.634 INFO 11708 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8211 (http)
2017-07-19 12:59:48.638 INFO 11708 --- [ main] com.webservice.demo.DemoApplication : Started DemoApplication in 2.869 seconds (JVM running for 3.44)
我也有類似的問題,並且能夠通過在此之后更正源包結構來最終解決它
組件掃描不會掃描您的控制器類。 您的 Controller 類必須在包層次結構中嵌套到具有 main() 方法的主 SpringApplication 類的下方,然后只會掃描它,並且在 Spring Boot 開始時,您還應該看到控制台輸出中列出的 RequestMappings。
在 Spring Boot 1.5.8.RELEASE 上測試
但是,如果您更喜歡使用自己的打包結構,您始終可以使用@ComponentScan
注釋來定義要掃描的basePackages
。
因為DemoApplication.class
和HelloController.class
在同一個包中
在其他類上方的根包中找到您的主應用程序類
查看 Spring Boot 文檔定位主應用程序類
使用根包還允許組件掃描僅應用於您的項目。
例如,在您的情況下,它如下所示:
com.webservice.demo.DemoApplication
com.webservice.demo.controller.HelloController
就我而言,它缺少來自 pom.xml 的依賴項,否則一切都編譯得很好。 Spring 日志中的 404 和缺少的映射信息是唯一的提示。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
我也遇到了類似問題,並使用正確的包結構解決了它,如下所示。 修正后,它可以正常工作。 例如
在@SpringBootApplication 上方的主類中添加@ComponentScan(com.webservice) 將解決您的問題。 參考下面的代碼
package com.webservice.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(com.webservice)
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
當我創建新項目時,依賴檢查Web >> Jersey(JAX-RS),我進入pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
一切都在編譯,日志中沒有映射,與你的情況相同。
所以,我在pox.xml中手動添加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
一切都開始奏效了。
之后我創建了一個新的項目,只使用了依賴Web >>向導中的Web檢查,我從頭開始就得到了我想要的東西。
在我的情況下,我沒有做任何與server.contextPath做任何方法的事情。
在我看來,當我們將組件掃描留給 Spring 時,就會出現這種可見性問題,Spring 具有使用標准約定查找類的特定方式。 在這種情況下,由於 Starter 類(DemoApplication)在 com.webservice.demo 包中,將 Controller 放在下面一層將有助於 Spring 使用默認的組件掃描機制找到類。 將 HelloController 放在 com.webservice.demo.controller 下應該可以解決問題。
就我而言,我使用@Controller
而不是@RestController
和@RequestMapping
這取決於幾個屬性:
server.contextPath
屬性。 如果它設置為任何值,那么您需要將其附加到您的請求網址中。 如果沒有這樣的屬性,那么在 application.properties server.contextPath=/
中添加這一行@RequestMapping
中的method
屬性,似乎沒有任何值,因此,根據文檔,它應該映射到所有方法。 但是,如果你想讓它監聽任何特定的方法,那么你可以將它設置為讓我們說method = HttpMethod.GET
我找到了答案。 這是因為在較新版本的 Spring Framework 中更新了安全配置。 所以我只是將我的版本從 1.5.4 更改為 1.3.2
在我的情況下,我使用錯誤的端口進行測試請求 - Tomcat 啟動時暴露了幾個端口(包括一個用於監視 /actuator)。
就我而言,我更改了配置文件的包。 把它移回原來的 com.example.demo 包,事情就開始工作了。
另一種情況可能是您像我一樣不小心將 Java 類放在了 Kotlin 源目錄中。
錯誤的:
src/main
┕ kotlin ← this is wrong for Java
┕ com
┕ example
┕ web
┕ Controller.class
正確的:
src/main
┕ java ← changed 'kotlin' to 'java'
┕ com
┕ example
┕ web
┕ Controller.class
因為在 Kotlin 源目錄中時,Java 類不會被拾取。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.