Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.ifengxue.http.contract.HttpOperations Maven / Gradle / Ivy
/*
* Copyright 2019 https://www.ifengxue.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ifengxue.http.contract;
import com.ifengxue.http.annotation.BodyType;
import com.ifengxue.http.annotation.HttpMethod;
import com.ifengxue.http.annotation.ResponseType;
import java.lang.reflect.Type;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* http operations,通过该接口定义的方法可以很容易的发起http请求
*/
public interface HttpOperations {
// get
/**
* 自定义执行get请求并获取json
编码的实体
*
* @param url 请求地址。如请求地址为https://example.com/foo/bar
,则url应为/foo/bar
* @param type 返回实体类型
* @param httpHeaders 请求header
* @param uriVariables 查询参数
* @return 由type
指定的返回值
*/
default T getForJsonEntity(@Nonnull String url, @Nonnull Type type, @Nullable Map httpHeaders,
@Nullable Map uriVariables) {
return exchange(url, HttpMethod.GET, BodyType.X_WWW_FORM_URLENCODED, ResponseType.APPLICATION_JSON, type,
httpHeaders, uriVariables, null);
}
/**
* 自定义执行get请求并获取实体
*
* @param url 请求地址。如请求地址为https://example.com/foo/bar
,则url应为/foo/bar
* @param bodyType 请求body类型
* @param responseType response body类型
* @param type 返回实体类型
* @param httpHeaders 请求header
* @param uriVariables 查询参数
* @return 由type
指定的返回值
*/
default T getForEntity(@Nonnull String url, @Nonnull BodyType bodyType, @Nonnull ResponseType responseType,
@Nonnull Type type, @Nullable Map httpHeaders,
@Nullable Map uriVariables) {
return exchange(url, HttpMethod.GET, bodyType, responseType, type, httpHeaders, uriVariables, null);
}
/**
* 自定义执行get请求并获取json
编码的实体
*
* @param url 请求地址。如请求地址为https://example.com/foo/bar
,则url应为/foo/bar
* @param responseEntityClass 返回实体类
* @param httpHeaders 请求header
* @param uriVariables 查询参数
* @return 由type
指定的返回值
*/
default T getForJsonEntity(@Nonnull String url, @Nonnull Class responseEntityClass,
@Nullable Map httpHeaders,
@Nullable Map uriVariables) {
return exchange(url, HttpMethod.GET, BodyType.X_WWW_FORM_URLENCODED, ResponseType.APPLICATION_JSON,
responseEntityClass,
httpHeaders, uriVariables, null);
}
/**
* 自定义执行get请求并获取实体
*
* @param url 请求地址。如请求地址为https://example.com/foo/bar
,则url应为/foo/bar
* @param bodyType 请求body类型
* @param responseType response body类型
* @param responseEntityClass 返回实体类
* @param httpHeaders 请求header
* @param uriVariables 查询参数
* @return 由responseEntityClass
指定的返回值
*/
default T getForEntity(@Nonnull String url, @Nonnull BodyType bodyType, @Nonnull ResponseType responseType,
@Nonnull Class responseEntityClass, @Nullable Map httpHeaders,
@Nullable Map uriVariables) {
return exchange(url, HttpMethod.GET, bodyType, responseType, (Type) responseEntityClass, httpHeaders, uriVariables,
null);
}
/**
* 自定义执行get请求并获取实体
*
* @param url 请求地址。如请求地址为https://example.com/foo/bar
,则url应为/foo/bar
* @param bodyType 请求body类型
* @param responseType response body类型
* @param typeReference 返回实体类型
* @param httpHeaders 请求header
* @param uriVariables 查询参数
* @return 由typeReference
指定的返回值
*/
default T getForEntity(@Nonnull String url, @Nonnull BodyType bodyType, @Nonnull ResponseType responseType,
@Nonnull TypeReference typeReference, @Nullable Map httpHeaders,
@Nullable Map uriVariables) {
return exchange(url, HttpMethod.GET, bodyType, responseType, typeReference.getType(), httpHeaders, uriVariables,
null);
}
// post
/**
* 自定义执行post请求并获取json
实体
*
* @param url 请求地址。如请求地址为https://example.com/foo/bar
,则url应为/foo/bar
* @param type 返回实体类型
* @param httpHeaders 请求header
* @param uriVariables 查询参数
* @return 由type
指定的返回值
*/
default T postForJsonEntity(@Nonnull String url, @Nonnull Type type, @Nullable Map httpHeaders,
@Nullable Map uriVariables, @Nullable Object requestBody) {
return exchange(url, HttpMethod.POST, BodyType.APPLICATION_JSON, ResponseType.APPLICATION_JSON, type, httpHeaders,
uriVariables, requestBody);
}
/**
* 自定义执行post请求并获取实体
*
* @param url 请求地址。如请求地址为https://example.com/foo/bar
,则url应为/foo/bar
* @param bodyType 请求body类型
* @param responseType response body类型
* @param type 返回实体类型
* @param httpHeaders 请求header
* @param uriVariables 查询参数
* @return 由type
指定的返回值
*/
default T postForEntity(@Nonnull String url, @Nonnull BodyType bodyType, @Nonnull ResponseType responseType,
@Nonnull Type type, @Nullable Map httpHeaders,
@Nullable Map uriVariables, @Nullable Object requestBody) {
return exchange(url, HttpMethod.POST, bodyType, responseType, type, httpHeaders, uriVariables, requestBody);
}
/**
* 自定义执行post请求并获取json
实体
*
* @param url 请求地址。如请求地址为https://example.com/foo/bar
,则url应为/foo/bar
* @param responseEntityClass 返回实体类
* @param httpHeaders 请求header
* @param uriVariables 查询参数
* @return 由type
指定的返回值
*/
default T postForJsonEntity(@Nonnull String url, @Nonnull Class responseEntityClass,
@Nullable Map httpHeaders,
@Nullable Map uriVariables, @Nullable Object requestBody) {
return exchange(url, HttpMethod.POST, BodyType.APPLICATION_JSON, ResponseType.APPLICATION_JSON, responseEntityClass,
httpHeaders, uriVariables, requestBody);
}
/**
* 自定义执行post请求并获取实体
*
* @param url 请求地址。如请求地址为https://example.com/foo/bar
,则url应为/foo/bar
* @param bodyType 请求body类型
* @param responseType response body类型
* @param responseEntityClass 返回实体类
* @param httpHeaders 请求header
* @param uriVariables 查询参数
* @return 由responseEntityClass
指定的返回值
*/
default T postForEntity(@Nonnull String url, @Nonnull BodyType bodyType, @Nonnull ResponseType responseType,
@Nonnull Class responseEntityClass, @Nullable Map httpHeaders,
@Nullable Map uriVariables, @Nullable Object requestBody) {
return exchange(url, HttpMethod.POST, bodyType, responseType, (Type) responseEntityClass, httpHeaders, uriVariables,
requestBody);
}
/**
* 自定义执行post请求并获取实体
*
* @param url 请求地址。如请求地址为https://example.com/foo/bar
,则url应为/foo/bar
* @param bodyType 请求body类型
* @param responseType response body类型
* @param typeReference 返回实体类型
* @param httpHeaders 请求header
* @param uriVariables 查询参数
* @return 由typeReference
指定的返回值
*/
default T postForEntity(@Nonnull String url, @Nonnull BodyType bodyType, @Nonnull ResponseType responseType,
@Nonnull TypeReference typeReference, @Nullable Map httpHeaders,
@Nullable Map uriVariables, @Nullable Object requestBody) {
return exchange(url, HttpMethod.POST, bodyType, responseType, typeReference.getType(), httpHeaders, uriVariables,
requestBody);
}
// exchange
/**
* 自定义执行http请求
*
* @param url 请求地址。如请求地址为https://example.com/foo/bar
,则url应为/foo/bar
* @param method 请求方法。如GET, POST
等
* @param bodyType 请求body类型
* @param responseType response body类型
* @param responseEntityClass 返回实体类
* @param httpHeaders 请求header
* @param uriVariables 查询参数
* @param requestBody 请求body
* @return 由responseEntityClass
指定的返回值
*/
default T exchange(@Nonnull String url, @Nonnull HttpMethod method, @Nonnull BodyType bodyType,
@Nonnull ResponseType responseType, @Nonnull Class responseEntityClass,
@Nullable Map httpHeaders, @Nullable Map uriVariables,
@Nullable Object requestBody) {
return exchange(url, method, bodyType, responseType, (Type) responseEntityClass,
httpHeaders, uriVariables, requestBody);
}
/**
* 自定义执行http请求
*
* @param url 请求地址。如请求地址为https://example.com/foo/bar
,则url应为/foo/bar
* @param method 请求方法。如GET, POST
等
* @param bodyType 请求body类型
* @param responseType response body类型
* @param typeReference 返回实体类型
* @param httpHeaders 请求header
* @param uriVariables 查询参数
* @param requestBody 请求body
* @return 由typeReference
指定的返回值
*/
default T exchange(@Nonnull String url, @Nonnull HttpMethod method, @Nonnull BodyType bodyType,
@Nonnull ResponseType responseType, @Nonnull TypeReference typeReference,
@Nullable Map httpHeaders, @Nullable Map uriVariables,
@Nullable Object requestBody) {
return exchange(url, method, bodyType, responseType, typeReference.getType(), httpHeaders, uriVariables,
requestBody);
}
/**
* 自定义执行http请求
*
* @param url 请求地址。如请求地址为https://example.com/foo/bar
,则url应为/foo/bar
* @param method 请求方法。如GET, POST
等
* @param bodyType 请求body类型
* @param responseType response body类型
* @param responseEntityType 返回实体类型 {@link TypeReference}
* @param httpHeaders 请求header
* @param uriVariables 查询参数
* @param requestBody 请求body
* @return 由responseEntityType
指定的返回值
*/
T exchange(@Nonnull String url, @Nonnull HttpMethod method, @Nonnull BodyType bodyType,
@Nonnull ResponseType responseType, @Nonnull Type responseEntityType, @Nullable Map httpHeaders,
@Nullable Map uriVariables, @Nullable Object requestBody);
}