ClassNameAwareRequestMappingHandlerMapping extends RequestMappingHandlerMapping { @Override protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { RequestMappingInfo info = createRequestMappingInfo(method); if (info != null) { RequestMappingInfo typeInfo = createRequestMappingInfo(handlerType); if (typeInfo != null) { info = typeInfo.combine(info); } if (shouldAddClassNameContext(handlerType)) { RequestMappingInfo prefixMappingInfo = prefixMappingInfo(handlerType); info = prefixMappingInfo.combine(info); } } return info; } ClassNameAwareRequestMappingHandlerMapping(StarterMvcProperties.ApiProperties apiProps);   }
@Test public void shouldNotAddAnyPrefixIfNoConfigProvidedForApiController() throws Exception { mappingHandler(apiProps); RequestMappingInfo info = handlerMapping.getMappingForMethod(ListEndpointApiController.class.getMethod("getList"), ListEndpointApiController.class); assertPattern(info, "/list-endpoint-api-controller/getList"); }
@Test @Parameters({"api", "/api", "api/", "/api/"}) public void addApiPathPrefixWhenSetUpForApiController(String apiPath) throws Exception { apiProps.setPath(apiPath); mappingHandler(apiProps); RequestMappingInfo info = handlerMapping.getMappingForMethod(ListEndpointApiController.class.getMethod("getList"), ListEndpointApiController.class); assertPattern(info, "/api/list-endpoint-api-controller/getList"); }
@Test @Parameters({"v1", "/v1", "v1/", "/v1/"}) public void addPrefixWhenDefaultPrefixSetUpForApiController(String defaultPrefix) throws Exception { apiProps.getResources().setDefaultPrefix(defaultPrefix); mappingHandler(apiProps); RequestMappingInfo info = handlerMapping.getMappingForMethod(ListEndpointApiController.class.getMethod("getList"), ListEndpointApiController.class); assertPattern(info, "/v1/list-endpoint-api-controller/getList"); }
@Test @Parameters({ "List?Controller, /endpoint-api/getList", "ListController, /list-endpoint-api-controller/getList", "List?Controller?, /endpoint-api/getList", "?Controller, /list-endpoint-api/getList", "List?, /endpoint-api-controller/getList", "SomeList?, /list-endpoint-api-controller/getList", "List?ControllerEnd, /list-endpoint-api-controller/getList", "?, /list-endpoint-api-controller/getList"}) public void shouldHandlePrefixAndSuffixWhenPatternProvided(String pattern, String expectedUrl) throws Exception { apiProps.getResources().setClassNamePattern(pattern); mappingHandler(apiProps); RequestMappingInfo info = handlerMapping.getMappingForMethod(ListEndpointApiController.class.getMethod("getList"), ListEndpointApiController.class); assertPattern(info, expectedUrl); }
@Test public void shouldIntegrateEveryOptionForApiController() throws Exception { apiProps.setPath("/api"); apiProps.getResources().setDefaultPrefix("v1"); apiProps.getResources().setClassNamePattern("List?Controller"); mappingHandler(apiProps); RequestMappingInfo info = handlerMapping.getMappingForMethod(ListEndpointApiController.class.getMethod("getList"), ListEndpointApiController.class); assertPattern(info, "/api/v1/endpoint-api/getList"); }
@Test public void shouldNotProcessApiOptionsForRestController() throws Exception { apiProps.setPath("/api"); apiProps.getResources().setDefaultPrefix("v1"); apiProps.getResources().setClassNamePattern("List?Controller"); mappingHandler(apiProps); RequestMappingInfo info = handlerMapping.getMappingForMethod(ListRestController.class.getMethod("getList"), ListRestController.class); assertPattern(info, "/getList"); }
@Test public void shouldFormatName() throws Exception { apiProps.getResources().setClassNamePattern("List?Controller"); apiProps.getResources().setCaseFormat(CaseFormat.LOWER_CAMEL); mappingHandler(apiProps); RequestMappingInfo info = handlerMapping.getMappingForMethod(ListEndpointApiController.class.getMethod("getList"), ListEndpointApiController.class); assertPattern(info, "/endpointApi/getList"); }
@Test public void shouldUseExplicitPrefixForApiControler() throws Exception { apiProps.getResources().setDefaultPrefix("v1"); apiProps.getResources().setClassNamePattern("?Controller"); mappingHandler(apiProps); RequestMappingInfo info = handlerMapping.getMappingForMethod(PrefixedApiController.class.getMethod("get"), PrefixedApiController.class); assertPattern(info, "/v2/prefixed-api/get"); }
@Test public void shouldAddRequestMappingValueAfterApiControllerPrefixes() throws Exception { apiProps.getResources().setDefaultPrefix("v1"); apiProps.getResources().setClassNamePattern("?Controller"); mappingHandler(apiProps); RequestMappingInfo info = handlerMapping.getMappingForMethod(ClassMappingController.class.getMethod("get"), ClassMappingController.class); assertPattern(info, "/v1/class-mapping/hi/get"); }