获取项目中所有feign调用接口

satuo20 1年前 ⋅ 368 阅读
package com.yjzhixue.auth.feign;

import cn.hutool.core.lang.ClassScanner;
import cn.hutool.core.util.ReflectUtil;
import io.swagger.annotations.ApiOperation;
import org.lazyjee.common.dto.BaseResponse;
import org.lazyjee.common.vo.UserVo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Set;


public interface AccountService {


    static void main(String[] args) {
        Set<Class<?>> classes = ClassScanner.scanPackageByAnnotation("com.yjzhixue", FeignClient.class);
//        System.err.println(classes);
        classes.forEach(cls -> {
            // 获取所有方法
            Method[] declaredMethods = cls.getDeclaredMethods();
            FeignClient declaredAnnotation = cls.getDeclaredAnnotation(FeignClient.class);
            String url = declaredAnnotation.url();
            String name = declaredAnnotation.name();
            if(!StringUtils.hasText(name)) {
                name = declaredAnnotation.value();
            }
            for (Method declaredMethod : declaredMethods) {
                if (Modifier.isStatic(declaredMethod.getModifiers())) {
                    continue;
                }
                if (Modifier.isAbstract(declaredMethod.getModifiers())) {
                    String requestUrl = getRequestUrl(url, declaredMethod);
                    System.err.println(String.format("name=%s,requestUrl=%s", name, requestUrl));
//                    System.err.println(declaredMethod.getName());
                }
            }
        });
    }

    static String getRequestUrl(String url, Method declaredMethod) {
        PostMapping postMappingAnnotation = declaredMethod.getDeclaredAnnotation(PostMapping.class);
        if (postMappingAnnotation != null) {
            return postMappingAnnotation.value()[0];
        }
        GetMapping getMappingAnnotation = declaredMethod.getDeclaredAnnotation(GetMapping.class);
        if (getMappingAnnotation != null) {
            if (getMappingAnnotation.value().length == 0) {
                return url + "/" + declaredMethod.getName();
            }
            return getMappingAnnotation.value()[0];
        }
        RequestMapping requestMappingAnnotation = declaredMethod.getDeclaredAnnotation(RequestMapping.class);
        if (requestMappingAnnotation != null) {
            return requestMappingAnnotation.value()[0];
        }
        PutMapping putMappingAnnotation = declaredMethod.getDeclaredAnnotation(PutMapping.class);
        if (putMappingAnnotation != null) {
            return putMappingAnnotation.value()[0];
        }
        DeleteMapping deleteMappingAnnotation = declaredMethod.getDeclaredAnnotation(DeleteMapping.class);
        if (deleteMappingAnnotation != null) {
            return deleteMappingAnnotation.value()[0];
        }
        return null;
    }

}


全部评论: 0

    我有话说: