繁体   English   中英

Spring Boot RestController继承和模棱两可的映射问题

[英]Spring Boot RestController inheritance and ambiguous mapping issue

我正在尝试为我的Spring Boot Rest项目实现继承的控制器。 如您在示例代码段中所看到的,BaseTableController上的所有控制器都有标准端点,但是对于某些具体的控制器,我需要重写方法。 当我像这样实现它时,我得到了Ambiguous mapping. Cannot map 'clientRfmController' method 启动时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);
    }

}
  1. 这是解决这个问题的正确方法吗?
  2. 当我添加@EnableWebMvc批注时,这变得毫无用处。 还有其他通用解决方案吗?
  @GetMapping({
            PATH_GET,
            PATH_VIEW_GET}
    ) 

在父类和子类中都被重复,因此您将面临歧义映射。 请更改一条路径。

暂无
暂无

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

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