简体   繁体   中英

Spring Boot RestController inheritance and ambiguous mapping issue

I'm trying to implement inherited controllers for my Spring Boot Rest project. As you see in example code snippets, I have standard endpoints for all controllers at BaseTableController, but for some of the concrete controllers I need to override methods. When I implement it like that I get Ambiguous mapping. Cannot map 'clientRfmController' method Ambiguous mapping. Cannot map 'clientRfmController' method exception on startup.

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;
    }


}

I've found a workaround by implementing custom 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. Is this correct way to handle this?
  2. When I add @EnableWebMvc annotation, this becomes useless. Is there any more generic solution?
  @GetMapping({
            PATH_GET,
            PATH_VIEW_GET}
    ) 

is being repeated in both the parent and child class so you are facing the Ambiguous mapping. please change the path for one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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