我有一个基本的SpringBoot应用程序。使用Spring Initializer,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行的JAR文件。
这是主要的课程
@SpringBootApplication
public class TdkApplication {
public static void main(String[] args) {
SpringApplication.run(TdkApplication.class, args);
}
}
这是一个控制器
@Controller
public class MockupIndexController {
@RequestMapping("/mockup/index")
public String welcome(Map<String, Object> model) {
return "mockups/index";
}
}
我的pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.tdk.iot.core</groupId>
<artifactId>tdk-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
但是当我把它放在网址中时:
http://localhost:8080/mockup/index
我在控制台中获得了以下日志
o.s.web.servlet.DispatcherServlet : Servlet 'dispatcherServlet' configured successfully
o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/mockup/index]
s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /mockup/index
s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/mockup/index]
o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/mockup/index] are [/**]
o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/mockup/index] are {}
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/mockup/index] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@8b91134]]] and 1 interceptor
o.s.web.servlet.DispatcherServlet : Last-Modified value for [/mockup/index] is: -1
o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
o.s.web.servlet.DispatcherServlet : Successfully completed request
o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
答案 0 :(得分:10)
我认为在扫描期间找不到您的控制器类,因为它位于默认情况下不会扫描的包中。我想你的应用程序就像com.tdk.app和com.tdk.controller中的控制器一样,对吧?如果是,只需将应用程序移动到com.tdk一级,您的问题就会消失。
答案 1 :(得分:1)
控制器包应该是&#34; TdkApplication&#34;的一级底层包。如果你的&#34; TdkApplication&#34;在com.test包中,然后你的控制器应该在com.test.controller包下。当我们与应用程序包进行比较时,意味着一个级别的包装
答案 2 :(得分:0)
以下工作
应用包应该像com.app
然后控制器将是com.app.controller。
如果你像下面那样使用它,那么spring将无法来扫描控制器
App包:com.app.main
控制器:com.app.controller