Guest User

Untitled

a guest
Dec 18th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. package com.importio.webcache.web.api;
  2.  
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.Parameter;
  5. import java.util.Arrays;
  6. import java.util.Collections;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.TreeMap;
  10.  
  11. import org.springframework.core.DefaultParameterNameDiscoverer;
  12. import org.springframework.core.ParameterNameDiscoverer;
  13. import org.springframework.web.bind.annotation.PathVariable;
  14. import org.springframework.web.bind.annotation.RequestBody;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RequestParam;
  17. import org.springframework.web.bind.annotation.ValueConstants;
  18. public class Foo {
  19.  
  20. public static void main(String[] args) {
  21. ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
  22. TreeMap<String, Map<String, Object>> map = new TreeMap<>();
  23. for ( Class<?> class1 : new Class<?>[]{ MyController.class }) {
  24. for ( Method m: class1.getMethods() ) {
  25. RequestMapping a = m.getAnnotation(RequestMapping.class);
  26. String[] parameterNames = parameterNameDiscoverer.getParameterNames(m);
  27. if ( a == null ) continue;
  28. String method = a.method().length == 0 ? "GET" : a.method()[0].toString();
  29. String path = a.value()[0].replaceAll(":.*?(/|$)", "}$1");
  30. if ( a.params() != null && a.params().length > 0 ) {
  31. List<String> list = Arrays.asList(a.params());
  32. Collections.sort(list);
  33. path += "?" + String.join(",", list);
  34. }
  35. if ( ! map.containsKey(path) ) map.put(path, new TreeMap<>());
  36. StringBuilder doc = new StringBuilder("Parameters:\n\n");
  37. for ( int i=0; i<m.getParameterCount(); i++) {
  38. Parameter parameter = m.getParameters()[i];
  39. if ( parameter.isAnnotationPresent(PathVariable.class) ) {
  40. doc.append(String.format("* Path - _%s_ - [description] - %s \n", parameterNames[i], parameter.getType().getSimpleName()));
  41. } else if ( parameter.isAnnotationPresent(RequestParam.class) ) {
  42. RequestParam r = parameter.getAnnotation(RequestParam.class);
  43. 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()));
  44. } else if ( parameter.isAnnotationPresent(RequestBody.class) ) {
  45. RequestBody r = parameter.getAnnotation(RequestBody.class);
  46. doc.append(String.format("* Body - _%s_ - [description] - %s (%s)\n", parameterNames[i], parameter.getType().getSimpleName(), r.required() ? "required" : "optional"));
  47. }
  48. }
  49. map.get(path).put(method, doc.toString());
  50. }
  51. }
  52.  
  53. map.forEach((path,methodMap) -> {
  54. System.out.println("==== "+path+"\n");
  55. methodMap.forEach((method,string) -> {
  56. System.out.println("===== "+method+"\n");
  57. System.out.println(string);
  58. });
  59. });
  60. }
  61. }
Add Comment
Please, Sign In to add comment