mercoledì 9 settembre 2015

Spring MVC 4 - elenco chiamate REST

Ottimo controller REST per visualizzare l'elenco delle chiamate disponibili nel proprio MVC (tipo il file wsdl)
@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) {
}
}
}
view raw ApiDocInfo.java hosted with ❤ by GitHub

Nessun commento: