我正在尝试为Spring Boot Rest项目实现继承的控制器。如您在示例代码段中所看到的,BaseTableController上的所有控制器都有标准端点,但是对于某些具体的控制器,我需要重写方法。当我像这样实现它时,我在启动时会遇到Ambiguous mapping. Cannot map 'clientRfmController' method
异常。
public abstract class BaseTableController<T extends BaseTableModel> {
@Override
public BaseTableService<T> getService() {
return (BaseTableService<T>) super.getService();
}
@GetMapping({
PATH_GET,
PATH_VIEW_GET}
)
public T getWithParam(@RequestParam UUID gid) {
T t = null;
if (gid != null) {
t = getService().get(gid);
}
return getWithErrorCheck(t);
}
}
-
@RestController
@RequestMapping(TBL_CLIENTRFM)
public class ClientRfmController extends BaseTableController<ClientRfmCommon> {
@Override
public ClientRfmService getService() {
return (ClientRfmService) super.getService();
}
@GetMapping(value = {PATH_GET, PATH_VIEW_GET})
public List<ClientRfmCommon> getByAccid(Long accid, @RequestParam(required = false) UUID gid) {
List<ClientRfmCommon> byAccid = null;
if (gid != null) {
byAccid = Collections.singletonList(super.getWithParam(gid));
} else {
byAccid = Collections.singletonList(getService().getByAccid(accid));
}
return byAccid;
}
}
我通过实现自定义RequestMappingHandlerMapping找到了一种解决方法。
@Configuration
public class WebMvcRegistrationsConfig implements WebMvcRegistrations {
@Override
public OoRequestMappingHandlerMapping getRequestMappingHandlerMapping() {
OoRequestMappingHandlerMapping ooRequestMappingHandlerMapping = new OoRequestMappingHandlerMapping();
ooRequestMappingHandlerMapping.setOrder(0);
return ooRequestMappingHandlerMapping;
}
}
-
public class OoRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
@Override
protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) {
HandlerMethod existingHandlerMethod = getHandlerMethods().get(mapping);
if (existingHandlerMethod != null) {
HandlerMethod handlerMethod = createHandlerMethod(handler, method);
if (handlerMethod.getMethod().getDeclaringClass().isAssignableFrom(existingHandlerMethod.getMethod().getDeclaringClass())) {
logger.warn(handlerMethod.getBeanType().getSimpleName() + " type (" + handlerMethod.getMethod().getDeclaringClass().getSimpleName() + "->" + handlerMethod.getMethod().getName() +
") registration omitted to avoid ambigious mapping (" + existingHandlerMethod.getMethod().getDeclaringClass().getSimpleName() + "->" + existingHandlerMethod.getMethod().getName() + ")");
return;
}
unregisterMapping(mapping);
}
super.registerHandlerMethod(handler, method, mapping);
}
}
答案 0 :(得分:0)
@GetMapping({
PATH_GET,
PATH_VIEW_GET}
)
在父类和子类中都重复,因此您将面临歧义映射。请更改一个路径。