Last active
January 20, 2021 06:49
-
-
Save retanoj/d47ac0e8474dc5b3f1483efda50d4624 to your computer and use it in GitHub Desktop.
get allUrls from spring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.RequestParam; | |
import org.springframework.web.bind.annotation.RestController; | |
import org.springframework.web.context.WebApplicationContext; | |
import org.springframework.web.method.HandlerMethod; | |
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition; | |
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition; | |
import org.springframework.web.servlet.mvc.method.RequestMappingInfo; | |
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; | |
import java.util.*; | |
@RestController | |
public class UrlsController { | |
@Autowired | |
WebApplicationContext applicationContext; | |
@RequestMapping(value = "allUrls") | |
public Object allUrls() { | |
RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class); | |
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods(); | |
List<Map<String, String>> list = new ArrayList<Map<String, String>>(); | |
for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) { | |
Map<String, String> map1 = new HashMap<String, String>(); | |
RequestMappingInfo info = m.getKey(); | |
HandlerMethod method = m.getValue(); | |
PatternsRequestCondition p = info.getPatternsCondition(); | |
for (String url : p.getPatterns()) { | |
map1.put("url", url); | |
} | |
map1.put("className", method.getMethod().getDeclaringClass().getName()); // 类名 | |
map1.put("method", method.getMethod().getName()); // 方法名 | |
RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition(); | |
for (RequestMethod requestMethod : methodsCondition.getMethods()) { | |
map1.put("type", requestMethod.toString()); | |
} | |
list.add(map1); | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment