,我發現了以下情況例外,如果我使用多級網址類像@RequestMapping("/api/v0.1")
發現:曖昧映射使用級多層次@RequestMapping URL時
java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'userController'
bean method getUsers()|to {[/api/v0.1]}: There is already 'userController' bean
method getUser(java.lang.String) mapped.
這就像方法級映射不進入根本沒有考慮。
但是,如果我把@RequestMapping("/api")
即刪除/v0.1
部分也沒關係。
這裏是剝離到最小的情況下,配置:
@Controller
@RequestMapping("/api/v0.1")
public class UserController {
@RequestMapping(value = "/users")
@ResponseBody
public List<User> getUsers() {
return null;
}
@RequestMapping(value = "https://stackoverflow.com/users/{username}")
@ResponseBody
public User getUser(@PathVariable String username) {
return null;
}
}
的web.xml
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
的servlet-context.xml中:
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- Forwards requests to the "/" resource to the "welcome" view -->
<mvc:view-controller path="/" view-name="home"/>
<!-- Handles HTTP GET requests for /assets/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
<mvc:resources mapping="/assets/**" location="/assets/" />
我使用的是春天3.1。我也嘗試將alwaysUseFullPath
屬性設置爲RequestMappingHandlerMapping
bean的屬性,但它沒有改變這種情況。
我記得在類級別(沒有點)嘗試'@RequestMapping(「/ api/v01」)並且沒有得到結果,但我會再試一次並讓你知道。謝謝。 – SelimOber 2012-07-08 15:53:26
好吧,我確實嘗試了v0_1,它的工作原理。所以我正在用這個。 – SelimOber 2012-07-09 12:20:53
由於AntPathMatcher允許正則表達式,我認爲如果你逃脫了這個點,它會起作用。例如。 'V0 \ .1' – jasop 2013-02-02 11:14:18