-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
List Controller methods from parent class #225
Comments
Example fix: @Override
public Set<Method> jsondocMethods(final Class<?> controller) {
// NEWLY ADDED - START
if (controller == null) {
return new LinkedHashSet<>();
}
// NEWLY ADDED - END
final Set<Method> annotatedMethods = new LinkedHashSet<>();
for (final Method method : controller.getDeclaredMethods()) {
if (method.isAnnotationPresent(RequestMapping.class)) {
annotatedMethods.add(method);
System.out.println(controller.getSimpleName() + " " + method);
}
}
// NEWLY ADDED - START
annotatedMethods.addAll(jsondocMethods(controller.getSuperclass()));
// NEWLY ADDED - END
return annotatedMethods;
} EDIT: See below for additional information. |
@ST-DDT Since controller methods are public, it would actually be much simpler and achieve the same purpose if we used |
@joffrey-bion AFAIK Spring does not require Controller methods to be public (although they should be IMO) thus this approach is necessary |
Unfortunately my solution/the parent scanning shows some other issues, because the It will be displayed as This can be fixed by from public abstract ApiMethodDoc initApiMethodDoc(Method method, Map<Class<?>, JSONDocTemplate> jsondocTemplates); to public abstract ApiMethodDoc initApiMethodDoc(Class<?> controller, Method method, Map<Class<?>, JSONDocTemplate> jsondocTemplates);
from public static Set<String> buildPath(Method method) {
Class<?> controller = method.getDeclaringClass(); to public static Set<String> buildPath(Class<?> controller, Method method) { PS: @joffrey-bion AFAICT your impl is likely to be also affected from this |
@ST-DDT Thanks for pointing it out. I'll double check Spring's implementation to see how paths are actually read. Or maybe you know already? Suppose we have this situation: @Controller
@RequestMapping("/parent")
public class ParentController {
@RequestMapping("/parentMethod")
public void parentMethod() { }
}
@Controller
@RequestMapping("/child")
public class ChildController extends ParentController {
@RequestMapping("/childMethod")
public void childMethod() { }
} What API does Spring generate? I would say:
But is the parent controller type-annotation really ignored? |
Usually the parent would neither be annotated with For clarification: // @Controller
// @RequestMapping("/parent")
public class ParentController {
@RequestMapping("/parentMethod")
public void parentMethod() { }
}
@Controller
@RequestMapping("/child")
public class ChildController extends ParentController {
@RequestMapping("/childMethod")
public void childMethod() { }
} Will provide the following request urls:
@Controller
@RequestMapping("/parent")
public class ParentController {
@RequestMapping("/parentMethod")
public void parentMethod() { }
}
@Controller
@RequestMapping("/child")
public class ChildController extends ParentController {
@RequestMapping("/childMethod")
public void childMethod() { }
} Will provide the following request urls:
// @Controller
// @RequestMapping("/parent")
public class ParentController {
@RequestMapping("/parentMethod")
public void parentMethod() { }
}
@Controller
@RequestMapping("/child")
public class ChildController extends ParentController {
@Override
@RequestMapping("/childMethod")
public void parentMethod() { }
} Will provide the following request urls:
package a;
// @Controller
// @RequestMapping("/parent")
public class ParentController {
@RequestMapping("/parentMethod")
void parentMethod() { }
}
package b;
@Controller
@RequestMapping("/child")
public class ChildController extends ParentController {
@RequestMapping("/childMethod")
void parentMethod() { }
} Will provide the following request urls:
RequestMappings are resolved in This produces an public class DummyMapper extends RequestMappingHandlerMapping {
@Override
public RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
return super.getMappingForMethod(method, handlerType);
}
public static void main(String[] args) throws NoSuchMethodException, SecurityException {
Class<?> controller = MyController.class;
Method method = controller.getMethod("list");
System.out.println(new DummyMapper().getMappingForMethod(method, controller).getPatternsCondition().getPatterns());
}
} On the other hand you might be able to read all registered methods from:
But this would only work if the server is running. Maybe this article helps: https://stackoverflow.com/questions/11082818/spring-mvc-get-all-request-mappings |
@joffrey-bion Does this help you? Maybe we should move the discussion over to your fork. |
@ST-DDT Yes, thanks a lot for the detailed reply. |
Currently only the methods present in the topmost controller class are detected by jsonDoc.
However in some applications there is a lot of repetitive logic in the controllers so I moved them to a shared parent class. However now they will no longer be detected by jsonDoc. it would be nice if methods from parent classes will be detected by jsonDoc as well.
This can be fixed by adding a parent class check/recursion to this method:
AbstractSpringJSONDocScanner#jsondocMethods()
The text was updated successfully, but these errors were encountered: