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.
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
@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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With