【问题标题】:No mapping found for HTTP request with URI [] in DispatcherServlet with name [duplicate]在名称为 [重复] 的 DispatcherServlet 中找不到带有 URI [] 的 HTTP 请求的映射
【发布时间】:2016-04-01 15:19:40
【问题描述】:

我在 spring webservices 上做示例,当我运行这个示例时,我遇到了错误

在名称为“springrest”的 DispatcherServlet 中找不到具有 URI [/SpringRestfulWebServicesExample/] 的 HTTP 请求的映射

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 <web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>  
  <servlet-name>springrest</servlet-name>  
<servlet-class>  
 org.springframework.web.servlet.DispatcherServlet  
</servlet-class>  
<load-on-startup>1</load-on-startup>  
</servlet>  
<servlet-mapping>  
<servlet-name>springrest</servlet-name>  
<url-pattern>/*</url-pattern>  
</servlet-mapping>  
</web-app>

springrest-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"  
xmlns:context="http://www.springframework.org/schema/context"  
xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation=" http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-3.0.xsd   http://www.springframework.org/schema/mvc   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
<mvc:annotation-driven/>  
<context:component-scan base-package="org.arpit.java2blog.controller" />  
</beans>  

SpringrestfulwebserviceExample.xml

<dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
 </dependency>

 <dependency>  
 <groupId>javax.servlet</groupId>  
 <artifactId>javax.servlet-api</artifactId>  
 <version>3.1.0</version>  
 </dependency>  

  <dependency>  
  <groupId>org.springframework</groupId>  
   <artifactId>spring-core</artifactId>  
   <version>${spring.version}</version>  
  </dependency>  
  <dependency>  
  <groupId>org.springframework</groupId>  
  <artifactId>spring-webmvc</artifactId>  
  <version>${spring.version}</version>  
   </dependency>  
   </dependencies>
   <build>

   <finalName>SpringRestfulWebServicesExample</finalName>

   <plugins>  
  <plugin>  
  <groupId>org.apache.maven.plugins</groupId>  
  <artifactId>maven-compiler-plugin</artifactId>  
  <version>3.1</version>  
  <configuration>  
  <source>${jdk.version}</source>  
  <target>${jdk.version}</target>  
  </configuration>  
  </plugin>  
  </plugins>  
  </build>

  <properties>  
  <spring.version>4.2.1.RELEASE</spring.version>  
  <jdk.version>1.8</jdk.version>  
  </properties>

  </project>

Springrestcontroller.java

    @RestController  
    @RequestMapping("/hello")  
    public class SpringRestController {  
    @RequestMapping(value = "/{name}", method = RequestMethod.GET)  
    public String hello(@PathVariable String name) {  
    String result="Hello "+name;    
    return result;  
   }  
   }

在 springrest-servlet.xml 中我遇到了以下错误

1. cvc-elt.1: Cannot find the declaration of element 'beans'
2. Multiple annotations found at this line:
- schema_reference.4: Failed to read schema document   'http://www.springframework.org/schema/context/spring-context-3.0.xsd', because    1) could 
 not find the document; 2) the document could not be read; 3) the root   element of the document is not <xsd:schema>.
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:component-scan'.

错误

Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.30
 Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
 INFO: Server built:          Dec 1 2015 22:30:46 UTC
 Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
 INFO: Server number:         8.0.30.0
 Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
 INFO: OS Name:               Windows 7
  Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
  INFO: OS Version:            6.1
  Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
   INFO: Architecture:          amd64
   Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
   INFO: Java Home:             C:\Program Files\Java\jdk1.8.0_65\jre
   Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
   INFO: JVM Version:           1.8.0_65-b17
   Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
  INFO: JVM Vendor:            Oracle Corporation
   Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
  INFO: CATALINA_BASE:         C:\Program Files\Apache Software Foundation\Tomcat 8.0
  Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
  INFO: CATALINA_HOME:         C:\Program Files\Apache Software Foundation\Tomcat 8.0
  Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
  INFO: Command line argument: -Dcatalina.base=C:\Program Files\Apache Software Foundation\Tomcat 8.0
   Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dcatalina.home=C:\Program Files\Apache  Software Foundation\Tomcat 8.0
  Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
     INFO: Command line argument: -Dwtp.deploy=C:\Program Files\Apache Software Foundation\Tomcat 8.0\wtpwebapps
   Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
   INFO: Command line argument: -Djava.endorsed.dirs=C:\Program Files\Apache    Software Foundation\Tomcat 8.0\endorsed
  Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.VersionLoggerListener log
 INFO: Command line argument: -Dfile.encoding=Cp1252
  Dec 27, 2015 11:11:54 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
  INFO: The APR based Apache Tomcat Native library which allows optimal   performance in production environments was not found on the java.library.path:     C:\Program   Files\Java\jdk1.8.0_65\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Window   s;D:/supriya/bin/server;D:/supriya/bin;D:/supriya/lib/amd64;C:\ProgramData\Oracl  e\Java\javapath;c:\Program Files (x86)\Intel\iCLS Client\;c:\Program    Files\Intel\iCLS  Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\Syste        m32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL     SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;C:\Program   Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program   Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files   (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\nodejs\;C:\Program Files\TortoiseSVN\bin;C:\android-sdk-  win\tools;C:\android-sdk-win\platform-tools;C:\Program Files\Git\bin;C:\Users\star\AppData\Roaming\Appcelerator\Titanium   Studio;C:\Program Files\nodejs;C:\Program Files   (x86)\Java\jdk1.6.0_20\bin;C:\Program Files\Microsoft\Web Platform   Installer\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web   Pages\v1.0\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\SourceGear\Common\DiffMerge\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Users\star\AppData\Roaming\npm;C:\Users\star\Desktop\eclipse-jee-mars-1-win32-x86_64\eclipse;;.
  Dec 27, 2015 11:11:54 PM org.apache.coyote.AbstractProtocol init
 INFO: Initializing ProtocolHandler ["http-nio-8080"]
  Dec 27, 2015 11:11:54 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
 INFO: Using a shared selector for servlet write/read
 Dec 27, 2015 11:11:54 PM org.apache.coyote.AbstractProtocol init
 INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
 Dec 27, 2015 11:11:54 PM org.apache.tomcat.util.net.NioSelectorPool  getSharedSelector
INFO: Using a shared selector for servlet write/read
 Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1108 ms
 Dec 27, 2015 11:11:54 PM org.apache.catalina.core.StandardService startInternal
 INFO: Starting service Catalina
 Dec 27, 2015 11:11:54 PM org.apache.catalina.core.StandardEngine startInternal
 INFO: Starting Servlet Engine: Apache Tomcat/8.0.30
 Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.HostConfig deployDescriptor
 INFO: Deploying configuration descriptor C:\Program Files\Apache Software  Foundation\Tomcat  8.0\conf\Catalina\localhost\SpringRestfulWebServicesExample.xml
 Dec 27, 2015 11:11:54 PM org.apache.catalina.startup.SetContextPropertiesRule begin
 WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:SpringRestfulWebServicesExample' did not find a matching property.
 Dec 27, 2015 11:11:56 PM org.apache.jasper.servlet.TldScanner scanJars
 INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable   debug logging for this logger for a complete list of JARs that were scanned but   no TLDs were found in them. Skipping unneeded JARs during scanning can improve   startup time and JSP compilation time.
 Dec 27, 2015 11:11:56 PM org.apache.catalina.core.ApplicationContext log
 INFO: No Spring WebApplicationInitializer types detected on classpath
 Dec 27, 2015 11:11:56 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
 INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [219] milliseconds.
Dec 27, 2015 11:11:56 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'springrest'
 Dec 27, 2015 11:11:56 PM org.springframework.web.servlet.DispatcherServlet initServletBean
 INFO: FrameworkServlet 'springrest': initialization started
Dec 27, 2015 11:11:56 PM     org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'springrest-servlet': startup date [Sun Dec 27 23:11:56 IST 2015]; root of context hierarchy
Dec 27, 2015 11:11:56 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/springrest-servlet.xml]
  Dec 27, 2015 11:11:58 PM   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
 INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'springrest-servlet': startup date [Sun Dec 27 23:11:56 IST 2015]; root of context hierarchy
 Dec 27, 2015 11:11:58 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapt er initControllerAdviceCache
 INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace   'springrest-servlet': startup date [Sun Dec 27 23:11:56 IST 2015]; root of   context hierarchy
 Dec 27, 2015 11:11:58 PM org.springframework.web.servlet.DispatcherServlet initServletBean
 INFO: FrameworkServlet 'springrest': initialization completed in 1569 ms
  Dec 27, 2015 11:11:58 PM org.apache.catalina.startup.HostConfig deployDescriptor
 INFO: Deployment of configuration descriptor C:\Program Files\Apache   Software Foundation\Tomcat   8.0\conf\Catalina\localhost\SpringRestfulWebServicesExample.xml has finished in   3,832 ms
 Dec 27, 2015 11:11:58 PM org.apache.catalina.startup.HostConfig deployDirectory
 INFO: Deploying web application directory C:\Program Files\Apache Software  Foundation\Tomcat 8.0\webapps\docs
  Dec 27, 2015 11:11:58 PM org.apache.catalina.startup.HostConfig deployDirectory
  INFO: Deployment of web application directory C:\Program Files\Apache   Software Foundation\Tomcat 8.0\webapps\docs has finished in 39 ms
   Dec 27, 2015 11:11:58 PM org.apache.catalina.startup.HostConfig deployDirectory
   INFO: Deploying web application directory C:\Program Files\Apache   Software Foundation\Tomcat 8.0\webapps\manager
   Dec 27, 2015 11:11:58 PM org.apache.catalina.startup.HostConfig deployDirectory
   INFO: Deployment of web application directory C:\Program Files\Apache   Software Foundation\Tomcat 8.0\webapps\manager has finished in 55 ms
  Dec 27, 2015 11:11:58 PM org.apache.catalina.startup.HostConfig deployDirectory
  INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\ROOT
  Dec 27, 2015 11:11:58 PM org.apache.catalina.startup.HostConfig deployDirectory
  INFO: Deployment of web application directory C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\ROOT has finished in 31 ms
  Dec 27, 2015 11:11:58 PM org.apache.coyote.AbstractProtocol start
  INFO: Starting ProtocolHandler ["http-nio-8080"]
  Dec 27, 2015 11:11:58 PM org.apache.coyote.AbstractProtocol start
  INFO: Starting ProtocolHandler ["ajp-nio-8009"]
   Dec 27, 2015 11:11:58 PM org.apache.catalina.startup.Catalina start
  INFO: Server startup in 4031 ms
  Dec 27, 2015 11:12:46 PM org.springframework.web.servlet.PageNotFound  noHandlerFound
  WARNING: No mapping found for HTTP request with URI [/SpringRestfulWebServicesExample/hello/name] in DispatcherServlet with name 'springrest'

如何解决这些问题。感谢任何帮助。

【问题讨论】:

标签: java eclipse spring servlets


【解决方案1】:

问题在于您将 java 文件放在 src&gt;main&gt;resources 中的项目结构。

你需要把它放在src&gt;main&gt;java

【讨论】:

    【解决方案2】:

    将您的 springrest servlet url 模式更改为以下任一

    &lt;url-pattern&gt;/SpringRestfulWebServicesExample/*&lt;/url-pattern&gt;

    &lt;url-pattern&gt;/&lt;/url-pattern&gt;

    因为带有/* 的servlet url 模式将匹配

     http://<ip>:<port>/*
    

    不是相对于您的上下文名称SpringRestfulWebServicesExample 的路径。对于相对路径,请尝试使用模式/。它将匹配http://&lt;ip&gt;:&lt;port&gt;/SpringRestfulWebServicesExample/*

    更新: pom.xml 中配置的 spring 版本和 bean 配置文件中使用的 schema 版本应该匹配。 您的配置文件中提供的架构是 spring 3.0.x,它不会出现在类路径 jar 中。对于 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:mvc="http://www.springframework.org/schema/mvc"
           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/mvc 
           http://www.springframework.org/schema/mvc/spring-mvc.xsd 
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context.xsd">
      <mvc:annotation-driven/>  
      <context:component-scan base-package="org.arpit.java2blog.controller" />  
    </beans> 
    

    同时将这些依赖添加到 pom 中并尝试:

     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    

    【讨论】:

    • 我也收到错误 cvc-elt.1:在 springrest-servlet.xml 中找不到元素“beans”的声明
    • 更改模式后,网址是否有效?
    • @allDroid 查看我的更新。这是一个工作副本
    • 在 xml 中显示相同的错误,所以我无法在服务器上运行
    • 进行以上修改后是否可以触发maven build,并在tomcat webapps中手动部署生成的war文件,运行看看是不是eclipse的问题
    猜你喜欢
    • 2013-09-12
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 2017-05-01
    • 2016-12-06
    • 1970-01-01
    相关资源
    最近更新 更多