简体   繁体   English

org.springframework.web.servlet.DispatcherServlet noHandlerFound

[英]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:访问 404 的http://api:8080/users时出现此错误,我在控制台中收到此错误:

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 警告:在 DispatcherServlet 中找不到带有 URI [/users] 的 HTTP 请求的映射,名称为“dispatcher”

When I access http://localhost:8080/api/users I 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.编辑:更新以显示我当前的文件状态。

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.附上截图供参考。

在此处输入图像描述

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 来运行您的代码

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.web then例如如果组Id-com.cavesofprogramming.spring.web那么

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

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.

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.在使用 web 原型创建 maven 项目时,我们在 main 下没有 java 目录。 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 需要的基本包。

servlet.xml and the location of the base-package image attached for reference servlet.xml 和附加的基本包图像的位置以供参考

View image for viewing the project查看图像以查看项目

in maven ,'java' folder was not created .在 maven 中,'java' 文件夹没有被创建。 I created as src/main/java and this issue was resolved .我创建为 src/main/java 并解决了这个问题。

it is suggested by jyot also in this post . jyot也在这篇文章中提出了建议。 org.springframework.web.servlet.DispatcherServlet noHandlerFound 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.原因 - 此问题背后的主要原因是 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.在 servlet.xml 文件中,您的基本包应指向控制器类所在的正确路径。

  2. @Controller should be mentioned over the controller class. @Controller 应该在控制器类中被提及。

  3. In controller class @RequestMapping value should be as same as the web.xml url-pattern parameter.在控制器类中,@RequestMapping 值应与 web.xml url-pattern 参数相同。

  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.在控制器类中,ModelAndView 返回的 .jsp 页面应该与指向 ViewResolver 的 servlet.xml 前缀值的路径相同。

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我在邮递员中点击不同的网址来解决这个问题只需检查您的邮递员您点击的网址

我刚刚检查了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我错过了添加 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" /> <mvc:annotation-driven /> <context:component-scan base-package="com.test" />

wrong package name as it was "com.test" and i had written "com.controller"..错误的包名,因为它是“com.test”,我写了“com.controller”..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.springframework.web.servlet.DispatcherServlet noHandlerFound:Spring MVC - org.springframework.web.servlet.DispatcherServlet noHandlerFound : Spring MVC org.springframework.web.servlet.DispatcherServlet noHandlerFound用于基本的Spring示例 - org.springframework.web.servlet.DispatcherServlet noHandlerFound for basic spring example org.springframework.web.servlet.DispatcherServlet noHandlerFound 404错误响应 - org.springframework.web.servlet.DispatcherServlet noHandlerFound 404 error response org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping found for HTTP request with URI in DispatcherServlet with name - org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping found for HTTP request with URI in DispatcherServlet with name 如何解决Spring框架项目错误org.springframework.web.servlet.DispatcherServlet noHandlerFound? - How to resolve Spring framework project error org.springframework.web.servlet.DispatcherServlet noHandlerFound? org.springframework.web.servlet.DispatcherServlet noHandlerFound 警告:GET /HelloWorld/helloweb 没有映射 - org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping for GET /HelloWorld/helloweb org.springframework.web.servlet.DispatcherServlet noHandlerFound 警告:GET /springMVCDemo/createAccount.html 没有映射 - org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping for GET /springMVCDemo/createAccount.html org.springframework.web.servlet.DispatcherServlet noHandlerFound(警告:未找到 HTTP 请求的映射) - org.springframework.web.servlet.DispatcherServlet noHandlerFound (WARNING: No mapping found for HTTP request) 资源处理程序Spring异常:org.springframework.web.servlet.DispatcherServlet noHandlerFound - resource handler Spring exception: org.springframework.web.servlet.DispatcherServlet noHandlerFound 错误[org.springframework.web.servlet.DispatcherServlet] - ERROR [org.springframework.web.servlet.DispatcherServlet]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM