【问题标题】:org.springframework.web.servlet.DispatcherServlet noHandlerFoundorg.springframework.web.servlet.DispatcherServlet noHandlerFound
【发布时间】:2014-09-23 22:09:36
【问题描述】:

我在访问 http://api:8080/users 时收到此错误,其中 404 和我在控制台中收到此错误:

2014 年 9 月 23 日下午 5:57:52 org.springframework.web.servlet.DispatcherServlet noHandlerFound 警告:在名称为“dispatcher”的 DispatcherServlet 中找不到带有 URI [/users] 的 HTTP 请求的映射

当我访问 http://localhost:8080/api/users 时,我在浏览器中收到 404,但在我的控制台中没有错误

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>

调度程序-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");
    }
}

如果有人有任何线索,我将不胜感激。我已经查找了几个与我得到的错误相同的问题,但常见的解决方案是添加我已经拥有的,所以我不确定还能做什么。谢谢。

编辑:我刚刚将所有代码更新为最新的参考文档,但我仍然遇到同样的错误。 编辑:更新以显示我当前的文件状态。

【问题讨论】:

  • 阅读 Spring 参考文档,您将旧配置与有关 Spring MVC 的新配置混合在一起。你的代码错了。
  • 你能说得具体一点吗?
  • 我已经用参考文档中的最新代码更新了我的问题,但仍然遇到同样的错误。
  • 现在您的配置是正确的。 URL http://api:8080/ 没有意义,要测试的 URL 必须是 http://localhost:8080/CONTEXTROOT/users,其中CONTEXTROOT 在您选择的项目Right Click -&gt; Properties -&gt; Web Project Settings 中可用。如果您的项目已经部署在 Tomcat 中,那么 URL 必须是 http://localhost:8080/projectname/users
  • 我已经完成了,但我仍然得到 404,但我的控制台没有错误。

标签: java spring tomcat


【解决方案1】:

花了一整天后,我知道了解决方案。 创建 maven 项目时,默认情况下它不会在“javaResource/src/main”下创建“java”文件夹,这是获取控制器路径所必需的。

确保首先在 'javaResource/src/main' 中创建 java 文件夹,然后创建包和控制器。

附上截图供参考。

【讨论】:

  • 谢谢,我遇到了同样的问题,现在你帮我解决了。
  • 谢谢。我创建了这个类,它最终在资源文件夹中。
【解决方案2】:

将您的 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>

和 dispatch-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">

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

您的代码不需要 applicationContext.xml 来运行

【讨论】:

【解决方案3】:

一个常见的错误是 确保 pom.xml 中的组 id 是

中提到的组的父组
    </context:component-scan>  

例如 如果组Id-com.cavesofprogramming.spring.web 那么

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

效果是在maven构建中找不到控制器并且 结果映射到正确的jsp。

【讨论】:

  • 请正确格式化您的代码,以便 OP 确定什么是 cmets 和什么是代码。
【解决方案4】:

需要在你的 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.

【讨论】:

    【解决方案5】:

    这是我如何解决这个问题以及我做错了什么。

    在使用 web 原型创建 maven 项目时,我们在 main 下没有 java 目录。您必须手动创建它并在其下添加一个目录。该目录是 servlet.xml 需要的基本包。

    servlet.xml 和基础包图像的位置附上以供参考

    View image for viewing the project

    【讨论】:

      【解决方案6】:

      在 maven 中,'java' 文件夹没有被创建。我创建为 src/main/java 并解决了这个问题。

      jyot 在这篇文章中也提出了建议。 org.springframework.web.servlet.DispatcherServlet noHandlerFound

      【讨论】:

        【解决方案7】:

        原因 - 此问题背后的主要原因是 servlet.xml 文件无法找到可以重定向您的请求的正确控制器。

        解决方案 - 确保几件事:

        1. 在 servlet.xml 文件中,您的基础包应指向控制器类所在的正确路径。

        2. 应该在控制器类中提及@Controller。

        3. 在控制器类中@RequestMapping 的值应该和web.xml 的url-pattern 参数一样。

        4. 在控制器类中,ModelAndView 返回的 .jsp 页面应该与指向 ViewResolver 的 servlet.xml 前缀值的路径相同。

        【讨论】:

          【解决方案8】:

          我也遇到了同样的问题 org.springframework.web.servlet.DispatcherServlet noHandlerFound

          我在邮递员中点击不同的网址来解决这个问题只需检查您的邮递员您点击的是哪个网址

          【讨论】:

            【解决方案9】:

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

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

            【讨论】:

              【解决方案10】:

              我在从一个项目复制到另一个项目时遇到了类似的问题。我错过了添加 index.jsp

              添加这个解决了我的问题。希望对您有所帮助。

              【讨论】:

                猜你喜欢
                • 2014-01-31
                • 2018-02-18
                • 1970-01-01
                • 2013-10-09
                • 1970-01-01
                • 2019-10-23
                • 1970-01-01
                • 2015-07-29
                • 2021-04-01
                相关资源
                最近更新 更多