這個代碼大部分是我抄的別的地方的,我試了下不打印就本身改了下。很少說了,下面是代碼。java
改動的地方有:web
用json返回;spring
WebApplicationContext appContext注入;json
原代碼網址就不發了。下面代碼是能夠正常輸出的。mvc
import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerMapping; 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 com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; @Controller @RequestMapping("/seminar") public class ShowAllMappingInfoController { @Autowired WebApplicationContext appContext; @RequestMapping(value = "/showAllMappingInfo", method = RequestMethod.GET) @ResponseBody public JSONArray index(HttpServletRequest request) { //請求url和處理方法的映射 JSONArray requestToMethodItemJSONArray = new JSONArray(); //獲取全部的RequestMapping Map<String, HandlerMapping> allRequestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext, HandlerMapping.class, true, false); for (HandlerMapping handlerMapping : allRequestMappings.values()) { //本項目只須要RequestMappingHandlerMapping中的URL映射 if (handlerMapping instanceof RequestMappingHandlerMapping) { RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) handlerMapping; Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods(); for (Map.Entry<RequestMappingInfo, HandlerMethod> requestMappingInfoHandlerMethodEntry : handlerMethods.entrySet()) { RequestMappingInfo requestMappingInfo = requestMappingInfoHandlerMethodEntry.getKey(); HandlerMethod mappingInfoValue = requestMappingInfoHandlerMethodEntry.getValue(); RequestMethodsRequestCondition methodCondition = requestMappingInfo.getMethodsCondition(); PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition(); String requestUrl = patternsCondition.getPatterns().toString(); String controllerName = mappingInfoValue.getBeanType().toString(); String requestMethodName = mappingInfoValue.getMethod().getName(); Class<?>[] methodParamTypes = mappingInfoValue.getMethod().getParameterTypes(); JSONObject jsonObject = new JSONObject(); jsonObject.put("requestUrl", requestUrl); jsonObject.put("controllerName", controllerName); jsonObject.put("requestMethodName", requestMethodName); requestToMethodItemJSONArray.add(jsonObject); } break; } } return requestToMethodItemJSONArray; } }