mirror of
https://gitee.com/egzosn/pay-java-parent.git
synced 2026-05-10 23:47:44 +08:00
请求单独增加请求头对象
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
package com.egzosn.pay.common.bean;
|
||||
|
||||
/**
|
||||
* 授权页面的授权类型
|
||||
* @author Actinia
|
||||
* @email hayesfu@qq.com
|
||||
* @date 2018/1/18
|
||||
*/
|
||||
public enum AuthPageType {
|
||||
//注册,登录
|
||||
registration,login;
|
||||
}
|
||||
@@ -154,9 +154,18 @@ public class ClientHttpRequest<T> extends HttpEntityEnclosingRequestBase impleme
|
||||
if (null == request){
|
||||
return this;
|
||||
}
|
||||
if (request instanceof HttpStringEntity){
|
||||
if (request instanceof HttpHeader){
|
||||
HttpHeader entity = (HttpHeader)request;
|
||||
if (null != entity.getHeaders() ){
|
||||
for (Header header : entity.getHeaders()){
|
||||
addHeader(header);
|
||||
}
|
||||
}
|
||||
}else if (request instanceof HttpStringEntity){
|
||||
HttpStringEntity entity = (HttpStringEntity)request;
|
||||
setEntity(entity);
|
||||
if (!entity.isEmpty()){
|
||||
setEntity(entity);
|
||||
}
|
||||
if (null != entity.getHeaders() ){
|
||||
for (Header header : entity.getHeaders()){
|
||||
addHeader(header);
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.egzosn.pay.common.http;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.egzosn.pay.common.http.UriVariables.getMapToParameters;
|
||||
|
||||
/**
|
||||
* 请求头
|
||||
*
|
||||
* @author egan
|
||||
* <pre>
|
||||
* email egzosn@gmail.com
|
||||
* date 2018/01/30
|
||||
* </pre>
|
||||
*/
|
||||
public class HttpHeader{
|
||||
/**
|
||||
* 请求头
|
||||
*/
|
||||
private List<Header> headers;
|
||||
|
||||
public HttpHeader() {
|
||||
}
|
||||
|
||||
public HttpHeader(List<Header> headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求头
|
||||
*
|
||||
* @param header 请求头
|
||||
*/
|
||||
public HttpHeader(Header header) {
|
||||
addHeader(header);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求头集
|
||||
*
|
||||
* @return 请求头集
|
||||
*/
|
||||
public List<Header> getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加请求头
|
||||
*
|
||||
* @param header 请求头
|
||||
*/
|
||||
public void addHeader(Header header) {
|
||||
if (null == this.headers) {
|
||||
this.headers = new ArrayList<>();
|
||||
}
|
||||
this.headers.add(header);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置请求头集
|
||||
*
|
||||
* @param headers 请求头集
|
||||
*/
|
||||
public void setHeaders(List<Header> headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置请求头集
|
||||
*
|
||||
* @param headers 请求头集
|
||||
*/
|
||||
public void setHeaders(Map<String, String> headers) {
|
||||
for (String key : headers.keySet()) {
|
||||
addHeader(new BasicHeader(key, headers.get(key)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -175,19 +175,18 @@ public class HttpRequestTemplate {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* get 请求
|
||||
* @param uri 请求地址
|
||||
*
|
||||
* @param uri 请求地址
|
||||
* @param responseType 响应类型
|
||||
* @param uriVariables 用于匹配表达式
|
||||
* @param <T> 响应类型
|
||||
* @return 类型对象
|
||||
* @param <T> 响应类型
|
||||
*
|
||||
* @return 类型对象
|
||||
* <p>
|
||||
* <code>
|
||||
* getForObject("http://egan.in/pay/{id}/f/{type}", String.class, "1", "APP")
|
||||
* getForObject("http://egan.in/pay/{id}/f/{type}", String.class, "1", "APP")
|
||||
* </code>
|
||||
*/
|
||||
public <T> T getForObject(String uri, Class<T> responseType, Object... uriVariables){
|
||||
@@ -218,6 +217,48 @@ public class HttpRequestTemplate {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get 请求
|
||||
* @param uri 请求地址
|
||||
* @param header 请求头
|
||||
* @param responseType 响应类型
|
||||
* @param uriVariables 用于匹配表达式
|
||||
* @param <T> 响应类型
|
||||
* @return 类型对象
|
||||
*
|
||||
* <code>
|
||||
* getForObject("http://egan.in/pay/{id}/f/{type}", String.class, "1", "APP")
|
||||
* </code>
|
||||
*/
|
||||
public <T> T getForObject(String uri, HttpHeader header,Class<T> responseType, Object... uriVariables){
|
||||
|
||||
return doExecute(URI.create(UriVariables.getUri(uri, uriVariables)), header, responseType, MethodType.GET);
|
||||
}
|
||||
|
||||
/**
|
||||
* get 请求
|
||||
*
|
||||
* @param uri 请求地址
|
||||
* @param header 请求头
|
||||
* @param responseType 响应类型
|
||||
* @param uriVariables 用于匹配表达式
|
||||
* @param <T> 响应类型
|
||||
* @return 类型对象
|
||||
* <code>
|
||||
* Map<String, String> uriVariables = new HashMap<String, String>();<br>
|
||||
*
|
||||
* uriVariables.put("id", "1");<br>
|
||||
*
|
||||
* uriVariables.put("type", "APP");<br>
|
||||
*
|
||||
* getForObject("http://egan.in/pay/{id}/f/{type}", String.class, uriVariables)<br>
|
||||
* </code>
|
||||
*/
|
||||
public <T> T getForObject(String uri, HttpHeader header, Class<T> responseType, Map<String, ?> uriVariables){
|
||||
return doExecute(URI.create(UriVariables.getUri(uri, uriVariables)), header, responseType, MethodType.GET);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* http 请求执行
|
||||
* @param uri 地址
|
||||
|
||||
@@ -26,6 +26,33 @@ public class HttpStringEntity extends StringEntity {
|
||||
* 请求头
|
||||
*/
|
||||
private List<Header> headers;
|
||||
/**
|
||||
* 是否为空的请求实体
|
||||
*/
|
||||
private boolean isEmpty = false;
|
||||
|
||||
|
||||
public boolean isEmpty() {
|
||||
return isEmpty;
|
||||
}
|
||||
|
||||
public void setEmpty(boolean empty) {
|
||||
isEmpty = empty;
|
||||
}
|
||||
|
||||
|
||||
public void requestIsEmpty(Map<String, Object> request) {
|
||||
if (null == request || request.isEmpty()){
|
||||
this.isEmpty = true;
|
||||
}
|
||||
}
|
||||
public void requestIsEmpty(String request) {
|
||||
if (null == request || request.isEmpty()){
|
||||
this.isEmpty = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
@@ -37,6 +64,7 @@ public class HttpStringEntity extends StringEntity {
|
||||
*/
|
||||
public HttpStringEntity(Map<String, Object> request, Header... headers) throws UnsupportedEncodingException {
|
||||
this(getMapToParameters(request), headers);
|
||||
requestIsEmpty(request);
|
||||
|
||||
}
|
||||
|
||||
@@ -50,6 +78,7 @@ public class HttpStringEntity extends StringEntity {
|
||||
*/
|
||||
public HttpStringEntity(Map<String, Object> request, Map<String, String> headers) throws UnsupportedEncodingException {
|
||||
this(getMapToParameters(request), headers);
|
||||
requestIsEmpty(request);
|
||||
|
||||
}
|
||||
|
||||
@@ -61,6 +90,7 @@ public class HttpStringEntity extends StringEntity {
|
||||
*/
|
||||
public HttpStringEntity(Map<String, Object> request, ContentType contentType) {
|
||||
super(getMapToParameters(request), contentType);
|
||||
requestIsEmpty(request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,6 +101,7 @@ public class HttpStringEntity extends StringEntity {
|
||||
*/
|
||||
public HttpStringEntity(Map<String, Object> request, String charset) {
|
||||
super(getMapToParameters(request), charset);
|
||||
requestIsEmpty(request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,6 +112,7 @@ public class HttpStringEntity extends StringEntity {
|
||||
*/
|
||||
public HttpStringEntity(Map<String, Object> request, Charset charset) {
|
||||
super(getMapToParameters(request), charset);
|
||||
requestIsEmpty(request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,6 +124,7 @@ public class HttpStringEntity extends StringEntity {
|
||||
*/
|
||||
public HttpStringEntity(Map<String, Object> request) throws UnsupportedEncodingException {
|
||||
super(getMapToParameters(request));
|
||||
requestIsEmpty(request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,6 +137,7 @@ public class HttpStringEntity extends StringEntity {
|
||||
*/
|
||||
public HttpStringEntity(String request, ContentType contentType) throws UnsupportedCharsetException {
|
||||
super(request, contentType);
|
||||
requestIsEmpty(request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,6 +150,7 @@ public class HttpStringEntity extends StringEntity {
|
||||
*/
|
||||
public HttpStringEntity(String request, String charset) throws UnsupportedCharsetException {
|
||||
super(request, charset);
|
||||
requestIsEmpty(request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,6 +161,7 @@ public class HttpStringEntity extends StringEntity {
|
||||
*/
|
||||
public HttpStringEntity(String request, Charset charset) {
|
||||
super(request, charset);
|
||||
requestIsEmpty(request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,6 +174,7 @@ public class HttpStringEntity extends StringEntity {
|
||||
*/
|
||||
public HttpStringEntity(String request, Header... headers) throws UnsupportedEncodingException {
|
||||
super(request);
|
||||
requestIsEmpty(request);
|
||||
if (null == headers) {
|
||||
this.headers = Arrays.asList(headers);
|
||||
}
|
||||
@@ -153,6 +190,7 @@ public class HttpStringEntity extends StringEntity {
|
||||
*/
|
||||
public HttpStringEntity(String request, Map<String, String> headers) throws UnsupportedEncodingException {
|
||||
super(request);
|
||||
requestIsEmpty(request);
|
||||
this.headers = new ArrayList<>();
|
||||
for (String key : headers.keySet()) {
|
||||
this.headers.add(new BasicHeader(key, headers.get(key)));
|
||||
|
||||
Reference in New Issue
Block a user