RequestMappingHandlerMapping是截断RestController

时间:2015-10-28 01:25:53

标签: java spring rest spring-mvc tomcat

我似乎对如何将RestController添加到我的应用程序感到困惑。我在这里非常接近,但是当我点击网址http://localhost:8080/rest/username/test时,我的日志中会显示以下内容。

2015-10-27 20:12:23,427 26366 [http-nio-8080-exec-5] DEBUG o.s.security.web.FilterChainProxy - /rest/username/test reached end of additional filter chain; proceeding with original chain 2015-10-27 20:12:23,427 26366 [http-nio-8080-exec-5] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'Rest' processing GET request for [/rest/username/test] 2015-10-27 20:12:23,427 26366 [http-nio-8080-exec-5] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /username/test 2015-10-27 20:12:23,428 26367 [http-nio-8080-exec-5] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/username/test] 2015-10-27 20:12:23,428 26367 [http-nio-8080-exec-5] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Matching patterns for request [/username/test] are [/**]

由于" dot"(。),我发现很多与截断路径变量相关的stackoverflow页面,但我的路径没有" dot"并且现在只返回一个字符串。

在我看来,RequestMappingHandlerMapping对象在尝试查找与适当方法匹配时截断路径。我只是不确定为什么,以及如何让它正常工作。

RestController

@RestController 
@RequestMapping(value = "/rest/{username}")
public class RestController {

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
    return "Rest controller data.";
}

}

的web.xml

<servlet-mapping>
    <servlet-name>Rest</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

其余-servlet.xml中

<!-- mvc config -->
<mvc:annotation-driven/>

<!-- scan for controller -->  
<context:component-scan base-package="abnd.pue.rest"/>

1 个答案:

答案 0 :(得分:1)

感谢您的帮助。问题是上下文路径是部署属性,不应将其添加到控制器的映射中。

修复

<强> RestController

@RestController 
@RequestMapping(value = "/{username}")
public class RestController {

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
return "Rest controller data.";
}

}