我是Spring Boot的新手。我尝试访问控制器url,但没有收到任何响应。
我们按照文档遵循Spring Boot项目结构。 我已经使用了componentscan注释,并在SpringBootApplication注释中添加了scanBasePackageClasses。但是仍然相同。
HomeController:-
package com.main.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
@RequestMapping(value="/home",method=RequestMethod.GET)
public String home() {
System.out.println("home");
return "home";
}
}
GAppsApplication.java:-
package com.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import com.main.controllers.HomeController;
@ComponentScan({"com.main.controllers"})
@SpringBootApplication(scanBasePackageClasses={HomeController.class})
public class GAppsApplication {
public static void main(String[] args) {
SpringApplication.run(GAppsApplication.class, args);
}
}
为便于参考,我在以下日志中分享了 日志:-
2018-08-28 08:26:16.404 INFO 8172 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: org.springframework.security.web.util.matcher.AnyRequestMatcher@1, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@58b9d6, org.springframework.security.web.context.SecurityContextPersistenceFilter@525747, org.springframework.security.web.header.HeaderWriterFilter@1638752, org.springframework.security.web.csrf.CsrfFilter@324c61, org.springframework.security.web.authentication.logout.LogoutFilter@14a6743, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@12d6ce4, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@1837d01, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@eda2d2, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@4a5109, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@4bb38c, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@12205be, org.springframework.security.web.session.SessionManagementFilter@2e4343, org.springframework.security.web.access.ExceptionTranslationFilter@1a81a58, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@12e7437]
2018-08-28 08:26:16.558 INFO 8172 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2018-08-28 08:26:16.592 INFO 8172 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-08-28 08:26:16.594 INFO 8172 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure
2018-08-28 08:26:16.600 INFO 8172 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2018-08-28 08:26:16.638 INFO 8172 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-08-28 08:26:16.642 INFO 8172 --- [ restartedMain] c.main.GAppsApplication : Started GAppsApplication in 5.153 seconds (JVM running for 5.734)
2018-08-28 08:27:11.961 INFO 8172 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-08-28 08:27:11.961 INFO 8172 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-08-28 08:27:11.980 INFO 8172 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms
答案 0 :(得分:0)
将您的控制器更改为如下所示:
@RestController
@RequestMapping("/home")
public class HomeController {
@GetMapping
public String home() {
System.out.println("home");
return "home";
}
}
由于您的基本软件包是com.main
,因此com.main
中的任何软件包都会被自动扫描。
您无需放置任何特殊注释即可扫描。