简体   繁体   中英

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.

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.

在此处输入图像描述

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

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.

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.

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

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

Cause - Main reason behind this issue is servlet.xml file is not able to find the proper controller that could redirect your request.

Solution - Ensure few things:

  1. In servlet.xml file your base-package should point to the correct path where the controller class resides.

  2. @Controller should be mentioned over the controller class.

  3. In controller class @RequestMapping value should be as same as the web.xml url-pattern parameter.

  4. In controller class, ModelAndView returns .jsp page should be at the same path which is pointing in the servlet.xml prefix value of ViewResolver.

I Have also faced the same problem org.springframework.web.servlet.DispatcherServlet noHandlerFound

I am hitting different url in postman to solve this problem just check your postman which URL you hitting

我刚刚检查了servlet.xml文件并解决了问题:

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

I have faced similar issue while copying from one project to another. I missed to add the index.jsp

Adding this resolved my issue. Hope it helps.

I faced the same issue and very basic mistake i did was:

<mvc:annotation-driven /> <context:component-scan base-package="com.test" />

wrong package name as it was "com.test" and i had written "com.controller"..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM