1. 程式人生 > 實用技巧 >獲取spring web專案的所有url請求資訊

獲取spring web專案的所有url請求資訊

本文講述獲取當前專案的所有restful請求的詳細資訊,含url、介面描述、請求方式等資訊,主要用到了swagger的相關特性,最終返回的資料格式如下:

[{
    "className": "cn.miao.controller.InfoController",
    "classDesc": "InfoController",
    "methodName": "getTimeStamp",
    "methodDesc": "獲取時間戳",
    "methodURL": "/getTimeStamp",
    "requestType": "GET"
  },
...
]

具體程式碼如下:

package cn.miao.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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.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.lang.annotation.Annotation; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @RestController @Api(value = "TestControllerApi", description = "獲取url") @RequestMapping("/get/api/demo") @Slf4j public class TestController { @Autowired WebApplicationContext applicationContext; @RequestMapping(value = "/getAllUrl", method = RequestMethod.GET) public List<Map<String, String>> getAllUrl() { List<Map<String, String>> resList = new ArrayList<>(); RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class); Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods(); for (Map.Entry<RequestMappingInfo, HandlerMethod> mappingInfoHandlerMethodEntry : map.entrySet()) { Map<String, String> resultMap = new LinkedHashMap<>(); RequestMappingInfo requestMappingInfo = mappingInfoHandlerMethodEntry.getKey(); HandlerMethod handlerMethod = mappingInfoHandlerMethodEntry.getValue(); resultMap.put("className", handlerMethod.getMethod().getDeclaringClass().getName()); Annotation[] parentAnnotations = handlerMethod.getBeanType().getAnnotations(); for (Annotation annotation : parentAnnotations) { if (annotation instanceof Api) { Api api = (Api) annotation; resultMap.put("classDesc", api.value()); } else if (annotation instanceof RequestMapping) { RequestMapping requestMapping = (RequestMapping) annotation; if (null != requestMapping.value() && requestMapping.value().length > 0) { resultMap.put("classURL", requestMapping.value()[0]); } } } resultMap.put("methodName", handlerMethod.getMethod().getName()); Annotation[] annotations = handlerMethod.getMethod().getDeclaredAnnotations(); if (annotations != null) { for (Annotation annotation : annotations) { if (annotation instanceof ApiOperation) { ApiOperation methodDesc = (ApiOperation) annotation; String desc = methodDesc.value(); resultMap.put("methodDesc", desc); } } } PatternsRequestCondition p = requestMappingInfo.getPatternsCondition(); for (String url : p.getPatterns()) { resultMap.put("methodURL", url); } RequestMethodsRequestCondition methodsRequestCondition = requestMappingInfo.getMethodsCondition(); for (RequestMethod requestMethod : methodsRequestCondition.getMethods()) { resultMap.put("requestType", requestMethod.toString()); } resList.add(resultMap); } return resList; } }