Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC controller not invoked by Tomcat

Update: Somehow, after another round of adjustments and redeployment, localhost:8080/ping-1.0/ping started working. Configuration files are still as below. I wish I knew what I fixed without knowing it, but it is solved now.

I've been wrestling with this for a couple days, tried all sorts of solutions I've seen here and elsewhere, and nothing has worked. I have a Spring MVC controller deployed in Tomcat, but can't access it.

Tools:

Spring 3.2.0
Tomcat 7
Java 1.6

Spring controller:

@Controller
public class PingController {

@RequestMapping("/ping")
public String ping (Model model) throws Exception {
    System.out.println("ping ping ping");
    String s = (new Date()).toString();
    model.addAttribute("message", s);
    return "ping";
}
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>
    <servlet-name>ping</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/ping-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>ping</servlet-name>
    <url-pattern>/ping</url-pattern>
</servlet-mapping>

</web-app>

ping-servlet.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    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">

<context:component-scan base-package="myclass.ping"/>

<mvc:annotation-driven />
</beans>

The WAR file is called ping-1.0.war. Deployment seems to go fine. I see a directory called ping-1.0 in $CATALINA_BASE/webapps and this in catalina.log:

INFO: Mapped "{[/ping],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String myclass.ping.PingController.ping(org.springframework.ui.Model) throws java.lang.Exception

Tomcat is running on port 8080. I can access localhost:8080/manager for instance. But localhost:8080/ping returns a 404 message from Tomcat. I see nothing in the logs other than a record of a GET request. No errors at all. I've tried a lot of variations of request mapping, URL filter, etc. and just can't get this to work.

like image 638
pmext Avatar asked Apr 29 '13 18:04

pmext


2 Answers

You don't have the context root on your URL and you actually need to have /ping/ping because both the dispatcher servlet and your ping controller are mapped to /ping. Try this:

http://localhost:8080/ping-1.0/ping/ping
like image 193
clav Avatar answered Nov 15 '22 04:11

clav


@RequestMapping("/ping")

Means /ping relative to the URL the dispatcher servlet listens on.

<url-pattern>/ping</url-pattern>

Here comes the problem. This makes your dispatcher servlet listen to one URL and one URL only. And that is assumingly localhost:8080/ping-1.0/ping. But your controller method is relative to that, so it would be localhost:8080/ping-1.0/ping/ping, and the disptacher servlet does not react on that URL. You have to use a pattern:

<url-pattern>/ping/*</url-pattern>

Now the dispatcher servlet can listen on all URLs starting with localhost:8080/ping-1.0/ping.

One final note: Depending on your configuration it could be that you have to omit ping-1.0.

like image 45
a better oliver Avatar answered Nov 15 '22 04:11

a better oliver