Reputation: 1253
There are many questions relating to this error on Stack Overflow, and I've tried the solutions to the most pertinent ones without success. Here is my problem.
I am trying to map this request: /user/{userId}
where userId
is a String. I am able to handle GET requests to /user
with the following annotated class and Spring configuration:
UserController.java
@Controller
@RequestMapping("/user")
public class UserController {
private static final Logger log = Logger.getLogger(UserController.class.getName());
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody String info() {
log.debug("mapping succeeded!");
return "<H1>foo</H1>";
}
}
web/WEB-INF/user-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
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="com.example"/>
</beans>
web.xml
<servlet>
<servlet-name>user</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>user</servlet-name>
<url-pattern>/user/*</url-pattern>
</servlet-mapping>
Then when I request /user
2011-01-14 15:47:41,942 DEBUG [com.example.rest.UserController] (http-11080-1) mapping succeeded!
Now to do something interesting. I change my code to the following:
@Controller
@RequestMapping("/user")
public class UserController {
private static final Logger log = Logger.getLogger(UserController.class.getName());
@RequestMapping(value="/{userId}", method=RequestMethod.GET)
public @ResponseBody String info(@PathVariable String userId) {
log.debug("mapping succeeded! userId=" + userId);
return "<H1>foo</H1>";
}
}
I have the dreaded No mapping found...
(main) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@36598d00: defining beans [userController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
(main) instantiating UserController
(main) Mapped URL path [/user/*] onto handler 'userController'
(main) Mapped URL path [/user/*.*] onto handler 'userController'
(main) Mapped URL path [/user/*/] onto handler 'userController'
...
(http-11080-1) No mapping found for HTTP request with URI [/user] in DispatcherServlet with name 'user'
(http-11080-1) No mapping found for HTTP request with URI [/user/foo] in DispatcherServlet with name 'user'
(http-11080-1) No mapping found for HTTP request with URI [/user/] in DispatcherServlet with name 'user'
What am I doing wrong?
Upvotes: 7
Views: 15249
Reputation: 47974
You don't usually include the actual servlet path that your dispatcher servlet is mapped to as part of your request mapping. The request mapping is relative to the dispatcher. In the degenerate first case the dispatcher is smart enough to figure out what you meant, but when you start adding path variables that breaks down. You should be able to access /user/user/foo
and get what you're looking for, with your current setup.
Upvotes: 9