Skip to content

Instantly share code, notes, and snippets.

@mjgp2
Created December 15, 2017 13:43
Show Gist options
  • Save mjgp2/c826b078f97dd1957a3a1b2a1d8b08ee to your computer and use it in GitHub Desktop.
Save mjgp2/c826b078f97dd1957a3a1b2a1d8b08ee to your computer and use it in GitHub Desktop.
Dump api docs from spring annotations
package com.importio.webcache.web.api;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ValueConstants;
public class Foo {
public static void main(String[] args) {
ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
TreeMap<String, Map<String, Object>> map = new TreeMap<>();
for ( Class<?> class1 : new Class<?>[]{ MyController.class }) {
for ( Method m: class1.getMethods() ) {
RequestMapping a = m.getAnnotation(RequestMapping.class);
String[] parameterNames = parameterNameDiscoverer.getParameterNames(m);
if ( a == null ) continue;
String method = a.method().length == 0 ? "GET" : a.method()[0].toString();
String path = a.value()[0].replaceAll(":.*?(/|$)", "}$1");
if ( a.params() != null && a.params().length > 0 ) {
List<String> list = Arrays.asList(a.params());
Collections.sort(list);
path += "?" + String.join(",", list);
}
if ( ! map.containsKey(path) ) map.put(path, new TreeMap<>());
StringBuilder doc = new StringBuilder("Parameters:\n\n");
for ( int i=0; i<m.getParameterCount(); i++) {
Parameter parameter = m.getParameters()[i];
if ( parameter.isAnnotationPresent(PathVariable.class) ) {
doc.append(String.format("* Path - _%s_ - [description] - %s \n", parameterNames[i], parameter.getType().getSimpleName()));
} else if ( parameter.isAnnotationPresent(RequestParam.class) ) {
RequestParam r = parameter.getAnnotation(RequestParam.class);
doc.append(String.format("* Query - _%s_ - [description] - %s (%s%s)\n", parameterNames[i], parameter.getType().getSimpleName(), r.required() ? "required" : "optional", r.defaultValue().equals(ValueConstants.DEFAULT_NONE) ? "" : ", default="+r.defaultValue()));
} else if ( parameter.isAnnotationPresent(RequestBody.class) ) {
RequestBody r = parameter.getAnnotation(RequestBody.class);
doc.append(String.format("* Body - _%s_ - [description] - %s (%s)\n", parameterNames[i], parameter.getType().getSimpleName(), r.required() ? "required" : "optional"));
}
}
map.get(path).put(method, doc.toString());
}
}
map.forEach((path,methodMap) -> {
System.out.println("==== "+path+"\n");
methodMap.forEach((method,string) -> {
System.out.println("===== "+method+"\n");
System.out.println(string);
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment