Java org.springframework.web.servlet.DispatcherServlet noHandlerFound

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26005483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:40:51  来源:igfitidea点击:

org.springframework.web.servlet.DispatcherServlet noHandlerFound

javaspringtomcat

提问by wonza

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

访问http://api:8080/userswhich 404时出现此错误,并且在控制台中出现此错误:

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'

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

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

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

applicationContext.xml

应用上下文.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:

调度程序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:

<?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:

用户.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.

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

采纳答案by Dino Tw

change your web.xml to

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

and dispatch-servlet.xml to

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

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

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

回答by Sam

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

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

回答by Nandan

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

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

    </context:component-scan>  

For Example If Group Id-com.cavesofprogramming.spring.webthen

例如 If Group Id-com.cavesofprogramming.spring.webthen

    <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.

结果是在 maven 构建中找不到控制器,因此映射没有完成到正确的 jsp。

回答by Rahul

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

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

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.

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

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

  3. 在控制器类中 @RequestMapping 值应该与 web.xml url-pattern 参数相同。

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

回答by iragond

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

我也遇到了同样的问题 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

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

回答by Jyot

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.

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

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

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

Screenshot attached for reference.

附上截图供参考。

enter image description here

在此处输入图片说明

回答by menoktaokan

I just checked the servlet.xmlfile and solved the problem:

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

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

回答by mainaak

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.

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

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

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

View image for viewing the project

查看图像以查看项目

回答by Samuelhuckelson

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

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

Adding this resolved my issue. Hope it helps.

添加这个解决了我的问题。希望能帮助到你。