繁体   English   中英

Spring boot Ambiguous handler

[英]Spring boot Ambiguous handler

我使用Spring Boot。 我创建了这两个方法:

@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
public UserAppDto getNameByUserId(@PathVariable("userId") Long userId) {
    return userService.getByUserId(userId);
}

@RequestMapping(value = "/user/{username}", method = RequestMethod.GET)
public UserAppDto getNameByUsername(@PathVariable("username") String username) {
    return userService.getNameByUsername(username);
}

当我尝试登录Web应用程序时,我得到:

java.lang.IllegalStateException:为HTTP路径' http:// localhost:8080 / rest / user / bsmith '映射的不明确的处理程序方法:{public com.zenar.dto.UserAppDto com.zenar.controller.UserController.getNameByUsername(java。 lang.String),public com.zenar.dto.UserAppDto com.zenar.controller.UserController.getNameByUserId(java.lang.Long)}

好像,它无法对数据类型做出改变。

那么需要修改URL吗? 最新版本中的任何修复?

根据Spring MVC文档 ,当URL匹配多个模式时,使用排序来查找最具体的匹配

具有较低URI变量和通配符计数的模式被认为更具体。 例如/hotels/{hotel}/*有1个URI变量和1个外卡,被认为比/hotels/{hotel}/**更具体,它们是1个URI变量和2个外卡。

如果两个模式具有相同的计数,则更长的模式被认为更具体。 例如/foo/bar*/foo/*更长并且被认为更具体。

当两个模式具有相同的计数和长度时,具有较少通配符的模式被认为更具体。 例如/hotels/{hotel}/hotels/*更具体。

应用这些规则后,当Spring MVC无法确定哪一个更具体时,它将抛出该异常。 解决此问题的一种方法是使其中一个更具体

@RequestMapping(value = "/user/{userId:\\d+}", method = RequestMethod.GET)
public UserAppDto getNameByUserId(@PathVariable("userId") Long userId) {
    return userService.getByUserId(userId);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM