From 7d0d5220f14550f7fe24b130a4dc5f313843ac6b Mon Sep 17 00:00:00 2001 From: wanggeng888 <450292408@qq.com> Date: Wed, 28 Apr 2021 12:01:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0restTemplate=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E5=92=8Cxml=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- basic-util/pom.xml | 7 + .../main/java/ink/wgink/util/ReflectUtil.java | 27 +++ .../util/request/AbstractRestTemplate.java | 160 ++++++++++++++++++ .../main/java/ink/wgink/util/xml/XMLUtil.java | 69 ++++++++ 4 files changed, 263 insertions(+) create mode 100644 basic-util/src/main/java/ink/wgink/util/request/AbstractRestTemplate.java create mode 100644 basic-util/src/main/java/ink/wgink/util/xml/XMLUtil.java diff --git a/basic-util/pom.xml b/basic-util/pom.xml index ae632f61..ff8665e4 100644 --- a/basic-util/pom.xml +++ b/basic-util/pom.xml @@ -120,6 +120,13 @@ + + + dom4j + dom4j + + + ink.wgink diff --git a/basic-util/src/main/java/ink/wgink/util/ReflectUtil.java b/basic-util/src/main/java/ink/wgink/util/ReflectUtil.java index 77a665c9..aaff7374 100644 --- a/basic-util/src/main/java/ink/wgink/util/ReflectUtil.java +++ b/basic-util/src/main/java/ink/wgink/util/ReflectUtil.java @@ -1,10 +1,14 @@ package ink.wgink.util; +import org.apache.commons.collections.KeyValue; +import org.apache.commons.collections.keyvalue.DefaultKeyValue; import org.apache.commons.lang3.StringUtils; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; /** * 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 fieldGetSetMethod(Field[] fields) { + if (fields == null || fields.length == 0) { + return null; + } + Map 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 ReflectException() { diff --git a/basic-util/src/main/java/ink/wgink/util/request/AbstractRestTemplate.java b/basic-util/src/main/java/ink/wgink/util/request/AbstractRestTemplate.java new file mode 100644 index 00000000..aebd624b --- /dev/null +++ b/basic-util/src/main/java/ink/wgink/util/request/AbstractRestTemplate.java @@ -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 { + 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(); + + /** + * GET请求 + * + * @param url + */ + public void get(String url) { + Class successResultTypeClass = successResultType(); + if (successResultTypeClass == null) { + LOG.error("method successResultType can not return null"); + return; + } + try { + ResponseEntity 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 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 httpEntity = new HttpEntity<>(postBodyType, isJson ? jsonHeaders() : formHeaders()); + ResponseEntity 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); + +} diff --git a/basic-util/src/main/java/ink/wgink/util/xml/XMLUtil.java b/basic-util/src/main/java/ink/wgink/util/xml/XMLUtil.java new file mode 100644 index 00000000..d1769fc6 --- /dev/null +++ b/basic-util/src/main/java/ink/wgink/util/xml/XMLUtil.java @@ -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 + * @return + * @throws Exception + */ + public static T xml2SampleBean(String xml, Class clazz) throws Exception { + if (StringUtils.isBlank(xml)) { + return null; + } + Field[] fields = clazz.getDeclaredFields(); + Map 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 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; + } + +}