GradleDroid GradleDroid -4 years ago 198
Java Question

Unable to add Spring MVC dependency to Maven Project

I am new to Spring Mvc.. I am trying to create a Maven Based Spring Mvc Demo..

The basic "hello world" Demo is working fine. It prints the "Hello World" when I hit "http://localhost:8080/SampleRestful/"

But, the moment I add

spring-webmvc
dependency to my pom.xml and run it on server, it throws the Error 404 on hitting "http://localhost:8080/SampleRestful/".

Here is the pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.promact.maven</groupId>
<artifactId>SampleRestful</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SampleRestful Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>

</dependencies>
<build>
<finalName>SampleRestful</finalName>
</build>
</project>


Here is the error log :

Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/8.5.8
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Nov 3 2016 21:14:13 UTC
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 8.5.8.0
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 7
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 6.1
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Program Files\Java\jdk1.8.0_92\jre
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_92-b14
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: D:\SpringWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: C:\Program Files\Apache Software Foundation\Tomcat 8.5
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=D:\SpringWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 8.5
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=D:\SpringWorkspace
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Program Files\Apache Software Foundation\Tomcat 8.5\endorsed
Mar 24, 2017 12:19:48 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Mar 24, 2017 12:19:48 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_92\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_92/bin/server;C:/Program Files/Java/jre1.8.0_92/bin;C:/Program Files/Java/jre1.8.0_92/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\webserver\ImageMagick;C:\Program Files (x86)\ImageMagick-6.8.1-Q16;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Python27;D:\Nishant\Android\android-sdk\platform-tools;D:\Nishant\Android\android-sdk\tools;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Ant 1.8\bin;C:\Program Files (x86)\Ant 1.8\lib;C:\Program Files\Java\jdk1.7.0_06\bin;C:\Program Files\Java\jdk1.6.0_30\bin;C:\Program Files\OpenSSL\bin;C:\webserver\MySQL\bin;C:\Ruby193\bin;C:\Users\nishant\Downloads\libs\depot_tools\;D:\Nishant\Android\android-ndk-r8e\;C:\Python27\;C:\Python27\Scripts\;C:\Program Files\gradle-2.1-all\gradle-2.1\bin\;C:\Program Files\TortoiseGit\bin;C:\Users\m\AppData\Local\GitHub\PortableGit_054f2e797ebafd44a30203088cd3d58663c627ef\bin;D:\setups\apache-ant-1.9.4\bin;C:\Program Files\nodejs;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Java\jdk1.8.0_92\bin\;C:\Program Files\Java\jdk1.8.0_92\;C:\Program Files\nodejs\;D:\setups\java\apache-maven-3.3.9\bin;C:\Users\m\AppData\Roaming\npm;C:\Users\m\AppData\Local\atom\bin;D:\setups\java\spring-tool-suite-3.8.2.RELEASE-e4.6.1-win32-x86_64\sts-bundle\sts-3.8.2.RELEASE;;.
Mar 24, 2017 12:19:49 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Mar 24, 2017 12:19:49 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Mar 24, 2017 12:19:49 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Mar 24, 2017 12:19:49 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Mar 24, 2017 12:19:49 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 835 ms
Mar 24, 2017 12:19:49 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 24, 2017 12:19:49 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.5.8
Mar 24, 2017 12:19:49 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [110] milliseconds.
Mar 24, 2017 12:19:49 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor D:\SpringWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\SampleRestful.xml
Mar 24, 2017 12:19:49 PM org.apache.catalina.startup.SetContextPropertiesRule begin
WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:SampleRestful' did not find a matching property.
Mar 24, 2017 12:19:49 PM org.apache.catalina.core.ContainerBase addChildInternal
SEVERE: ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SampleRestful]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:596)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1805)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [org.apache.catalina.webresources.StandardRoot@3f797939]
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:112)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)
at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4842)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4974)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 10 more
Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [org.apache.catalina.webresources.DirResourceSet@572b1df8]
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:112)
at org.apache.catalina.webresources.StandardRoot.initInternal(StandardRoot.java:675)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
... 14 more
Caused by: java.lang.IllegalArgumentException: The directory specified by base and internal path [D:\SpringWorkspace\SampleRestful\target\m2e-wtp\web-resources]\[] does not exist.
at org.apache.catalina.webresources.DirResourceSet.checkType(DirResourceSet.java:251)
at org.apache.catalina.webresources.AbstractFileResourceSet.initInternal(AbstractFileResourceSet.java:145)
at org.apache.catalina.webresources.DirResourceSet.initInternal(DirResourceSet.java:259)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
... 16 more

Mar 24, 2017 12:19:49 PM org.apache.catalina.startup.HostConfig deployDescriptor
SEVERE: Error deploying configuration descriptor D:\SpringWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\SampleRestful.xml
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SampleRestful]]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:756)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:596)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1805)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

Mar 24, 2017 12:19:49 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deployment of configuration descriptor D:\SpringWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\SampleRestful.xml has finished in 15 ms
Mar 24, 2017 12:19:49 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler [http-nio-8080]
Mar 24, 2017 12:19:49 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler [ajp-nio-8009]
Mar 24, 2017 12:19:49 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 632 ms


I am unable to find the reason behind the issue. Please Help..!! :(

Answer Source

Spring MVC can't be used alone, it needs other dependecies like : Spring-core, Spring-context, Spring-beans and Spring-web.

If you want to start a new SpringMVC project I'll recommand you to do as follow (this is how I do when I start a new SpringMVC project):

1) Add Spring Tool Suite to Eclipse (go to "Help"(menu)->Eclipse Marketplace-> (in Find serch bar) enter "STS" and install it.

2) Then in Eclipse, go to File->Spring Legacy Project->Spring MVC Project (enter a project name), click next, enter a package name and finish.

You'll have a pre-configured Spring MVC project which works fine. I suggest you to change the versions of Java and other dependecies.

I hope this will help you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download