Spring MVC - No mapping found for URI request

There are many questions related to this error in Stack Overflow, and I have tried solutions for the most suitable without success. Here is my problem.

I am trying to display this query: /user/{userId} where userId is a string. I can handle GET requests on /user with the following annotated class and Spring :

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 let's 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 a scary No mapping found...

 (main) Pre-instantiating singletons in org.s pringframework.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?

+7
source share
1 answer

Usually you do not specify the actual servlet path that your dispatcher servlet displays as part of your query mapping. The query display refers to the dispatcher. In the degenerate first case, the dispatcher is smart enough to understand what you had in mind, but when you start adding path variables that break. You must have access to /user/user/foo and get what you are looking for with your current setting.

+9
source

All Articles