Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.springframework.web.servlet.DispatcherServlet noHandlerFound

I get this error when accessing http://api:8080/users which 404's and I get this error in my console:

Sep 23, 2014 5:57:52 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping found for HTTP request with URI [/users] in DispatcherServlet with name 'dispatcher'

When I access http://localhost:8080/api/users I get a 404 in my browser but no error in my console

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.api"/>
</beans>

dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
    ">

  <context:component-scan base-package="com.api"/>      
</beans>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

Users.java:

package com.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/users")
public class Users {
     @Autowired
     public Users() {

     }

    @RequestMapping(method = RequestMethod.GET)
    public void getUser() {
        System.out.println("\nhere\n");
    }
}

If anyone has any clue I'd greatly appreciate it. I've looked up several questions with the same error as I'm getting but common solution was to add which I have already, so I'm not sure what else to do. Thanks.

EDIT: I've just updated all the code to what the latest reference doc, and I still get the same error. EDIT: Update to show my current state of files.

like image 264
wonza Avatar asked Sep 23 '14 22:09

wonza


6 Answers

After spending whole day i got to know the solution. When you create maven project, by default it doesn't create 'java' folder under 'javaResource/src/main', which is required to get the controller path.

Make sure you create java folder first inside 'javaResource/src/main', then create your package and controller.

Screenshot attached for reference.

enter image description here

like image 55
Jyot Avatar answered Nov 16 '22 10:11

Jyot


change your web.xml to

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

and dispatch-servlet.xml to

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

  <context:component-scan base-package="com.api"/>
</beans>

You don't need applicationContext.xml for your code to run

like image 20
Dino Tw Avatar answered Nov 16 '22 09:11

Dino Tw


A common mistake is this Make sure the group id in pom.xml is the parent of that mentioned in

    </context:component-scan>  

For Example If Group Id-com.cavesofprogramming.spring.web then

    <context:component-scan base- package="com.cavesofprogramming.spring.web.controllers">

The effect is that the controller is not found in the maven build and as a result the mapping not done to the correct jsp.

like image 33
Nandan Avatar answered Nov 16 '22 08:11

Nandan


Need to add the following lines in your spring-context.xml

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

Note: property prefix and suffix need to modify based on your application like  .jsp if you are using jsp files.
like image 42
Sam Avatar answered Nov 16 '22 09:11

Sam


Here is how I got around this and what I was doing wrong.

On creating a maven project with web archetype we do not have the java directory under main. You will have to create it manually and add a directory under it. This directory is the base-package that the servlet.xml wants.

servlet.xml and the location of the base-package image attached for reference

View image for viewing the project

like image 1
mainaak Avatar answered Nov 16 '22 10:11

mainaak


in maven ,'java' folder was not created . I created as src/main/java and this issue was resolved .

it is suggested by jyot also in this post . org.springframework.web.servlet.DispatcherServlet noHandlerFound

like image 1
user3678536 Avatar answered Nov 16 '22 09:11

user3678536