2014-09-23 222 views
0

访问http://api:8080/users这404级的时候,我得到这个错误,我得到这个错误在我的控制台:当我访问http://localhost:8080/api/usersorg.springframework.web.servlet.DispatcherServlet noHandlerFound

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' 

我在浏览器中得到了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"); 
    } 
} 

如果任何人有任何线索,我会非常感激。我查阅了几个与我得到的错误相同的问题,但常见的解决方案是添加已有的,所以我不知道还有什么可以做。谢谢。

编辑:我刚刚更新了所有的代码,以最新的参考文档是什么,我仍然得到同样的错误。 编辑:更新显示我目前的档案状态。

+0

阅读Spring参考文档,你与有关Spring MVC的新配置混合在一起的旧配置。你的代码是错误的。 – 2014-09-23 22:16:24

+2

你能更具体一点吗? – wonza 2014-09-23 22:22:58

+0

我用参考文档中的最新代码更新了我的问题,但仍然收到相同的错误。 – wonza 2014-09-23 23:08:11

回答

1

改变你的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> 

和调度 - 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为您的代码运行

+0

没有什么我试过,但我不得不添加:http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd让它工作,但我仍然得到相同的错误 – wonza 2014-09-24 00:57:04

+0

你得到了什么错误? – 2014-09-24 05:05:33

+0

它显示在我的问题的顶部 – wonza 2014-09-24 14:10:39

0

需要添加以下行的弹簧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. 
0
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. 
+0

请正确格式化您的代码,以便OP可以确定什么是评论和什么是代码。 – 2017-11-16 12:15:16

相关问题