java 项目启动 获取 接口信息

114 阅读1分钟

@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));
        }
    }
}

}