eclipsehttpspring-mvctomcattcserver

Getting HTTP 404 Error in Eclipse project


I'm trying to learn how to run a Spring MVC project on a server (still very new at this), and I'm getting an http 404 error.. I'm using Eclipse Luna 4.4.1, Java 7, Spring 4.1.4 and Pivotal tc server 3.0.2 on a Windows 7 machine. Before I go further, let me say that I get the same 404 error when creating a simple practice project using Tomcat 7, so I think the problem is somewhere in my configuration of the servers (both in tc server and in Tomcat). Here is a screenshot showing my project directory, the servers I've tried running, and the error page: enter image description here

The program code seems fine. When I run my HomeControllerTest (which I've basically gotten from the new 4th edition of Spring in Action by Craig Walls), it compiles and passes. Where else do I need to look to troubleshoot this problem? ` package com.kwalker.practicewellness;

import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.kwalker.practicewellness.web.HomeController;

public class HomeControllerTest {

@Test
public void testHomePage() throws Exception {
    HomeController controller = new HomeController();
    MockMvc mockMvc = standaloneSetup(controller).build();

    mockMvc.perform(get("/")).andExpect(view().name("home"));

}

}

` Here is the HomeController class:

package com.kwalker.practicewellness.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {

@RequestMapping(value="/", method=RequestMethod.GET)
public String home() {
    return "home";
}
}

Here is the WellnessWebAppInitializer:

package com.kwalker.practicewellness.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WellnessWebAppInitializer extends
    AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] { RootConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[] { WebConfig.class };
}

@Override
protected String[] getServletMappings() {
    return new String[] { "/" };
}

}

Here is the RootConfig:

package com.kwalker.practicewellness.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages={"practicewellness"}, 
            excludeFilters={@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)})
public class RootConfig {

}

Here is WebConfig:

package com.kwalker.practicewellness.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("practicewellness.web")
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public ViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    return resolver;
}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

}

Here is the home.jsp page:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Welcome</title>
<link rel="stylesheet"
        type="text/css"
        href="<c:url value="/resources/style.css"/>">
</head>
<body>
<h1>
Welcome to Vital Potential!  
</h1>

<P>  The time on the server is ${serverTime}. </P>
</body>
</html>

Here is the output from Maven build:

[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder    org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Practice Wellness 1.0.0-BUILD-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ practicewellness ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ practicewellness ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.954 s
[INFO] Finished at: 2015-01-04T10:37:59-07:00
[INFO] Final Memory: 8M/115M
[INFO] ------------------------------------------------------------------------

Here is the output on the Console: (only one WARNING, and it's the same warning they get on the training video I'm watching, which runs fine for them):

Jan 04, 2015 10:42:37 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to  'org.eclipse.jst.jee.server:Practice Wellness' did not find a matching property.
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/7.0.57
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Nov 3 2014 08:39:16 UTC
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         7.0.57.0
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 7
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.1
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JAVA_HOME:             C:\Program Files\Java\jdk1.7.0_71\jre
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.7.0_71-b14
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:            C:\Users\kyle\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\Apache-Tomcat\apache-tomcat-7.0.57
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -    Dcatalina.base=C:\Users\kyle\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Apache-Tomcat\apache-tomcat-7.0.57
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: - Dwtp.deploy=C:\Users\kyle\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Apache-Tomcat\apache-tomcat-7.0.57\endorsed
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Jan 04, 2015 10:42:37 AM 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.7.0_71\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\Orac le\Java\javapath;C:\Program Files (x86)\HP SimplePass 2011\x64;C:\Program Files (x86)\HP SimplePass  2011\;;C:\Program Files\Broadcom\Broadcom 802.11\Driver;;C:\Program Files\Common Files\Microsoft   Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows  Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;C:\Program Files (x86)\Common  Files\Roxio Shared\DLLShared\;C:\Program Files (x86)\Common Files\Roxio  Shared\12.0\DLLShared\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\WIDCOMM\Bluetooth  Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;C:\Program  Files\Broadcom\WHL\;C:\Program Files\Broadcom\WHL\syswow64;C:\Program  Files\Broadcom\WHL\SysWow64\;C:\Program Files\Broadcom\WHL\SysWow64\syswow64;C:\Program Files  (x86)\Intel\Services\IPT\;C:\Program Files\Java\jdk1.8.0_25\bin;C:\Program Files\Gradle\Gradle- 2.0\gradle-2.2.1\bin;.
Jan 04, 2015 10:42:37 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jan 04, 2015 10:42:37 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jan 04, 2015 10:42:37 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 911 ms
Jan 04, 2015 10:42:38 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jan 04, 2015 10:42:38 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.57
Jan 04, 2015 10:42:40 AM org.apache.catalina.core.ApplicationContext log
INFO: Spring WebApplicationInitializers detected on classpath:  [com.kwalker.practicewellness.config.WellnessWebAppInitializer@4eda480b]
Jan 04, 2015 10:42:40 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization  started
INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Refreshing  Root WebApplicationContext: startup date [Sun Jan 04 10:42:40 MST 2015]; root of context hierarchy
INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Registering annotated classes: [class com.kwalker.practicewellness.config.RootConfig]
 INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 524 ms
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcher': initialization started
Jan 04, 2015 10:42:41 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Refreshing  WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sun Jan 04 10:42:41 MST  2015]; parent: Root WebApplicationContext
INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Registering annotated classes: [class com.kwalker.practicewellness.config.WebConfig]
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/**]  onto handler of type [class  org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter -  Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date   [Sun Jan 04 10:42:41 MST 2015]; parent: Root WebApplicationContext
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcher': initialization completed in 1029 ms
Jan 04, 2015 10:42:42 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jan 04, 2015 10:42:42 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jan 04, 2015 10:42:42 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4348 ms

Solution

  • It looks like in all your config you're using the @ComponentScan incorrectly - the base package name is missing the com.kwalker. part - my guess is that the component scan isn't picking up your home controller.

    Try updating the base package in your configs to full package name.

    Also, if you start the server in INFO logging mode you should get details of all the controller/paths that have been registered - that might shed some more light on it.