All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.cweijan.mock.feign.FeignMockerContext Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package io.github.cweijan.mock.feign;

import feign.RequestInterceptor;
import io.github.cweijan.mock.context.HttpMockContext;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author cweijan
 * @since 2020/05/22 15:36
 */
public class FeignMockerContext {

    private static final Map, Object> PROXY_CACHE = new HashMap<>();

    static {
        addInterceptor(template -> template.header("feign-flag", "true"));
    }

    public static void addInterceptor(RequestInterceptor requestInterceptor) {
        FeignBuilder.REQUEST_INTERCEPTORS.add(requestInterceptor);
    }

    /**
     * 根据目标controller的方法构造出feign接口
     *
     * @param controllerClass 目标Controlller
     * @param context
     */
    @SuppressWarnings("unchecked")
    public static  T getFeignClientWrapper(Class controllerClass, HttpMockContext context) {

        return (T) PROXY_CACHE.computeIfAbsent(controllerClass, keyClass -> {
            validateClass(controllerClass);
            Object feignClient = FeignBuilder.createFeignClient(controllerClass, context);
            return FeignBuilder.generateProxy(controllerClass, feignClient);
        });
    }

    private static  void validateClass(Class controllerClass) {

        Objects.requireNonNull(controllerClass);

        if (AnnotationUtils.findAnnotation(controllerClass, Controller.class) == null
                && controllerClass.getAnnotation(FeignClient.class) == null) {
            throw new UnsupportedOperationException("只支持创建controller代理对象!");
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy