美文网首页
Java Web程序统计接口数量

Java Web程序统计接口数量

作者: BySjm | 来源:发表于2022-07-27 13:54 被阅读0次

    步骤:

    1. 把RestModel和ApiController类复制到自己的代码中
    2. 访问接口/api/getAllApi
    3. 把返回的json个数据保存成Json文件,通过网站转成CSV文件

    关于乱码:保存的Json可以用idea打开,设置成UTF-8格式,如果转成的csv文件还是乱码,可以用IDEA打开CSV文件,设置UTF-8格式,再次用Excel打开CSV文件;或者选择左下角Data一次复制一列

    RestModel类

    import lombok.AllArgsConstructor;
    import lombok.Builder;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public class RestModel {
        /**
         * 接口 path
         */
        private String path;
    
    
        /**
         * 对应的 controller 类
         */
        private String clazz;
    
    
        /**
         * 对应的 controller 类中的方法名
         */
        private String method;
    
    
        /**
         * http 方法
         */
        private String httpMethod;
    
    
        /**
         * http 方法名字
         */
        private String methodName
    }
    

    ApiController类

    import com.didichuxing.erp.cloud.bid.basic.model.RestModel;
    import com.didichuxing.erp.common.response.ResultModel;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.beans.factory.BeanFactoryUtils;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.core.annotation.AnnotationUtils;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    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.List;
    import java.util.Map;
    import java.util.Objects;
    import java.util.Set;
    
    @RestController
    @RequestMapping("/api")
    @Api(value = "获取所有接口 API")
    public class ApiController {
    
        @Value("${server.port}")
        private String serverPort;
    
        @Value("${server.servlet.contextPath}")
        private String serverContextPath;
    
        /**
         * TODO  该接口做权限控制
         *
         * @return
         */
        @GetMapping("/getAllApi")
        @ApiOperation("获取系统所有接口信息")
        public ResponseEntity<ResultModel> getAllApi() {
            String urlPrefix = ":" + serverPort + serverContextPath;
            List<RestModel> restModels = new ArrayList<>();
    
            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) {
                    Map<RequestMappingInfo, HandlerMethod> handlerMethods =
                            ((RequestMappingHandlerMapping) handlerMapping).getHandlerMethods();
    
                    for (RequestMappingInfo rmi : handlerMethods.keySet()) {
                        PatternsRequestCondition prc = rmi.getPatternsCondition();
                        Set<String> patterns = prc.getPatterns();
                        HandlerMethod handlerMethod = handlerMethods.get(rmi);
                        for (String path : patterns) {
                            path = urlPrefix + path;
                            Class<?> clazz = handlerMethod.getBeanType();
                            Method method = handlerMethod.getMethod();
                            ApiOperation controlAnnotation = this.getControlAnnotation(handlerMethod);
                            RestModel restModel = RestModel.builder()
                                    .path(path)
                                    .clazz(clazz.toString())
                                    .method(method.toString())
                                    .methodName(Objects.isNull(controlAnnotation) ? "" : controlAnnotation.value())
                                    .httpMethod(rmi.getMethodsCondition().toString()).build();
                            restModels.add(restModel);
                        }
                    }
                }
            }
    
            return ResponseEntity.ok(ResultModel.ok(restModels));
        }
    
        private ApiOperation getControlAnnotation(HandlerMethod method) {
            if (method.getMethodAnnotation(ApiOperation.class) != null) {
                return (ApiOperation) method.getMethodAnnotation(ApiOperation.class);
            } else {
                return AnnotationUtils.findAnnotation(method.getBean().getClass(), ApiOperation.class) != null
                        ? (ApiOperation) AnnotationUtils.findAnnotation(method.getBean().getClass(),
                        ApiOperation.class) : null;
            }
        }
    
    }
    

    相关文章

      网友评论

          本文标题:Java Web程序统计接口数量

          本文链接:https://www.haomeiwen.com/subject/lqdzirtx.html