1

Requirements - In an existing project we want to introduce swagger for rest api documentation.

Problem - We cant modify each and every controller and method.

So , looking for a decorator which will automatically add the annotation to all the controller class and also which will add annotation for methods depending on the request type.

Tried with java reflections but couldnt find any option to add annotation .

public <A> A getControllerInstance(Class<A> clazz) {
    Annotation[] annotations = clazz.getAnnotations();
    // This check is used to suppress any annotation if it is not injected
    // using spring DI. Example Security.Authenticated.
    if (clazz.isAnnotationPresent(Component.class))
        return ctx.getBean(clazz);
    else
        return null;
}

public Action onRequest(Request request, Method actionMethod) {
    System.out.println("before each request..." + request.toString());
    Object[] obj = actionMethod.getAnnotations();

    return super.onRequest(request, actionMethod);
}
Manoj Salvi
  • 2,639
  • 1
  • 17
  • 21
Div
  • 99
  • 1
  • 1
  • 7
  • Possible doublicate [http://stackoverflow.com/questions/1635108/adding-java-annotations-at-runtime](http://stackoverflow.com/questions/1635108/adding-java-annotations-at-runtime) – Mikey Nov 27 '15 at 13:18
  • Thanks, using javassist i am able to add annotations. But swagger is not working. Any suggestions for implementing swagger during run time. – Div Nov 30 '15 at 10:41

1 Answers1

0

Swagger UI works based on the JSON it gets from the server. So, instead of adding swagger annotations in runtime, it would be better to give the JSON from a static file which documents all the services and usages.

kavig
  • 21
  • 3