Commit 874578cf authored by duanruiming's avatar duanruiming

删除无用工具类目录

parent afdca7dd
package net.wanji.web.exception; package net.wanji.web.common.exception;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
......
package net.wanji.web.util; package net.wanji.web.common.util;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -186,7 +186,7 @@ public class IdWorker { ...@@ -186,7 +186,7 @@ public class IdWorker {
public static void main(String[] args) { public static void main(String[] args) {
IdWorker idWorker = new IdWorker(0, 0); IdWorker idWorker = new IdWorker();
for (int i = 0; i < 10000; i++) { for (int i = 0; i < 10000; i++) {
long nextId = idWorker.nextId(); long nextId = idWorker.nextId();
......
...@@ -2,7 +2,7 @@ package net.wanji.web.handler; ...@@ -2,7 +2,7 @@ package net.wanji.web.handler;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.wanji.feign.pojo.result.JsonViewObject; import net.wanji.feign.pojo.result.JsonViewObject;
import net.wanji.web.exception.CrossException; import net.wanji.web.common.exception.CrossException;
import org.springframework.validation.FieldError; import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
......
...@@ -3,6 +3,7 @@ package net.wanji.web.service; ...@@ -3,6 +3,7 @@ package net.wanji.web.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.wanji.feign.pojo.result.JsonViewObject; import net.wanji.feign.pojo.result.JsonViewObject;
import net.wanji.web.common.enums.CrossTypeEnum; import net.wanji.web.common.enums.CrossTypeEnum;
import net.wanji.web.common.util.IdWorker;
import net.wanji.web.entity.TBaseAreaInfo; import net.wanji.web.entity.TBaseAreaInfo;
import net.wanji.web.entity.TBaseCrossInfo; import net.wanji.web.entity.TBaseCrossInfo;
import net.wanji.web.mapper.TBaseAreaInfoMapper; import net.wanji.web.mapper.TBaseAreaInfoMapper;
...@@ -10,7 +11,6 @@ import net.wanji.web.mapper.TBaseCrossInfoMapper; ...@@ -10,7 +11,6 @@ import net.wanji.web.mapper.TBaseCrossInfoMapper;
import net.wanji.web.po.AreaTreePO; import net.wanji.web.po.AreaTreePO;
import net.wanji.web.po.CrossInfoPO; import net.wanji.web.po.CrossInfoPO;
import net.wanji.web.po.PageResultPO; import net.wanji.web.po.PageResultPO;
import net.wanji.web.util.IdWorker;
import net.wanji.web.vo.BaseCrossInfoVO; import net.wanji.web.vo.BaseCrossInfoVO;
import net.wanji.web.vo.CrossInfoVO; import net.wanji.web.vo.CrossInfoVO;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
......
package net.wanji.web.util;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
@Component
public class HttpRestUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpRestUtil.class);
@Autowired
private RestTemplate restTemplate;
public static Map<String, String> buildHeader() {
Map<String, String> header = new HashMap<>();
header.put("Content-Type", "application/json; charset=UTF-8");
header.put("Accept", MediaType.APPLICATION_JSON.toString());
return header;
}
/**
* @param url 请求URL
* @param param 请求参数
* @param classType 转换对象
* @return T 返回结果
* @description: 发送GET请求,支持转换类型,应用多变性返回值,classType返回值类型
*/
public <T> T doGet(String url, Map<String, String> headers, String param, Class<T> classType) {
long startTime = System.currentTimeMillis();
LOGGER.info("\n 请求地址 = {} || 请求方式 = {} || 请求头 = {} || 请求参数 = {}",
url, "GET", JSON.toJSONString(headers), JSON.toJSONString(param));
//封装httpEntity
HttpEntity httpEntity = getHttpEntity(headers, param);
//发送请求
ResponseEntity<T> responseEntity;
try {
responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, classType);
} catch (ResourceAccessException e) {
LOGGER.error("HttpRestUtil -> doExecute 捕获IOException异常,{}", e.getMessage());
try {
Thread.sleep(1000);
} catch (Exception ex) {
LOGGER.error("HttpRestUtil -> doExecute 暂停2S异常!", ex);
}
responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, classType);
}
//响应状态值 200 201 202
boolean statusBool = false;
if (HttpStatus.OK.equals(responseEntity.getStatusCode()) || HttpStatus.CREATED.equals(responseEntity.getStatusCode()) || HttpStatus.ACCEPTED.equals(responseEntity.getStatusCode())) {
statusBool = true;
}
if (null != responseEntity && statusBool) {
T result = responseEntity.getBody();
LOGGER.info("请求地址 = {} || 请求参数 = {} || 响应时长 = {} || 响应结果 = {}",
url, JSON.toJSONString(param), System.currentTimeMillis() - startTime, JSON.toJSONString(result));
if (StringUtils.isEmpty(result)) {
return null;
}
return result;
} else {
LOGGER.error("请求地址 = {} || 请求参数 = {} || 响应时长 = {} || 响应结果 = {}",
url, JSON.toJSONString(param), System.currentTimeMillis() - startTime, JSON.toJSONString(responseEntity.getBody()));
// throw new RuntimeException("调用接口 = " + url + " || " +
// "失败状态 = " + responseEntity.getStatusCode() + " || " +
// "响应 = " + JSON.toJSONString(responseEntity.getBody()));
throw new HttpClientErrorException(responseEntity.getStatusCode(),
"调用接口 = " + url + " || " +
"失败状态 = " + responseEntity.getStatusCode() + " || " +
"响应 = " + responseEntity.getBody());
}
}
/**
* @param url 请求URL
* @param headers 请求头
* @param param 请求参数
* @param classType 转换类型
* @return T 返回结果
* @description: 发送POST请求,支持转换类型,应用多变性返回值,classType返回值类型
*/
public <T> T doPost(String url, Map<String, String> headers, String param, Class<T> classType) {
Map<String, String> header = Objects.isNull(headers) ? buildHeader() : headers;
long startTime = System.currentTimeMillis();
LOGGER.info("\n 请求地址 = {} || 请求方式 = {} || 请求头 = {} || 请求参数 = {}",
url, "POST", JSON.toJSONString(header), JSON.toJSONString(param));
//封装httpEntity
HttpEntity httpEntity = getHttpEntity(header, param);
ResponseEntity<T> responseEntity;
try {
responseEntity = restTemplate.postForEntity(url, httpEntity, classType);
} catch (ResourceAccessException e) {
LOGGER.error("HttpRestUtil -> doPost 捕获IOException异常,{}" + e.getMessage());
try {
Thread.sleep(2000);
} catch (Exception ex) {
LOGGER.error("HttpRestUtil -> doExecute 暂停2S异常!");
}
responseEntity = restTemplate.postForEntity(url, httpEntity, classType);
}
//响应状态值 200 201 202
boolean statusBool = false;
if (HttpStatus.OK.equals(responseEntity.getStatusCode()) || HttpStatus.CREATED.equals(responseEntity.getStatusCode()) || HttpStatus.ACCEPTED.equals(responseEntity.getStatusCode())) {
statusBool = true;
}
if (null != responseEntity && statusBool) {
T result = responseEntity.getBody();
LOGGER.info("请求地址 = {} || 请求参数 = {} || 响应时长 = {} || 响应结果 = {}",
url, JSON.toJSONString(param), System.currentTimeMillis() - startTime, JSON.toJSONString(responseEntity.getBody()));
if (StringUtils.isEmpty(result)) {
return null;
}
return result;
} else {
LOGGER.error("请求地址 = {} || 请求参数 = {} || 响应时长 = {} || 响应结果 = {}",
url, JSON.toJSONString(param), System.currentTimeMillis() - startTime, JSON.toJSONString(responseEntity.getBody()));
throw new HttpClientErrorException(responseEntity.getStatusCode(),
"调用接口 = " + url + " || " +
"失败状态 = " + responseEntity.getStatusCode() + " || " +
"响应 = " + responseEntity.getBody());
}
}
/**
* @param url 请求地址
* @param headers 请求头
* @param method 请求类型 POST、GET
* @param param 参数
* @return String 返回结果
* @description: 发送请求,返回string字符串,需要自己转换,应用返回结果简单的请求
*/
public String doExecute(String url, Map<String, String> headers, HttpMethod method, String param) {
long startTime = System.currentTimeMillis();
LOGGER.info("\n 请求地址 = {} || 请求方式 = {} || 请求头 = {} || 请求参数 = {}",
url, method.name(), JSON.toJSONString(headers), JSON.toJSONString(param));
//封装httpEntity
HttpEntity httpEntity = getHttpEntity(headers, param);
//发送请求
ResponseEntity<String> responseEntity;
try {
responseEntity = restTemplate.exchange(url, method, httpEntity, String.class);
} catch (ResourceAccessException e) {
LOGGER.error("HttpRestUtil -> doExecute 捕获IOException异常,{}", e.getMessage());
try {
Thread.sleep(2000);
} catch (Exception ex) {
LOGGER.error("HttpRestUtil -> doExecute 暂停2S异常!", ex);
}
responseEntity = restTemplate.exchange(url, method, httpEntity, String.class);
}
//ResponseEntity<String> responseEntity = get().exchange(url, method, httpEntity, String.class);
//响应状态值 200 201 202
boolean statusBool = false;
if (HttpStatus.OK.equals(responseEntity.getStatusCode()) || HttpStatus.CREATED.equals(responseEntity.getStatusCode()) || HttpStatus.ACCEPTED.equals(responseEntity.getStatusCode())) {
statusBool = true;
}
if (null != responseEntity && statusBool) {
String result = responseEntity.getBody();
LOGGER.info("请求地址 = {} || 请求参数 = {} || 响应时长 = {} || 响应结果 = {}",
url, JSON.toJSONString(param), System.currentTimeMillis() - startTime, result);
if (StringUtils.isEmpty(result)) {
return null;
}
return result;
} else {
LOGGER.error("请求地址 = {} || 请求参数 = {} || 响应时长 = {} || 响应结果 = {}",
url, JSON.toJSONString(param), System.currentTimeMillis() - startTime, responseEntity.getBody());
throw new HttpClientErrorException(responseEntity.getStatusCode(),
"调用接口 = " + url + " || " +
"失败状态 = " + responseEntity.getStatusCode() + " || " +
"响应 = " + responseEntity.getBody());
}
}
/**
* @param url 请求地址
* @param classType 请返回类型
* @param uriVariables 参数
* @return
* @description: 发送get请求,占位符方式请求
*/
public <T> T doGet(String url, Class<T> classType, Object... uriVariables) {
long startTime = System.currentTimeMillis();
LOGGER.info("\n 请求地址 = {} || 请求方式 = {} || 请求参数 = {}",
url, "GET", JSON.toJSONString(uriVariables));
//发送请求
ResponseEntity<T> responseEntity;
try {
responseEntity = restTemplate.getForEntity(url, classType, uriVariables);
} catch (ResourceAccessException e) {
LOGGER.error("HttpRestUtil -> doExecute 捕获IOException异常," + e.getMessage());
try {
Thread.sleep(2000);
} catch (Exception ex) {
LOGGER.error("HttpRestUtil -> doExecute 暂停2S异常!" + ex.getMessage());
}
responseEntity = restTemplate.getForEntity(url, classType, uriVariables);
}
//响应状态值 200 201 202
boolean statusBool = false;
if (HttpStatus.OK.equals(responseEntity.getStatusCode()) || HttpStatus.CREATED.equals(responseEntity.getStatusCode()) || HttpStatus.ACCEPTED.equals(responseEntity.getStatusCode())) {
statusBool = true;
}
if (null != responseEntity && statusBool) {
T result = responseEntity.getBody();
LOGGER.info("请求地址 = {} || 请求参数 = {} || 响应时长 = {} || 响应结果 = {}",
url, JSON.toJSONString(uriVariables), System.currentTimeMillis() - startTime, result);
if (StringUtils.isEmpty(result)) {
return null;
}
return result;
} else {
LOGGER.error("请求地址 = {} || 请求参数 = {} || 响应时长 = {} || 响应结果 = {}",
url, JSON.toJSONString(uriVariables), System.currentTimeMillis() - startTime, responseEntity.getBody());
throw new HttpClientErrorException(responseEntity.getStatusCode(),
"调用接口 = " + url + " || " +
"失败状态 = " + responseEntity.getStatusCode() + " || " +
"响应 = " + responseEntity.getBody());
}
}
/**
* 发送POST请求,表单形式提交请求
*
* @param url 请求URL
* @param classType 返回对象类型
* @param param 请求参数
* @param <T> 返回结果
* @return
*/
public <T> T doPost(String url, Class<T> classType, Map<String, Object> param) {
long startTime = System.currentTimeMillis();
LOGGER.info("\n 请求地址 = {} || 请求方式 = {} || 请求参数 = {}", url, "POST", JSON.toJSONString(param));
MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
for (String key : param.keySet()) {
postParameters.add(key, param.get(key));
}
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
HttpEntity<MultiValueMap<String, Object>> r = new HttpEntity<>(postParameters, headers);
T t = restTemplate.postForObject(url, r, classType);
LOGGER.info("请求地址 = {} || 请求参数 = {} || 响应时长 = {} || 响应结果 = {}",
url, JSON.toJSONString(param), System.currentTimeMillis() - startTime, JSON.toJSONString(t));
return t;
}
private static HttpEntity getHttpEntity(Map<String, String> headers, String param) {
HttpHeaders httpHeaders = new HttpHeaders();
Iterator iterator = headers.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry) iterator.next();
String key = entry.getKey();
if (!httpHeaders.containsKey(key)) {
httpHeaders.add(key, entry.getValue());
}
}
return new HttpEntity(param, httpHeaders);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment