jeecgboot启动时日志打印所有接口,作为开发时的参考作用吧。

发布时间 2023-04-27 18:21:23作者: 令狐hero

主要的方式是使用了RequestMappingHandlerMapping这个bean当中保存了所有的映射、对应的controller类、方法等等的信息。在单体启动类中取出这个bean然后遍历就可以了,代码如下:

/**
* 单体启动类(采用此类启动为单体模式)
*/
@Slf4j
@SpringBootApplication
public class JeecgSystemApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(JeecgSystemApplication.class);
    }

    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(JeecgSystemApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = oConvertUtils.getString(env.getProperty("server.servlet.context-path"));
        log.info("\n----------------------------------------------------------\n\t" +
                "Application Jeecg-Boot is running! Access URLs:\n\t" +
                "Local: \t\thttp://localhost:" + port + path + "/\n\t" +
                "External: \thttp://" + ip + ":" + port + path + "/\n\t" +
                "Swagger文档: \thttp://" + ip + ":" + port + path + "/doc.html\n" +
                "----------------------------------------------------------");
        //获取所有的接口,打印出来,作为参考
        RequestMappingHandlerMapping requestMappingHandlerMapping = application.getBean(RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
        for(RequestMappingInfo requestMappingInfo : handlerMethods.keySet()){
            Set<String> setPatterns = requestMappingInfo.getPatternsCondition().getPatterns();
            Set<RequestMethod> setMethods = requestMappingInfo.getMethodsCondition().getMethods();
            String pattern = "";
            if(setPatterns.iterator().hasNext()){
                pattern = setPatterns.iterator().next();
            }
            String method = "";
            if(setMethods.iterator().hasNext()){
                method = setMethods.iterator().next().toString();
            }
            HandlerMethod handlerMethod = handlerMethods.get(requestMappingInfo);
            log.info("----- " + getApiName(handlerMethod.getBeanType()) + ":" + getOperationName(handlerMethod.getMethod()));
            log.info("----- key:" + method + " " + pattern);
            log.info("----- url:" + "http://" + ip + ":" + port + path + pattern);
            log.info("********************************************************************");
        }

    }

    private static String getApiName(Class<?> clazz) {
        String name = clazz.getName();
        Api api = clazz.getAnnotation(Api.class);
        if (api != null && StringUtils.isNotBlank(api.value())) {
            name = api.value();
        }
        if (api != null && api.tags().length > 0) {
            name = String.join(",",api.tags());
        }
        return name;
    }

    private static String getOperationName(Method method) {
        String name = method.getName();
        ApiOperation operation = method.getAnnotation(ApiOperation.class);
        if (operation != null && StringUtils.isNotBlank(operation.value())) {
            name = operation.value();
        }
        return name;
    }

}