java 项目启动时获取所有接口信息

@Component
public class GetAllControllerConfig implements ApplicationContextAware {

@Resource
private RedisTemplate redisTemplate;

@SneakyThrows
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

// Map<String, Object> beansWithAnn = applicationContext.getBeansWithAnnotation(RestController.class);
// // 遍历每个controller
// for(Map.Entry<String,Object> entry:beansWithAnn.entrySet()){
// Object value = entry.getValue();
// Class<?> classValue =AopUtils.getTargetClass(value);
// // 获取所有的方法
// List methodList = Arrays.asList(classValue.getMethods());
// System.out.println(“方法:”+methodList);
//
// }
RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();

    for(Map.Entry mappingInfoHadlerMethod:map.entrySet()){
        SysInterfaceDTO dto = new SysInterfaceDTO();
        List<SysInterfaceDTO.Field> fields = new ArrayList<>();
        // 获取url 及 请求类型
        RequestMappingInfo info = (RequestMappingInfo)mappingInfoHadlerMethod.getKey();
        Set<String> urlSet = info.getPatternsCondition().getPatterns();
        Set<RequestMethod> methods = info.getMethodsCondition().getMethods();
        dto.setUrl(urlSet.toString());
        dto.setMethod(methods.toString());
        // 获取接口信息
        HandlerMethod method =(HandlerMethod) mappingInfoHadlerMethod.getValue();
        // 类名
        String className = method.getMethod().getDeclaringClass().getName();
        Annotation[] annotations = method.getBeanType().getAnnotations();
        for(Annotation annotation :annotations){
            if(annotation instanceof Api){
                // 获取api tags
                Api api = (Api) annotation;
                String[] tags = api.tags();
                dto.setApiTags(tags[0]);
            }
           
        }
        // 方法名
        String methodName = method.getMethod().getName();
        Annotation[] annotationsMethod = method.getMethod().getDeclaredAnnotations();
        for(Annotation annotation :annotationsMethod){
            // 获取apioperation value
            if(annotation instanceof ApiOperation){
                ApiOperation api = (ApiOperation) annotation;
                // 描述
                String desc = api.value();
                dto.setApiOpertion(desc);
            }
            if(annotation instanceof ApiImplicitParam){
                // 参数描述
                ApiImplicitParam api = (ApiImplicitParam)annotation;
                // 字段类型
                String value =api.dataType();
                // 字段 描述
                String desc= api.value();
                // 字段值
                String name = api.name();

                SysInterfaceDTO.Field field = new SysInterfaceDTO.Field();
                field.setFieldValue(value);
                field.setFieldDesc(desc);
                field.setFieldName(name);
                fields.add(field);
            }
            if(annotation instanceof ApiImplicitParams){
                ApiImplicitParams api = (ApiImplicitParams)annotation;
                ApiImplicitParam[] values = api.value();
                for(ApiImplicitParam value:values){
                    // 字段描述
                    String desc = value.value();
                    // 字段类型
                    String type = value.dataType();
                    // 字段名
                    String name = value.name();
                    SysInterfaceDTO.Field field = new SysInterfaceDTO.Field();
                    field.setFieldValue(type);
                    field.setFieldDesc(desc);
                    field.setFieldName(name);
                    fields.add(field);
                }
            }

            // 参数
            if(CollectionUtils.isNotEmpty(fields)){
                dto.setFieldList(fields);
            }
            else {
                // 字段实体对象
                Class<?>[] parameterTypes = method.getMethod().getParameterTypes();
                for(Class<?> type :parameterTypes){
                    if("[POST]".equals(methods.toString())){
                        Class clazz = Class.forName(type.getName());
                        Field[] declaredFields = clazz.getDeclaredFields();
                        for(Field field :declaredFields){
                            String name = field.getName();
                            if("serialVersionUID".equals(name)==false){
                                Annotation annotation1 = field.getAnnotation(ApiModelProperty.class);
                                if(annotation1 !=null){
                                    ApiModelProperty api = (ApiModelProperty)annotation1;
                                    String value = api.value();
                                    SysInterfaceDTO.Field field2 = new SysInterfaceDTO.Field();
                                    field2.setFieldName(name);
                                    field2.setFieldDesc(value);
                                    field2.setFieldValue(api.dataType());
                                    fields.add(field2);
                                }
                            }
                        }
                        if(CollectionUtils.isNotEmpty(fields)){
                            dto.setFieldList(fields);
                        }
                    }
                }
            }
             //将数据添加进redis 中 MAP_SYS_INTERFACE 以路径为key
            redisTemplate.opsForHash().put(RedisHashEnum.MAP_SYS_INTERFACE.getValue(),dto.getUrl(), JSONObject.toJSON(dto));
        }
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值