Skip to content

Instantly share code, notes, and snippets.

@marcoberri
Last active September 9, 2015 08:15
Show Gist options
  • Save marcoberri/36a4d26ed7755623efa1 to your computer and use it in GitHub Desktop.
Save marcoberri/36a4d26ed7755623efa1 to your computer and use it in GitHub Desktop.
@Controller
public class ApiDocInfo {
private final RequestMappingHandlerMapping handlerMapping;
@Autowired
public System(RequestMappingHandlerMapping handlerMapping) {
this.handlerMapping = handlerMapping;
}
@RequestMapping(value = "/apidoc", method = RequestMethod.GET)
public void getApiDoc(Model model, HttpServletResponse response) {
model.addAttribute("handlerMethods", this.handlerMapping.getHandlerMethods());
try {
response.setContentType(MediaType.TEXT_HTML_VALUE);
StringBuffer sb = new StringBuffer();
sb.append("<html>");
sb.append("<body>");
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : this.handlerMapping.getHandlerMethods().entrySet()) {
sb.append("<div><hr>");
sb.append("<p>");
if (entry.getValue().getMethod()!= null) {
sb.append("<strong>" + entry.getValue().getMethod().getName() + "</strong>");
}
sb.append("</p>");
sb.append("</div>");
sb.append("<p>");
sb.append("<span>Path:</span><br>");
if (entry.getKey().getPatternsCondition().getPatterns() != null) {
sb.append("<strong>" +entry.getKey().getPatternsCondition().getPatterns() + "</strong>");
}
sb.append("</p>");
sb.append("</div>");
sb.append("<p>");
sb.append("<span>Method:</span>");
if (entry.getKey().getMethodsCondition().getMethods() != null) {
sb.append(entry.getKey().getMethodsCondition().getMethods());
}
sb.append("</p>");
sb.append("</div>");
sb.append("<p>");
sb.append("<span>Parameter annotation:</span><br>");
if (entry.getValue().getMethod().getParameterAnnotations()!= null) {
for(Annotation a[] : entry.getValue().getMethod().getParameterAnnotations()){
for(Annotation b : a){
if(!b.annotationType().isAnnotation())
continue;
if( b.annotationType().getSimpleName().equals("PathVariable"))
continue;
sb.append(b.annotationType().getSimpleName() + "<br/>");
}
}
}
sb.append("</p>");
sb.append("</div>");
sb.append("<p>");
sb.append("<span>Return Class:</span>");
if (entry.getValue().getMethod().getReturnType()!= null) {
sb.append(entry.getValue().getMethod().getReturnType());
}
sb.append("</p>");
sb.append("</div>");
sb.append("<p>");
sb.append("<span>Complete method:</span><br>");
sb.append(entry.getValue());
sb.append("</p>");
sb.append("</div>");
sb.append("<p>");
sb.append("<span>Params:</span><br>");
if (entry.getKey().getParamsCondition() != null) {
sb.append(entry.getKey().getParamsCondition());
}
sb.append("</p>");
sb.append("</div>");
}
sb.append("</body>");
sb.append("</html>");
response.getWriter().print(sb);
} catch (final IOException e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment