Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBoot cannot find handler method

Tags:

spring-boot

I have a basic SpringBoot app. using Spring Initializer, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file.

this is the main class

@SpringBootApplication
public class TdkApplication {
    public static void main(String[] args) {
        SpringApplication.run(TdkApplication.class, args);
    }
}

This is a controller

@Controller
public class MockupIndexController {
    @RequestMapping("/mockup/index")
    public String welcome(Map<String, Object> model) {
        return "mockups/index";
    }
}

This my 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>

But when I put this in the URL:

http://localhost:8080/mockup/index

I got the following log in the console

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
like image 596
Nunyet de Can Calçada Avatar asked May 02 '17 21:05

Nunyet de Can Calçada


2 Answers

I assume your controller class is not found during scanning cause it is in a package that will not be scanned by default. I guess your application is in something like com.tdk.app and your controller in com.tdk.controller, right? If yes just move the app one level up to com.tdk and your issue should go away.

like image 75
daniel.eichten Avatar answered Oct 13 '22 00:10

daniel.eichten


The controller package should be the one level bottom package of "TdkApplication" if your "TdkApplication" is in com.test package, Then your controller should be under com.test.controller package. Means one level down package when we comparing with application package

like image 32
Mr.DevEng Avatar answered Oct 13 '22 01:10

Mr.DevEng