Search code examples
javaspringspring-bootspring-restcontroller

Rest Controller method not getting called in spring boot


I am implementing rest webservice via a spring boot application.

POM

<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>com.myCompany</groupId>
    <artifactId>services</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>REST Services</name>
    <description>REST Services</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>   
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.properties

server.port=8082
server.contexPath=/services
logging.level.org.springframework.web = DEBUG
logging.level.com.myCompany= INFO
logging.file = ../logs/services.log

Application launcher class

@SpringBootApplication
public class Application {

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

Controller

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AccountRestController {

    private static final Logger logger = LoggerFactory.getLogger(AccountRestController.class);

    @RequestMapping(value="/account", method=RequestMethod.GET)
    public void createAccount(){
        logger.info("ACCOUNT METHOD CALLED");
    }
} 

I see Did not find handler method for [/services/account] message in the console when I fire http://localhost:8082/services/account URL in the browser as shown below

2018-02-06 08:30:42.868 DEBUG 7532 --- [http-nio-8082-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /services/account
2018-02-06 08:30:42.868 DEBUG 7532 --- [http-nio-8082-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/services/account]
2018-02-06 08:30:42.869 DEBUG 7532 --- [http-nio-8082-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/services/account] are [/**]
2018-02-06 08:30:42.869 DEBUG 7532 --- [http-nio-8082-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/services/account] are {}

I dont see message - ACCOUNT METHOD CALLED in the console meaning controller method is not getting invoked.Can you please let me know why createAccount() method not getting called ?
Is controller class getting identified/scanned by Spring factory?
What is causing this error?


Solution

  • Please change server.contexPath to server.contextPath.

    And try again, it should work as there's no extra config or code is there.

    http://www.baeldung.com/spring-boot-application-configuration