diff --git a/src/main/java/aiyh/utils/httpUtil/ResponeVo.java b/src/main/java/aiyh/utils/httpUtil/ResponeVo.java index 6f07cbb..dcc34b3 100644 --- a/src/main/java/aiyh/utils/httpUtil/ResponeVo.java +++ b/src/main/java/aiyh/utils/httpUtil/ResponeVo.java @@ -27,6 +27,8 @@ public class ResponeVo implements HttpResponse { * 相应内容 */ private String entityString; + + private Map entityMap; /** * 相应头信息 */ @@ -39,7 +41,6 @@ public class ResponeVo implements HttpResponse { private Map requestData; - private HttpResponse response; public int getCode() { @@ -68,11 +69,15 @@ public class ResponeVo implements HttpResponse { * @return 资源皇后的map集合 * @throws JsonProcessingException JSON转换异常 */ + @Deprecated public Map getEntityMap() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(this.getEntityString(), Map.class); } + public Map getResponseMap(){ + return this.entityMap; + } /** * 根据相应结果,转化为实体类 * @@ -81,11 +86,21 @@ public class ResponeVo implements HttpResponse { * @return 转换后的实体类 * @throws JsonProcessingException JSON转换异常 */ + @Deprecated public T getEntity(Class clazz) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(this.getEntityString(), clazz); } + public T getResponseEntity(Class clazz) { + ObjectMapper mapper = new ObjectMapper(); + try { + return mapper.readValue(this.getEntityString(), clazz); + } catch (JsonProcessingException e) { + return null; + } + } + /** * 根据相应结果,转化为实体类 * @@ -98,6 +113,45 @@ public class ResponeVo implements HttpResponse { return mapper.readValue(this.getEntityString(), typeReference); } + public String getEntityString() { + return entityString; + } + + public void setEntityString(String entityString) { + this.entityString = entityString; + try{ + ObjectMapper mapper = new ObjectMapper(); + this.entityMap = mapper.readValue(this.getEntityString(), Map.class); + }catch (Exception ignore){ + + } + } + + public InputStream getContent() { + return content; + } + + public void setContent(InputStream content) { + this.content = content; + } + + public byte[] getContentByteArr() { + return contentByteArr; + } + + public void setContentByteArr(byte[] contentByteArr) { + this.contentByteArr = contentByteArr; + } + + @Override + public String toString() { + return "ResponeVo{" + + "code=" + code + + ", entityString='" + entityString + '\'' + + ", otherParam=" + requestData + + '}'; + } + /** * 根据相应结果转化为实体集合 * @@ -243,37 +297,6 @@ public class ResponeVo implements HttpResponse { } - public String getEntityString() { - return entityString; - } - public void setEntityString(String entityString) { - this.entityString = entityString; - } - - public InputStream getContent() { - return content; - } - - public void setContent(InputStream content) { - this.content = content; - } - - public byte[] getContentByteArr() { - return contentByteArr; - } - - public void setContentByteArr(byte[] contentByteArr) { - this.contentByteArr = contentByteArr; - } - - @Override - public String toString() { - return "ResponeVo{" + - "code=" + code + - ", entityString='" + entityString + '\'' + - ", otherParam=" + requestData + - '}'; - } } diff --git a/src/main/java/aiyh/utils/httpUtil/util/HttpUtilParamInfo.java b/src/main/java/aiyh/utils/httpUtil/util/HttpUtilParamInfo.java index 01790ed..7e32e34 100644 --- a/src/main/java/aiyh/utils/httpUtil/util/HttpUtilParamInfo.java +++ b/src/main/java/aiyh/utils/httpUtil/util/HttpUtilParamInfo.java @@ -5,6 +5,7 @@ import com.alibaba.fastjson.annotation.JSONField; import lombok.Data; import java.util.Date; +import java.util.Map; /** *

http请求信息

@@ -27,5 +28,7 @@ public class HttpUtilParamInfo { @JSONField(format = "yyyy-MM-dd HH:mm:ss") private Date responseDate; + private Map responseMap; + private String responseString; private ResponeVo response; } diff --git a/src/main/java/aiyh/utils/httpUtil/util/HttpUtils.java b/src/main/java/aiyh/utils/httpUtil/util/HttpUtils.java index 6ea581f..44665b8 100644 --- a/src/main/java/aiyh/utils/httpUtil/util/HttpUtils.java +++ b/src/main/java/aiyh/utils/httpUtil/util/HttpUtils.java @@ -8,9 +8,9 @@ import aiyh.utils.zwl.common.ToolUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; +import com.alibaba.fastjson.support.spring.PropertyPreFilters; import com.google.common.base.Strings; import com.google.common.util.concurrent.ThreadFactoryBuilder; -import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.CredentialsProvider; @@ -57,6 +57,8 @@ public class HttpUtils { * basic 认证 */ private CredentialsProvider credentialsProvider = null; + private final PropertyPreFilters filters = new PropertyPreFilters(); + private final PropertyPreFilters.MySimplePropertyPreFilter excludefilter = filters.addFilter(); { // private final ExecutorService executorService = Executors.newFixedThreadPool(3); @@ -67,6 +69,8 @@ public class HttpUtils { new LinkedBlockingQueue<>(1024), threadFactory, new ThreadPoolExecutor.AbortPolicy()); + String[] excludeProperties = {"locale","contentByteArr","response"}; + excludefilter.addExcludes(excludeProperties); } public HttpUtils() { @@ -638,10 +642,16 @@ public class HttpUtils { httpUtilParamInfo = new HttpUtilParamInfo(); } httpUtilParamInfo.setResponse(apply); + if(apply.getResponseMap() == null){ + httpUtilParamInfo.setResponseString(apply.getEntityString()); + }else { + httpUtilParamInfo.setResponseMap(apply.getResponseMap()); + } httpUtilParamInfo.setResponseDate(new Date()); try { log.info(Util.logStr("url [{}] request info : [\n{}\n];", httpUtilParamInfo.getUrl(), JSONObject.toJSONString(httpUtilParamInfo, + excludefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteDateUseDateFormat))); } catch (Exception ignore) { @@ -669,11 +679,17 @@ public class HttpUtils { httpUtilParamInfo = new HttpUtilParamInfo(); } httpUtilParamInfo.setResponse(apply); + if(apply.getResponseMap() == null){ + httpUtilParamInfo.setResponseString(apply.getEntityString()); + }else { + httpUtilParamInfo.setResponseMap(apply.getResponseMap()); + } httpUtilParamInfo.setResponseDate(new Date()); HTTP_UTIL_PARAM_INFO_THREAD_LOCAL.remove(); try { log.info(Util.logStr("url [{}] request info : [\n{}\n];", httpUtilParamInfo.getUrl(), JSONObject.toJSONString(httpUtilParamInfo, + excludefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteDateUseDateFormat))); } catch (Exception ignore) { @@ -696,17 +712,22 @@ public class HttpUtils { try { response = httpClient.execute(request); HttpEntity entity = response.getEntity(); - Header[] allHeaders = response.getAllHeaders(); Locale locale = response.getLocale(); responeVo.setLocale(locale); responeVo.setEntityString(EntityUtils.toString(entity, DEFAULT_ENCODING)); responeVo.setCode(response.getStatusLine().getStatusCode()); responeVo.setResponse(response); httpUtilParamInfo.setResponse(responeVo); + if(responeVo.getResponseMap() == null){ + httpUtilParamInfo.setResponseString(responeVo.getEntityString()); + }else { + httpUtilParamInfo.setResponseMap(responeVo.getResponseMap()); + } httpUtilParamInfo.setResponseDate(new Date()); try { log.info(Util.logStr("url [{}] request info : [\n{}\n];", httpUtilParamInfo.getUrl(), JSONObject.toJSONString(httpUtilParamInfo, + excludefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteDateUseDateFormat))); } catch (Exception ignore) { @@ -718,6 +739,7 @@ public class HttpUtils { httpUtilParamInfo.setResponseDate(new Date()); log.info(Util.logStr("url [{}] request info : [\n{}\n];", httpUtilParamInfo.getUrl(), JSONObject.toJSONString(httpUtilParamInfo, + excludefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteDateUseDateFormat))); } catch (Exception ignore) { @@ -1021,9 +1043,11 @@ public class HttpUtils { private HttpPost uploadFileByInputStream(String url, List multipartFileList, Map params, Map headers) { log.info(Util.logStr("start request : url is [{}],other param [\n{}\n],heard [\n{}\n]", url, JSONObject.toJSONString(params, + excludefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteDateUseDateFormat), JSONObject.toJSONString(headers, + excludefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteDateUseDateFormat))); HttpUtilParamInfo httpUtilParamInfo = new HttpUtilParamInfo(); @@ -1074,9 +1098,11 @@ public class HttpUtils { private HttpPut uploadFileByInputStreamPut(String url, List multipartFileList, Map params, Map headers) { log.info(Util.logStr("start request : url is [{}],other param [\n{}\n],heard [\n{}\n]", url, JSONObject.toJSONString(params, + excludefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteDateUseDateFormat), JSONObject.toJSONString(headers, + excludefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteDateUseDateFormat))); HttpUtilParamInfo httpUtilParamInfo = new HttpUtilParamInfo();