Spring自定义HTTP请求注解

本文详细介绍了如何在Spring中自定义HTTP请求注解,包括URL、发送、启动注解的定义,解码器和请求处理器的实现,以及通过RestTemplate调用第三方接口的测试。
摘要由CSDN通过智能技术生成

1、Http请求URL注解定义

@Retention(RetentionPolicy.RUNTIME)
@Target({
   ElementType.TYPE})
public @interface HttpUrl {
   
    String prefixUrl();
}

2、Http请求发送注解定义

@Retention(RetentionPolicy.RUNTIME)
@Target({
   ElementType.METHOD})
public @interface HttpRequest {
   
    HttpMethod method() default HttpMethod.POST;

    String value();
}

3、Http注解启动注解定义

@Retention(RetentionPolicy.RUNTIME)
@Target({
   ElementType.TYPE})
@Documented
@Import({
   HttpRequestRegistrar.class})
public @interface EnableHttpRequest {
   
    String[] value() default {
   };

    String[] basePackages() default {
   };

    Class<?>[] defaultConfiguration() default {
   };
}

4、解码器定义

public interface Decoder {
   
    Object decode(ResponseEntity responseEntity, Type type) throws Exception;
}
public class DistanceDecoder implements Decoder{
   

    private ObjectMapper objectMapper = new ObjectMapper();

    /**
     *数据解码
     * @param response
     * @param type
     * @return
     * @throws Exception
     */
    @Override
    public Object decode(ResponseEntity response, Type type) throws Exception {
   
        Object body = response.getBody();
        String value = null;
        if (body instanceof String) {
   
            value = (String)body;
        }

        JavaType javaType = this.getJavaType(type, null);
        return this.objectMapper.readValue(value, javaType);
    }

    protected JavaType getJavaType(Type type, Class<?> contextClass) {
   
        return contextClass != null ? this.objectMapper.getTypeFactory().constructType(type, contextClass) : this.objectMapper.constructType(type);
    }
}

5、请求代理处理器

@Slf4j
public class DistanceHandler implements EnvironmentAware {
   

    @Autowired(required = true)
    private HttpRequestHandler httpRequestHandler;

    private Environment environment;

    public Object handle(Object proxy,Method method, Object[] args) {
   
        RestTemplate restTemplate = RestTemplateSingleton.getInstance();
        HttpRequest request = method.getAnnotation(HttpRequest.class);
        String value = request.value();
        HttpMethod httpMethod = request.method();
        Type returnType = method.getGenericReturnType();
        Class<?> declaringClass = method.getDeclaringClass();
        Annotation[][] annotations = method.getParameterAnnotations();
        HttpUrl httpConnection = declaringClass.getAnnotation(HttpUrl.class);
        String prefixUrl = httpConnection.prefixUrl();
        //解析占位符
        prefixUrl = this.resolve(prefixUrl);
        String requestUrl = prefixUrl + (prefixUrl.endsWith("/") ? "" : "/") + value;
        String name = httpMethod.name();
        if ("POST".equals(name)) {
   
            return this.httpRequestHandler.doPost(restTemplate, annotations, args, httpMethod, requestUrl, returnType,method);
        } else {
   
            return "GET".equals(name) ? this.httpRequestHandler.doGet(restTemplate, annotations, args, httpMethod, requestUrl, returnType,method) : null;
        }
    }

    public HttpRequestHandler getHttpRequestHandler() {
   
        return this.httpRequestHandler;
    }

    public void setHttpRequestHandler(HttpRequestHandler httpRequestHandler) {
   
        this.httpRequestHandler = httpRequestHandler;
    }

    @Override
    public void setEnvironment(Environment environment) {
   
        this.environment = environment;
    }

    /**
     * 占位符解析
     */
    private String resolve(String value) {
   
        if (StringUtils.hasText(value)) {
   
            return this.environment.resolvePlaceholders(value);
        }
        return value;
    }
}

6、请求处理器

@Slf4j
public class HttpRequestHandler {
   

    @Autowired
    private Decoder decoder;

    private static final String HTTP_FORM = "application/x-www-form-urlencoded";

    private 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值