Skip to content

Instantly share code, notes, and snippets.

@dwelch2344
Created February 24, 2015 16:55
Show Gist options
  • Save dwelch2344/7d2058250f69ce532c5c to your computer and use it in GitHub Desktop.
Save dwelch2344/7d2058250f69ce532c5c to your computer and use it in GitHub Desktop.
Still crude, but enough to get the picture...
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.*;
@RestController
@RequestMapping("/api")
public class DiscoveryController {
@Inject
private ApplicationContext ctx;
private Map<String, MappingInformation> results = new HashMap<String, MappingInformation>();
@Inject
private List<RequestMappingHandlerMapping> mappings;
@PostConstruct
protected void init(){
Map<String, RequestMappingHandlerMapping> mappings = ctx.getBeansOfType(RequestMappingHandlerMapping.class);
for(RequestMappingHandlerMapping mapping : mappings.values()){
Map<RequestMappingInfo, HandlerMethod> methods = mapping.getHandlerMethods();
for(RequestMappingInfo info : methods.keySet() ){
MappingInformation result = get(info.getPatternsCondition().getPatterns().iterator().next());
result.getMethods().addAll( info.getMethodsCondition().getMethods() );
}
}
}
private MappingInformation get(String path){
MappingInformation info = results.get(path);
if( info == null ){
info = new MappingInformation(path);
results.put(path, info);
}
return info;
};
@RequestMapping("")
public Collection<MappingInformation> get() throws Exception {
return results.values();
}
@RequiredArgsConstructor
@Getter
public static class MappingInformation{
private final String paths;
private final Set<RequestMethod> methods = new HashSet<RequestMethod>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment