gpt4 book ai didi

spring - 使用 ControllerClassNameHandlerMapping 发现模糊映射

转载 作者:行者123 更新时间:2023-12-04 06:23:39 32 4
gpt4 key购买 nike

如何在方法之上没有显式注释的情况下将请求映射到方法?例如,以下请求:

http://somedomain:8080/SampleSpring/access/loginFailed

应该映射到

“AccessController”的“loginFailed”方法,无需对方法进行显式注释,例如:

@RequestMapping("/access/loginFailed")

这是我的 Spring 配置:

<context:component-scan base-package="com.robikcodes.samplespring"/>
<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="basePackage" value="com.robikcodes.samplespring.controller"/>
<property name="caseSensitive" value="true"/>
<property name="defaultHandler">
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
</property>
</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>

这是我的 Controller :

@Controller
public class AccessController{

@RequestMapping(method = RequestMethod.GET)
public void login(ModelMap m) {}

@RequestMapping(method = RequestMethod.GET)
public String loginFailed(ModelMap m) {
m.addAttribute("error", "true");
return "access/login";
}

@RequestMapping(method = RequestMethod.GET)
public String logout(ModelMap m) {
m.addAttribute("logoutStatus","true");
return "access/login";
}
}

我收到以下错误(似乎只有登录方法被正确映射):

org.apache.catalina.LifecycleException: 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 'accessController' bean method 
public java.lang.String com.robikcodes.samplespring.controller.AccessController.logout(org.springframework.ui.ModelMap)
to {[],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'accessController' bean method
public void com.robikcodes.samplespring.controller.AccessController.login(org.springframework.ui.ModelMap) mapped.

最佳答案

您正在使用 ControllerClassNameHandlerMapping假设不正确;来自 Java 文档:

Implementation of HandlerMapping that follows a simple convention for generating URL path mappings from the class names of registered Controller beans as well as @Controller annotated beans.

文档没有说它也遵循方法名。比较 Controller 的“处理程序映射”的主要引用是放在方法上的 @RequestMapping 注释。因此,对于您的 Controller ,Spring 将它们读取为:

{methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}

对于 所有 AccessController 中定义的方法,具有以下 @RequestMapping:

@RequestMapping(method = RequestMethod.GET)

这就是您看到模棱两可的异常的原因。

据我了解,最干净的解决方案是使用@RequestMappingvalue 属性来定义不同的请求URI。不建议尝试将请求 URI 映射到方法名称的解决方案。

关于spring - 使用 ControllerClassNameHandlerMapping 发现模糊映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20739366/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com