簡體   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