Reputation: 1661
I created a Spring project in Eclipse. The problem is that, the URL is not getting resolved in the way I think it should.
web.xml
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/files/*</url-pattern>
<url-pattern>/filestest/*</url-pattern>
</servlet-mapping>
This is my mvc-dispatcher-servlet.xml
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.github.myproject.controllers" />
</beans>
This is my Controller file:
@Controller
@RequestMapping("/filestest")
public class TestController {
@RequestMapping(value="", method=RequestMethod.GET)
public @ResponseBody String getBase() {
System.out.println("Base");
return "Base";
}
@RequestMapping(value="/", method=RequestMethod.GET)
public @ResponseBody String getRoot() {
System.out.println("Root");
return "Root";
}
@RequestMapping(value="abc", method=RequestMethod.GET)
public @ResponseBody String getABC() {
System.out.println("ABC");
return "ABC";
}
@RequestMapping(value = "/abc", method=RequestMethod.GET)
public @ResponseBody String getBaseAbc() {
System.out.println("Base ABC");
return "Base ABC";
}
@RequestMapping(value="/{pathVar}", method=RequestMethod.GET)
public @ResponseBody String getPathVar(@PathVariable(value="pathVar") String pathVar) {
System.out.println(pathVar);
return pathVar;
}
}
Here are the output I got
http://mylocalhost.com/filestest/abc - 404
http://mylocalhost.com/filestest/ - 404
http://mylocalhost.com/filestest - Valid Output - "Base"
From my understanding of Spring documentation, the web.xml should route all /filestest requests to DispatcherServlet -> which routes the request to my Controller -> which should then match the correct method.
Can someone please help me figure out why I am getting 404 File not found error for URLs - http://mylocalhost.com/filestest/abc and http://mylocalhost.com/filestest/ when I try to deploy and test my application?
Upvotes: 1
Views: 580
Reputation: 18194
Remove /filestest
from the controller's RequestMapping
annotation.
Spring is using only the mapped path part (*
) so there is no need to have the servlet prefix inside your RequestMapping
.
Upvotes: 1