增加restTemplate工具和xml工具
This commit is contained in:
parent
b3e21ecec2
commit
7d0d5220f1
@ -120,6 +120,13 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<!-- slf4j end -->
|
<!-- slf4j end -->
|
||||||
|
|
||||||
|
<!-- dom4j start -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>dom4j</groupId>
|
||||||
|
<artifactId>dom4j</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- dom4j end -->
|
||||||
|
|
||||||
<!-- wgink start -->
|
<!-- wgink start -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>ink.wgink</groupId>
|
<groupId>ink.wgink</groupId>
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
package ink.wgink.util;
|
package ink.wgink.util;
|
||||||
|
|
||||||
|
import org.apache.commons.collections.KeyValue;
|
||||||
|
import org.apache.commons.collections.keyvalue.DefaultKeyValue;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When you feel like quitting. Think about why you started
|
* When you feel like quitting. Think about why you started
|
||||||
@ -100,6 +104,29 @@ public class ReflectUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get、set方法
|
||||||
|
*
|
||||||
|
* @param fields
|
||||||
|
* @return [0] getMethodName; [1] setMethodName;
|
||||||
|
*/
|
||||||
|
public static Map<String, String[]> fieldGetSetMethod(Field[] fields) {
|
||||||
|
if (fields == null || fields.length == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Map<String, String[]> result = new HashMap<>(16);
|
||||||
|
for (Field field : fields) {
|
||||||
|
String fieldName = field.getName();
|
||||||
|
String firstLetter = fieldName.substring(0, 1).toUpperCase();
|
||||||
|
String firstUpperMethodName = firstLetter + fieldName.substring(1);
|
||||||
|
String getMethodName = "get" + firstUpperMethodName;
|
||||||
|
String setMethodName = "set" + firstUpperMethodName;
|
||||||
|
String[] getSetNameArray = new String[]{getMethodName, setMethodName};
|
||||||
|
result.put(fieldName, getSetNameArray);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public static class ReflectException extends ReflectiveOperationException {
|
public static class ReflectException extends ReflectiveOperationException {
|
||||||
|
|
||||||
public ReflectException() {
|
public ReflectException() {
|
||||||
|
@ -0,0 +1,160 @@
|
|||||||
|
package ink.wgink.util.request;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.*;
|
||||||
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When you feel like quitting. Think about why you started
|
||||||
|
* 当你想要放弃的时候,想想当初你为何开始
|
||||||
|
*
|
||||||
|
* @ClassName: AbstractRestTemplateUtil
|
||||||
|
* @Description:
|
||||||
|
* @Author: wanggeng
|
||||||
|
* @Date: 2021/3/31 12:18 下午
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
public abstract class AbstractRestTemplate<SuccessResultType, PostBodyType> {
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(AbstractRestTemplate.class);
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
public AbstractRestTemplate() {
|
||||||
|
SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
|
||||||
|
simpleClientHttpRequestFactory.setConnectTimeout(20 * 1000);
|
||||||
|
simpleClientHttpRequestFactory.setReadTimeout(60 * 1000);
|
||||||
|
this.restTemplate = new RestTemplate(simpleClientHttpRequestFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求头
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public HttpHeaders headers() {
|
||||||
|
return new HttpHeaders();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* json请求头
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public HttpHeaders jsonHeaders() {
|
||||||
|
HttpHeaders httpHeaders = headers();
|
||||||
|
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
||||||
|
return httpHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpHeaders formHeaders() {
|
||||||
|
HttpHeaders httpHeaders = headers();
|
||||||
|
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||||
|
return httpHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST请求主体
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public abstract PostBodyType postBody();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 成功返回类型
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public abstract Class<SuccessResultType> successResultType();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET请求
|
||||||
|
*
|
||||||
|
* @param url
|
||||||
|
*/
|
||||||
|
public void get(String url) {
|
||||||
|
Class<SuccessResultType> successResultTypeClass = successResultType();
|
||||||
|
if (successResultTypeClass == null) {
|
||||||
|
LOG.error("method successResultType can not return null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
ResponseEntity<SuccessResultType> responseEntity = restTemplate.getForEntity(url, successResultTypeClass);
|
||||||
|
SuccessResultType result = responseEntity.getBody();
|
||||||
|
if (HttpStatus.OK.value() == responseEntity.getStatusCodeValue()) {
|
||||||
|
success200(result);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (e instanceof HttpClientErrorException) {
|
||||||
|
HttpClientErrorException exception = (HttpClientErrorException) e;
|
||||||
|
if (exception.getRawStatusCode() == HttpStatus.BAD_REQUEST.value()) {
|
||||||
|
error400(exception);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST提交FORM
|
||||||
|
*
|
||||||
|
* @param url
|
||||||
|
*/
|
||||||
|
public void post(String url) {
|
||||||
|
post(url, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST提交JSON
|
||||||
|
*
|
||||||
|
* @param url
|
||||||
|
*/
|
||||||
|
public void postJson(String url) {
|
||||||
|
post(url, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void post(String url, boolean isJson) {
|
||||||
|
PostBodyType postBodyType = postBody();
|
||||||
|
Class<SuccessResultType> successResultTypeClass = successResultType();
|
||||||
|
if (postBodyType == null) {
|
||||||
|
LOG.error("method postBody can not return null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (successResultTypeClass == null) {
|
||||||
|
LOG.error("method successResultType can not return null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
HttpEntity<PostBodyType> httpEntity = new HttpEntity<>(postBodyType, isJson ? jsonHeaders() : formHeaders());
|
||||||
|
ResponseEntity<SuccessResultType> responseEntity = restTemplate.postForEntity(url, httpEntity, successResultTypeClass);
|
||||||
|
SuccessResultType result = responseEntity.getBody();
|
||||||
|
if (HttpStatus.OK.value() == responseEntity.getStatusCodeValue()) {
|
||||||
|
success200(result);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (e instanceof HttpClientErrorException) {
|
||||||
|
HttpClientErrorException exception = (HttpClientErrorException) e;
|
||||||
|
if (exception.getRawStatusCode() == HttpStatus.BAD_REQUEST.value()) {
|
||||||
|
error400(exception);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 成功结果
|
||||||
|
*
|
||||||
|
* @param result
|
||||||
|
*/
|
||||||
|
public abstract void success200(SuccessResultType result);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param error
|
||||||
|
*/
|
||||||
|
public abstract void error400(HttpClientErrorException error);
|
||||||
|
|
||||||
|
}
|
69
basic-util/src/main/java/ink/wgink/util/xml/XMLUtil.java
Normal file
69
basic-util/src/main/java/ink/wgink/util/xml/XMLUtil.java
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package ink.wgink.util.xml;
|
||||||
|
|
||||||
|
import ink.wgink.util.ReflectUtil;
|
||||||
|
import org.apache.commons.collections.KeyValue;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.dom4j.Document;
|
||||||
|
import org.dom4j.DocumentException;
|
||||||
|
import org.dom4j.Element;
|
||||||
|
import org.dom4j.io.SAXReader;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When you feel like quitting. Think about why you started
|
||||||
|
* 当你想要放弃的时候,想想当初你为何开始
|
||||||
|
*
|
||||||
|
* @ClassName: XMLUtil
|
||||||
|
* @Description: XML工具类
|
||||||
|
* @Author: wanggeng
|
||||||
|
* @Date: 2021/4/28 9:44 上午
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
public class XMLUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* XML转简单bean,bean属性类型为字符串
|
||||||
|
*
|
||||||
|
* @param xml
|
||||||
|
* @param clazz
|
||||||
|
* @param <T>
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static <T> T xml2SampleBean(String xml, Class<T> clazz) throws Exception {
|
||||||
|
if (StringUtils.isBlank(xml)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Field[] fields = clazz.getDeclaredFields();
|
||||||
|
Map<String, String[]> getSetMethodMap = ReflectUtil.fieldGetSetMethod(fields);
|
||||||
|
if (getSetMethodMap == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
SAXReader saxReader = new SAXReader();
|
||||||
|
Document document = saxReader.read(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
|
||||||
|
Element rootElement = document.getRootElement();
|
||||||
|
List<Element> elements = rootElement.elements();
|
||||||
|
if (elements.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
T t = clazz.newInstance();
|
||||||
|
for (Element element : elements) {
|
||||||
|
String elementName = element.getName();
|
||||||
|
String firstLower = elementName.substring(0, 1).toLowerCase();
|
||||||
|
String firstLowerMethodName = firstLower + elementName.substring(1);
|
||||||
|
String[] getSetMethodArray = getSetMethodMap.get(firstLowerMethodName);
|
||||||
|
Method setMethod = clazz.getMethod(getSetMethodArray[1], String.class);
|
||||||
|
String elementText = element.getTextTrim();
|
||||||
|
setMethod.invoke(t, StringUtils.isBlank(elementText) ? "" : elementText);
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user