UriTemplateParser { UriTemplate parseMethod(Method method, String basePath) { UriBuilder uriBuilder = UriBuilder.fromPath(basePath); Path controllerPath = AnnotationUtils.getAnnotation(method.getDeclaringClass(), Path.class); if (controllerPath != null) { uriBuilder.path(controllerPath.value()); } Path methodPath = AnnotationUtils.getAnnotation(method, Path.class); if (methodPath != null) { uriBuilder.path(methodPath.value()); } UriTemplate.Builder uriTemplateBuilder = UriTemplate.fromTemplate(uriBuilder.toTemplate()); List<AnnotatedElement> annotatedElements = BeanUtils.getFieldsAndAccessors(method.getDeclaringClass()); Arrays.asList(method.getParameters()).forEach(param -> { if (param.isAnnotationPresent(BeanParam.class)) { annotatedElements.addAll(BeanUtils.getFieldsAndAccessors(param.getType())); } else { annotatedElements.add(param); } }); annotatedElements.forEach(accessibleObject -> { if (accessibleObject.isAnnotationPresent(QueryParam.class)) { uriTemplateBuilder.queryParam(accessibleObject.getAnnotation(QueryParam.class).value()); } if (accessibleObject.isAnnotationPresent(MatrixParam.class)) { uriTemplateBuilder.matrixParam(accessibleObject.getAnnotation(MatrixParam.class).value()); } }); return uriTemplateBuilder.build(); }  @PostConstruct void init();  }
@Test public void shouldParseMethods() throws NoSuchMethodException { assertThat(uriTemplateParser.parseMethod(UriBuilderTestControllers.SomeController.class.getMethod("root"), BASE_PATH), equalTo(UriTemplate.fromTemplate("/base-path/some").build())); assertThat(uriTemplateParser.parseMethod(UriBuilderTestControllers.SomeController.class.getMethod("path"), BASE_PATH), equalTo(UriTemplate.fromTemplate("/base-path/some/path").build())); }
@Test public void shouldParseFromInheritedMethods() throws NoSuchMethodException { assertThat(uriTemplateParser.parseMethod(UriBuilderTestControllers.SomeController.class.getMethod("parent"), BASE_PATH), equalTo(UriTemplate.fromTemplate("/base-path/some/parent").build())); assertThat(uriTemplateParser.parseMethod(UriBuilderTestControllers.ControllerImplementation.class.getMethod("show"), BASE_PATH), equalTo(UriTemplate.fromTemplate("/base-path/implementation/show").build())); }
@Test public void shouldParseMethodParams() throws NoSuchMethodException { assertThat(uriTemplateParser.parseMethod( UriBuilderTestControllers.ParamsController.class.getMethod("pathParams", String.class, long.class), BASE_PATH), equalTo(UriTemplate.fromTemplate("/base-path/params/path/{p1}/{p2}").build())); assertThat(uriTemplateParser.parseMethod( UriBuilderTestControllers.ParamsController.class.getMethod("queryParams", String.class, long.class), BASE_PATH), equalTo(UriTemplate.fromTemplate("/base-path/params/query").queryParam("q1").queryParam("q2").build())); assertThat(uriTemplateParser.parseMethod( UriBuilderTestControllers.ParamsController.class.getMethod("matrixParams", String.class, long.class), BASE_PATH), equalTo(UriTemplate.fromTemplate("/base-path/params/matrix").matrixParam("m1").matrixParam("m2").build())); }
@Test public void shouldParseFields() throws NoSuchMethodException { assertThat(uriTemplateParser.parseMethod(UriBuilderTestControllers.FieldsController.class.getMethod("root"), BASE_PATH), equalTo(UriTemplate.fromTemplate("/base-path/fields/{p1}/{p2}/{p3}") .matrixParam("m1").matrixParam("m2").matrixParam("m3") .queryParam("q1").queryParam("q2").queryParam("q3").build())); }
@Test public void shouldParseBeanParams() throws NoSuchMethodException { assertThat(uriTemplateParser.parseMethod( UriBuilderTestControllers.BeanParamController.class.getMethod("bean", UriBuilderTestControllers.BeanParams.class), BASE_PATH), equalTo(UriTemplate.fromTemplate("/base-path/bean/{p}").matrixParam("m").queryParam("q").build())); }