1. 程式人生 > >獲取SpringMVC所有的rest接口及其對應函數信息

獲取SpringMVC所有的rest接口及其對應函數信息

gfs response key sys out mvc size ref action

package com.geostar.gfstack.operationcenter.core.cloud.action;

import com.geostar.gfstack.operationcenter.core.cloud.model.RestModel;
import com.google.gson.Gson;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition; import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; @Controller @Component("urlAction") @RequestMapping("urlAction") public class UrlAction { @ResponseBody @RequestMapping("index") public void index() { List<RestModel> list = new ArrayList<RestModel>(); Map<String, RestModel> map = new HashMap<String, RestModel>(); List<String> urlList = new ArrayList<String>(); WebApplicationContext wac = (WebApplicationContext) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE); Map<String, HandlerMapping> requestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(wac, HandlerMapping.class, true, false); for (HandlerMapping handlerMapping : requestMappings.values()) { if (handlerMapping instanceof RequestMappingHandlerMapping) { RequestMappingHandlerMapping rmhm = (RequestMappingHandlerMapping) handlerMapping; Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods(); for (RequestMappingInfo rmi : handlerMethods.keySet()) { PatternsRequestCondition prc = rmi.getPatternsCondition(); Set<String> patterns = prc.getPatterns(); HandlerMethod handlerMethod = handlerMethods.get(rmi); for (String url : patterns) { Class<?> clazz = handlerMethod.getBeanType(); Method method = handlerMethod.getMethod(); RestModel restModel = new RestModel(); restModel.setUrl(url); restModel.setClazz(clazz.toString()); restModel.setMethod(method.toString()); map.put(url, restModel); urlList.add(url); } } } } String[] urls = new String[urlList.size()]; urls = urlList.toArray(urls); Arrays.sort(urls); for (String url : urls) { list.add(map.get(url)); } Gson gson = new Gson(); System.out.println(gson.toJson(list)); } }

獲取SpringMVC所有的rest接口及其對應函數信息