我在 Spring 3.0 中有一个现有应用程序,它使用 ControllerClassNameHandlerMapping 来映射控制器和方法,例如:
StartController.class is mapped to http://127.0.0.1/app/start/*
then
StartController.class has a method called init() that is mapped to http://127.0.0.1/app/start/init.html
这是我的配置:
@Bean
public ControllerClassNameHandlerMapping classNameControllerMappings() {
return new ControllerClassNameHandlerMapping() {{
setCaseSensitive(true);
setDefaultHandler(new UrlFilenameViewController());
setInterceptors(new Object[]
{callProgressionInterceptorHandler(),
callSessionInterceptorHandler(),
localeChangeInterceptor()});
}};
}
我的大多数控制器在每个控制器中都有 5-15 个请求映射方法。
但是,当我升级到 Spring 3.1+ 时,每个控制器的请求映射变得不明确,并且未正确映射。
我读过一种解决方案是显式添加方法名称:
@RequestMapping(method = RequestMethod.GET)
现在将是:
@RequestMapping(method = RequestMethod.GET, value = "init")
如果不需要的话,我真的不想手动将 @RequestMapping 值添加到 100 多个方法中。
有人可以帮忙提供更好的解决方案吗?
这是我不断收到的错误:
47672 [btpool0-1] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
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 'addressConfirmationController' bean method
public void com.comcast.ivr.d2.web.controllers.AddressConfirmationController.houseNumber_rc(org.springframework.ui.ModelMap)
to {[],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'addressConfirmationController' bean method
我还添加了 setOrder(1);到 ControllerClassNameHandlerMapping 仍然收到此错误。
更新:
我看到了http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html以下摘录:
在 Spring 3.1 之前,类型和方法级请求映射在两个单独的阶段进行检查——首先由 DefaultAnnotationHandlerMapping 选择控制器,然后由 AnnotationMethodHandlerAdapter 缩小要调用的实际方法的范围。
使用 Spring 3.1 中的新支持类,RequestMappingHandlerMapping 是决定哪个方法应处理请求的唯一位置。将控制器方法视为唯一端点的集合,其中每个方法都有从类型和方法级 @RequestMapping 信息派生的映射。
这是否意味着我无法像
为了实现这一点,我有数百种方法需要修改......:-(