我在spring webservices上做示例,当我运行此示例时,我收到以下错误
在DispatcherServlet中找不到名为'springrest'的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'
如何解决这些问题。感谢任何帮助。
答案 0 :(得分:0)
将您的springrest
servlet网址格式更改为以下任意一种
<url-pattern>/SpringRestfulWebServicesExample/*</url-pattern>
或
<url-pattern>/</url-pattern>
由于带有/*
的servlet网址格式匹配
http://<ip>:<port>/*
不是相对于您的上下文名称SpringRestfulWebServicesExample
的路径。对于相对路径,请尝试使用模式/
。它将匹配http://<ip>:<port>/SpringRestfulWebServicesExample/*
更新 在pom.xml中配置的spring版本和bean配置文件中使用的模式版本应该匹配。 配置文件中提供的模式是spring 3.0.x,它不会出现在类路径jars中。对于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>
答案 1 :(得分:0)
问题在于您将项目结构放在src>main>resources
内的 java 文件中。
您需要将其src>main>java