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