【发布时间】:2014-04-19 20:51:09
【问题描述】:
在我的控制器中,我有两种方法来处理来自用户的请求。
@RequestMapping(method = RequestMethod.GET)
@PreAuthorize("hasRole('ROLE_INTERNAL_USER')")
public String homeInt() {
return "redirect:/internal";
}
@RequestMapping(method = RequestMethod.GET)
@PreAuthorize("hasRole('ROLE_EXTERNAL_USER')")
public String homeExt() {
return "redirect:/external";
}
例外:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'loginController' bean method
public java.lang.String de.mark.project.web.controller.LoginController.homeInt()
to {[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'loginController' bean method
public java.lang.String de.mark.project.web.controller.LoginController.homeExt() mapped.
但问题是这两种方法都不能映射到一个请求方法或一个 URI。有什么解决方案可以按照我的逻辑映射请求吗?
【问题讨论】:
-
方法的主体应该根据你拥有的任何信息做出决定,并重定向到正确的 url,内部或外部。
-
“方法的主体应该做出决定”是什么意思?我的注释完成了这项工作。不是吗?
-
大概,您想要一个带有单一方法 (GET) 的单一 URL,那么您将不得不使用其他方法来确定正确的响应是内部响应还是外部响应。您可以编写一个方法来响应请求,并在末尾添加
if语句,从而导致外部或内部重定向。 -
真的,要回答这个问题,我需要更多地了解请求本身。内部响应与外部响应有何区别?你怎么知道客户是内部的还是外部的?仅仅是他们的角色吗?您不能按角色路由请求,只能通过请求映射。
标签: spring spring-mvc spring-security