Error 404 of a Spring MVC application

1

I'm following Caelum's handout. I made the application "hello world" with Spring MVC, which has both the configuration and the Controller .

However, when accessing the URL localhost:8080/WebSpring/olaMundoSpring , the 404 error page appears.

My application code is available from GitHub:

  

link

When I click on the URL it does not generate any errors in TomCat

Out 07, 2014 10:12:43 AM org.apache.catalina.core.AprLifecycleListener init
INFORMAÇÕES: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Out 07, 2014 10:12:43 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
ADVERTÊNCIA: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:WebSpring' did not find a matching property.
Out 07, 2014 10:12:43 AM org.apache.coyote.AbstractProtocol init
INFORMAÇÕES: Initializing ProtocolHandler ["http-bio-8080"]
Out 07, 2014 10:12:43 AM org.apache.coyote.AbstractProtocol init
INFORMAÇÕES: Initializing ProtocolHandler ["ajp-bio-8009"]
Out 07, 2014 10:12:43 AM org.apache.catalina.startup.Catalina load
INFORMAÇÕES: Initialization processed in 962 ms
Out 07, 2014 10:12:43 AM org.apache.catalina.core.StandardService startInternal
INFORMAÇÕES: Starting service Catalina
Out 07, 2014 10:12:43 AM org.apache.catalina.core.StandardEngine startInternal
INFORMAÇÕES: Starting Servlet Engine: Apache Tomcat/7.0.55
Out 07, 2014 10:12:46 AM org.apache.catalina.core.ApplicationContext log
INFORMAÇÕES: No Spring WebApplicationInitializer types detected on classpath
log4j:WARN No appenders could be found for logger (org.springframework.web.servlet.DispatcherServlet).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Out 07, 2014 10:12:46 AM org.apache.catalina.core.ApplicationContext log
INFORMAÇÕES: Initializing Spring FrameworkServlet 'springmvc'
Out 07, 2014 10:12:47 AM org.apache.coyote.AbstractProtocol start
INFORMAÇÕES: Starting ProtocolHandler ["http-bio-8080"]
Out 07, 2014 10:12:47 AM org.apache.coyote.AbstractProtocol start
INFORMAÇÕES: Starting ProtocolHandler ["ajp-bio-8009"]
Out 07, 2014 10:12:47 AM org.apache.catalina.startup.Catalina start
INFORMAÇÕES: Server startup in 3562 ms
    
asked by anonymous 06.10.2014 / 17:00

2 answers

2

If you want to simply return a string using as text / plain, please put the @ResponseBody annotation in your method:

@RequestMapping("/olaMundoSpring")
@ResponseBody
public String execute() {
    System.out.println("Executando a lógica com Spring MVC");
    return "ok";
}

As you can see, Spring MVC can be using jsp with view handler, as it does not find an ok.jsp or something like that, it responds with 404.

    
07.10.2014 / 15:19
0

I downloaded your GitHub project and tested the application without any changes: It worked!

Probably a Tomcat configuration problem or something related to the runtime environment. I used JDK 7 and Tomcat 7 for testing.

Something that will help you identify the exact problem, it is also recommended for any project, is to properly configure the application logs.

For example, create a file named log4j.xml directly in the src folder of the project with the following content:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
                     xmlns:log4j='http://jakarta.apache.org/log4j/'>

   <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
      </layout>
   </appender>

   <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
      <param name="append" value="false"/>
      <param name="file" value="output.log"/>
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
      </layout>
   </appender>

   <root>
      <level value="INFO"/>
      <appender-ref ref="consoleAppender"/>
      <appender-ref ref="fileAppender"/>
   </root>

</log4j:configuration>

With this, important Spring information will be displayed on the console and written to a log file. One of them is highlighted below:

  

09 Oct 2014 16:37:51 INFO RequestMappingHandlerMapping - Mapped ", methods = [], params = [], headers = [], consumes = [], produces = [], custom = ]} "onto public java.lang.String br.com.caelum.tarefas.controller.OlaMundoController.execute ()

     

09 Oct 2014 16:37:51 INFO DispatcherServlet - FrameworkServlet 'springmvc': initialization completed in 1222 ms

This information is the mapping of your Controller URL. Make sure everything is correct with the new log information.

    
09.10.2014 / 21:43