404 error of a Spring MVC application

Asked

Viewed 1,198 times

1

I am following the Caelum booklet. I made the application "hello world" with Spring MVC, which has both the configuration and the respective Controller.

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

My app code is available on Github:

https://github.com/wladyband/SpringComErro/tree/master/WebSpring

when clicking on the URL does not generate any error 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
  • 1

    I compared your project with a basic example that I have and it doesn’t look like a problem. It might just be time to publish it on Tomcat. Could post the server startup log?

  • put the log, take a look at please

  • Unfortunately I couldn’t find the problem through the log. The only thing I found out is that Spring 3 doesn’t work in Java 8. I don’t have an earlier version to test the project now. Already tried to upgrade to Spring 4?

  • 404 is the error of not found.... checks if all the files are actually in patchs specified

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";
}

From what I could tell Spring MVC may be using jsp with view Handler, as it does not find an ok.jsp or something like it responds with 404.

  • Looking at the Github project, there is a ok.jsp.

  • So in this case you can also consider problems with permission if you are running Tomcat on linux, another possibility is the mapping of Servlet and one last one would be to try to make explicit that you are mapping this path (path) to requests with GET method.

0

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

It’s probably a Tomcat configuration problem or something related to the running environment. I used JDK 7 and Tomcat 7 for the tests.

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

For example, create a named file log4j.xml directly in the folder src 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 recorded in a log file. One of them is highlighted below:

09 Oct 2014 16:37:51 INFO Requestmappinghandlermapping - Mapped "{[/olaMundoSpring],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 URL Controller. Make sure everything is correct with the new log information.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.