springrestspring-resteclipse-jee

No mapping found for GET /api/keyservice/key


I'm creating a spring-rest-app.

This is my dispatcher config (I also have a root config that has DataSource bean)

@Configuration
@ComponentScan(basePackages= {"config", "cache", "dao", "entity", "exception", "rest", "service"})
@EnableWebMvc
public class DispatcherConfiguration {

    @Bean
    public KeyCache keyCache() {
        return new KeyCacheImpl();
    }
}

This is my webapp initializer

public class TinyURLKeyServiceInitializor implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        AnnotationConfigWebApplicationContext appcontext = new AnnotationConfigWebApplicationContext();
        appcontext.register(ConfigurationClass.class);

        servletContext.addListener(new ContextLoaderListener(appcontext));

        AnnotationConfigWebApplicationContext dispatchercontext = new AnnotationConfigWebApplicationContext();
        dispatchercontext.register(DispatcherConfiguration.class);

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatchercontext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

}

This is the controller

@Controller
@RequestMapping("/api/keyservice")
public class KeyServiceController {

    @Autowired
    private KeyService keyService;

    @GetMapping(value="/key", produces="application/json")
    @ResponseBody
    public String getKey() {
        return keyService.getKey();
    }
}

When I startup the Web app and Send - GET http://localhost:7080/api/keyservice/key I get the following in DEBUG LOG

[INFO] Completed initialization in 1006 ms
May 17, 2020 11:56:10 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-7080"]
[DEBUG] GET "/api/keyservice/key", parameters={}
[WARNING] No mapping for GET /api/keyservice/key
[DEBUG] Completed 404 NOT_FOUND

I've put @EnableMvc for registring MappingHandlers. But still they are not able to detect the mapping between endpoint and controller method.

I put a debug point in DispatcherServlet.getHandler and it returns null everytime. Has anyone ever faced similar problem?


Solution

  • Found the reason I was hitting -

    http://localhost:7080/api/keyservice/key

    My pom.xml had -

    <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>7080</port>
                    <path>/api/keyservice</path>
                </configuration>
            </plugin>
    

    My Controller

    @Controller
    @RequestMapping("/api/keyservice")
    public class KeyServiceController {
    
        @Autowired
        private KeyService keyService;
    
        @GetMapping(value="/key", produces="application/json")
        @ResponseBody
        public String getKey() {
    

    Since context path = /api/keyservice (due to setting in pom.xml), spring was trying to find a mapping for /key. Cleary there is no mapping for /key in my controller.

    Removed the controller RequestMapping. And it worked.