first commit

master
wenfei 7 months ago
parent a4e6567e0c
commit 7136d94af2

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="okhttp3util" />
<module name="jpa" />
<module name="util" />
<module name="test" />
<module name="web" />
<module name="spring-util" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="jpa" options="-parameters" />
<module name="spring-util" options="-parameters" />
<module name="util" options="-parameters" />
</option>
</component>
</project>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/jpa/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/okhttp3util/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/spring-util/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/test/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/util/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/web/src/main/java" charset="UTF-8" />
</component>
</project>

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="http://47.242.184.139:8081/repository/maven-public/" />
</remote-repository>
<remote-repository>
<option name="id" value="maven-snapshots" />
<option name="name" value="maven-snapshots" />
<option name="url" value="http://47.242.184.139:8081/repository/maven-public/" />
</remote-repository>
<remote-repository>
<option name="id" value="repository" />
<option name="name" value="repository" />
<option name="url" value="http://47.242.184.139:8081/repository/maven-public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="maven-central" />
<option name="name" value="maven-central" />
<option name="url" value="http://47.242.184.139:8081/repository/maven-public/" />
</remote-repository>
<remote-repository>
<option name="id" value="maven-releases" />
<option name="name" value="maven-releases" />
<option name="url" value="http://47.242.184.139:8081/repository/maven-public/" />
</remote-repository>
</component>
</project>

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="corretto-1.8" project-jdk-type="JavaSDK" />
</project>

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

@ -1,3 +1,24 @@
# utils202108
# utils
工具集合
utils202108
# jpa
jpa工具
#myJs
js 常用函数
js 常用功能封装
# okhttp3util
对okhttp3进行轻量级封装
# spring-util
spring工具类 引入后自动将自己注入到程序中
#test
测试工具
# util
个人编程过程中经常用的工具集合
# web
web编程工具

@ -0,0 +1,116 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liuhuiyu</groupId>
<artifactId>jpa</artifactId>
<version>2.0-SNAPSHOT</version>
<name>jpa</name>
<url>http://www.liuhuiyu.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>19.0.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.liuhuiyu</groupId>-->
<!-- <artifactId>spring-util</artifactId>-->
<!-- <version>1.0.0</version>-->
<!-- </dependency>-->
</dependencies>
<!--项目部署和发布的话需要配置: 在pom.xml中添加 distributionManagement 配置-->
<!-- maven仓库配置 deploy时可推送到配置的仓库中 -->
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>Nexus Releases Repository Pro</name>
<url>http://47.242.184.139:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>Nexus Snapshots Repository Pro</name>
<url>http://47.242.184.139:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

@ -0,0 +1,203 @@
package com.liuhuiyu.jpa;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.jpa.repository.query.Jpa21Utils;
import javax.sql.DataSource;
import java.sql.*;
import java.util.*;
import java.util.function.Consumer;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-03-22 9:12
*/
public abstract class BaseView {
private final DataSource dataSource;
public BaseView(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
*
*
* @param callFunctions
* @author LiuHuiYu
* Created DateTime 2021-03-25 10:42
*/
protected void actionConnection(Consumer<Connection> callFunctions) {
try (Connection connection = this.dataSource.getConnection()) {
callFunctions.accept(connection);
}
catch (SQLException throwable) {
throw new RuntimeException("获取连接异常。", throwable);
}
}
/**
* PreparedStatement
*
* @param sql
* @param callFunctions
* @author LiuHuiYu
* Created DateTime 2021-03-25 10:45
*/
protected void actionPreparedStatement(String sql, Consumer<PreparedStatement> callFunctions) {
actionConnection(connection -> {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
callFunctions.accept(preparedStatement);
}
catch (SQLException e) {
throw new RuntimeException(e);
}
// PreparedStatement preparedStatement;
// try {
// preparedStatement = connection.prepareStatement(sql);
// }
// catch (SQLException throwable) {
// throw new RuntimeException("创建PreparedStatement异常", throwable);
// }
// callFunctions.accept(preparedStatement);
// try {
// connection.close();
// }
// catch (SQLException e) {
// throw new RuntimeException(e);
// }
});
}
/**
* sqlResultSet
*
* @param sql
* @param parameterMap Map
* @param callFunctions
* @author LiuHuiYu
* Created DateTime 2021-03-25 10:46
*/
protected void fullResultSet(String sql, Map<String, Object> parameterMap, Consumer<ResultSet> callFunctions) {
NamedParameterStatement namedParameterStatement = new NamedParameterStatement(sql);
this.actionPreparedStatement(namedParameterStatement.getSql(), preparedStatement -> {
if (parameterMap != null) {
namedParameterStatement.fillParameters(preparedStatement, parameterMap);
}
Log.i(namedParameterStatement.getSql());
ResultSet resultSet;
try {
resultSet = preparedStatement.executeQuery();
}
catch (SQLException throwable) {
throw new RuntimeException("执行sql语句异常。", throwable);
}
callFunctions.accept(resultSet);
});
}
/**
*
*
* @param sql
* @param parameterMap
* @return java.util.List<java.lang.Object [ ]>
* @author LiuHuiYu
* Created DateTime 2021-03-22 14:05
*/
protected List<Object[]> getResultList(String sql, Map<String, Object> parameterMap) {
return getResultList(sql, parameterMap, false);
}
/**
*
*
* @param sql
* @param parameterMap
* @param onlyFirst
* @return java.util.List<java.lang.Object [ ]>
* @author LiuHuiYu
* Created DateTime 2021-03-22 14:05
*/
protected List<Object[]> getResultList(String sql, Map<String, Object> parameterMap, boolean onlyFirst) {
ArrayList<Object[]> resList = new ArrayList<>();
this.fullResultSet(sql, parameterMap, (resultSet) -> {
try {
int columnCount = resultSet.getMetaData().getColumnCount();
while (resultSet.next()) {
Object[] objs = new Object[columnCount];
for (int i = 1; i <= columnCount; i++) {
objs[i - 1] = (resultSet.getObject(i));
}
resList.add(objs);
if (onlyFirst) {
return;
}
}
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
});
return resList;
}
/**
*
*
* @param b DaoOperator
* @param sql
* @param parameterMap
* @return java.util.List<T>
* @author LiuHuiYu
* Created DateTime 2021-03-22 14:04
*/
protected <T> List<T> getResultList(DaoOperator<T> b, String sql, Map<String, Object> parameterMap) {
List<?> list = this.getResultList(sql, parameterMap);
List<T> resList = new ArrayList<>();
for (Object obj : list) {
resList.add(b.objectToT(obj));
}
return resList;
}
/**
*
*
* @param b DaoOperator
* @param sql
* @param parameterMap
* @return java.util.Optional<T>
* @author LiuHuiYu
* Created DateTime 2021-03-22 14:02
*/
protected <T> Optional<T> getFirstResult(DaoOperator<T> b, String sql, Map<String, Object> parameterMap) {
List<?> list = this.getResultList(sql, parameterMap, true);
if (list.size() == 0) {
return Optional.empty();
}
else {
T t = b.objectToT(list.get(0));
return Optional.of(t);
}
}
/**
*
*
* @param sql
* @param parameterMap
* @return java.util.Optional<java.lang.Object>
* @author LiuHuiYu
* Created DateTime 2021-03-22 13:46
*/
protected Optional<Object> getSingleResult(String sql, Map<String, Object> parameterMap) {
List<Object[]> list = this.getResultList(sql, parameterMap, true);
if (list.size() == 0 || list.get(0).length == 0) {
return Optional.empty();
}
else {
return Optional.of(list.get(0)[0]);
}
}
}

@ -0,0 +1,17 @@
package com.liuhuiyu.jpa;
/**
* (obj->T)
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-01-22 9:10
*/
@FunctionalInterface
public interface DaoOperator<T> {
/**
* Object T
* @param o Object
* @return T
*/
T objectToT(Object o);
}

@ -0,0 +1,25 @@
package com.liuhuiyu.jpa;
import lombok.extern.log4j.Log4j2;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-07-31 16:54
*/
@Log4j2
public class Log {
public static boolean outLog = false;
public static void i(String info) {
if (outLog) {
log.info(info);
}
}
public static void d(String info) {
if (outLog) {
log.debug(info);
}
}
}

@ -0,0 +1,83 @@
package com.liuhuiyu.jpa;
import lombok.extern.log4j.Log4j2;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-03-22 11:01
*/
public class NamedParameterStatement {
private final Map<Integer, String> paramsMap = new HashMap<>();
private String sql = "";
public Map<Integer, String> getParamsMap() {
return paramsMap;
}
public String getSql() {
return this.sql;
}
private void emptyMap() {
paramsMap.clear();
}
public NamedParameterStatement(String sql) {
this.parseSql(sql);
}
/**
* SQL使Map?
*
* @param sql
* @author LiuHuiYu
* Created DateTime 2021-03-22 14:10
*/
private void parseSql(String sql) {
String regex = "(:(\\w+))";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(sql);
emptyMap();
int idx = 1;
while (m.find()) {
//参数名称可能有重复使用序号来做Key
paramsMap.put(idx++, m.group(2));
}
this.sql = sql.replaceAll(regex, "?");
Log.i("分析前:" + sql);
Log.d("分析后:" + this.sql);
}
/**
* 使MappStat
*
* @param pStat PreparedStatement
* @param pMap
* @author LiuHuiYu
* Created DateTime 2021-03-22 14:10
*/
public void fillParameters(PreparedStatement pStat, Map<String, Object> pMap) {
this.paramsMap.forEach((idKey, valueKey) -> {
if (pMap.containsKey(valueKey)) {
Object value = pMap.get(valueKey);
try {
pStat.setObject(idKey, value);
}
catch (SQLException throwables) {
throw new RuntimeException("填充参数出错,原因:", throwables);
}
}
else {
throw new RuntimeException("语句中参数‘" + valueKey + "’不存在列表中");
}
});
}
}

@ -0,0 +1,165 @@
package com.liuhuiyu.view;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.function.Function;
/**
*
*
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-03-09 17:30
*/
public class ObjectArray {
private final Object[] objects;
private int index = 0;
public ObjectArray(Object obj) {
this.objects = (Object[]) obj;
}
public void reset() {
this.index = 0;
}
public Object get() {
if (this.index >= objects.length) {
throw new RuntimeException("获取的数据数量" + this.index + "超出了原始数据长度" + this.objects.length);
}
return objects[this.index++];
}
public String getString() {
return getString("");
}
public String getString(String defValue) {
return getT((obj) -> {
if (obj instanceof String) {
return (String) obj;
}
else {
return obj.toString();
}
}, defValue);
}
public Integer getInteger() {
return getInteger(0);
}
public Integer getInteger(Integer defValue) {
return getT((obj) -> {
if (obj instanceof Number) {
return ((Number) obj).intValue();
}
else {
return Integer.parseInt(obj.toString());
}
}, defValue);
}
public Long getLong() {
return getLong(0L);
}
public Long getLong(Long defValue) {
return getT((obj) -> {
if (obj instanceof Number) {
return ((Number) obj).longValue();
}
else {
return Long.parseLong(obj.toString());
}
}, defValue);
}
public Boolean getBoolean() {
return getBoolean(false);
}
public Boolean getBoolean(Boolean defValue) {
return getT((obj) -> {
if (obj instanceof Boolean) {
return (Boolean) obj;
}
else if (obj instanceof Number) {
return ((Number) obj).intValue() == 1;
}
else {
return Boolean.parseBoolean(obj.toString());
}
}, defValue);
}
public BigDecimal getBigDecimal() {
return getBigDecimal(new BigDecimal("0"));
}
public BigDecimal getBigDecimal(BigDecimal defValue) {
return getT((obj) -> {
if (obj instanceof BigDecimal) {
return (BigDecimal) obj;
}
else if (obj instanceof Number) {
return new BigDecimal(((Number) obj).toString());
}
else {
return new BigDecimal(obj.toString());
}
}, defValue);
}
public Timestamp getTimestamp() {
return getTimestamp(Timestamp.valueOf(LocalDateTime.MIN));
}
public Timestamp getTimestamp(Timestamp defValue) {
return getT((obj) -> {
if (obj instanceof Timestamp) {
return (Timestamp) obj;
}
else {
return defValue;
}
}, defValue);
}
public BigInteger getBigInteger() {
return getBigInteger(BigInteger.ZERO);
}
public BigInteger getBigInteger(BigInteger defValue) {
return getT((obj) -> {
if (obj instanceof BigInteger) {
return (BigInteger) obj;
}
if (obj instanceof Number) {
return BigInteger.valueOf(((Number) obj).longValue());
}
else {
return defValue;
}
}, defValue);
}
private <T> T getT(Function<Object, T> function, T defValue) {
Object obj = this.get();
if (obj == null) {
return defValue;
}
else {
try {
return function.apply(obj);
}
catch (Exception ex) {
return defValue;
}
}
}
}

@ -0,0 +1,2 @@
#org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
#com.liuhuiyu.jpa.BaseView

@ -0,0 +1,39 @@
package com.liuhuiyu;
import static org.junit.Assert.assertTrue;
import com.liuhuiyu.jpa.DaoOperator;
import lombok.Data;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest {
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue() {
Object obj = new Object[]{1, "abc"};
Abc abc = new Abc(obj);
Abc def=objToClass(Abc::new,obj);
assertTrue(true);
}
private <T> T objToClass(DaoOperator<T> daoOperator,Object obj){
return daoOperator.objectToT(obj);
}
@Data
static class Abc {
int index;
String name;
public Abc(Object obj) {
Object[] objects = (Object[]) obj;
index = (int) objects[0];
name = objects[1].toString();
}
}
}

@ -0,0 +1,65 @@
package com.liuhuiyu.view;
import lombok.extern.log4j.Log4j2;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-04-02 8:19
*/
@Log4j2
public class ObjectArrayTest {
@Test
public void get() {
Object object = new Object[]{1, 30_000_000_000L, "bbbb", true};
ObjectArray objectArray = new ObjectArray(object);
log.info("原始i={};l={},s={},b={}", 1, 30_000_000_000L, "bbbb", true);
int i = objectArray.getInteger();
Long l = objectArray.getLong();
String s = objectArray.getString();
boolean b = objectArray.getBoolean();
log.info("i l s bi={};l={},s={},b={}", i, l, s, b);
objectArray.reset();
b = objectArray.getBoolean(true);
i = objectArray.getInteger(18);
l = objectArray.getLong(100L);
s = objectArray.getString("NONE");
log.info("b i l s i={};l={},s={},b={}", i, l, s, b);
objectArray.reset();
s = objectArray.getString("NONE");
b = objectArray.getBoolean(true);
i = objectArray.getInteger(18);
l = objectArray.getLong(100L);
log.info("s b i l i={};l={},s={},b={}", i, l, s, b);
objectArray.reset();
s = objectArray.getString("NONE");
i = objectArray.getInteger(18);
b = objectArray.getBoolean(true);
l = objectArray.getLong(100L);
log.info("s i b l i={};l={},s={},b={}", i, l, s, b);
}
@Test
public void get2() {
int r1 = 1_000_000_000;
Object object = new Object[]{1, 30_000_000_000L, "bbbb", true};
ObjectArray objectArray = new ObjectArray(object);
int i = 0;
Long l = 0L;
String s = "";
boolean b = false;
for (int r = 0; r <= r1; r++) {
objectArray.reset();
i = objectArray.getInteger();
l = objectArray.getLong();
s = objectArray.getString();
b = objectArray.getBoolean();
}
log.info("s i b l i={};l={},s={},b={}", i, l, s, b);
}
}

@ -0,0 +1,3 @@
//分页
let paging=new ProgPaging({});
paging.showPage(10,5,6);

@ -0,0 +1,391 @@
/**
* 程序通用函数
* @type {{assignObj: (function(*=, *=): *), exitFullScreen: progFunction.exitFullScreen, isFullScreen: (function(): boolean), getMaxZIndex: (function(): any), fullScreen: progFunction.fullScreen}}
*/
progFunction = {
/**
* 功能描述
* @author LiuHuiYu
* Created DateTime 2021-03-15 9:00
* @param url
* @param data
* @param backFunction {success,error,complete}回调函数
* @return
*/
ajax: function (url, data, backFunction) {
if (typeof (backFunction.beforeSend) === "undefined") {
backFunction.beforeSend = function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa("test:test"));
};
}
$.ajax({
type: "POST",
url: url,
dataType: "json",
async: true,
data: data,
traditional: true,
beforeSend: function (xhr) {
backFunction.beforeSend(xhr);
},
success: function (res) {
if (res.success) {
progFunction.runFunc(backFunction.success, res.data);
}
else {
// debugger;
progFunction.runFunc(backFunction.error, res.msg);
}
},
error: function (res) {
// debugger;
progFunction.runFunc(backFunction.error, "错误码:" + res.status);
},
complete: function () {
progFunction.runFunc(backFunction.complete);
}
});
},
runFunc: function (runFunction, v1, v2, v3, v4, v5) {
if (typeof (runFunction) === "function") {
runFunction(v1, v2, v3, v4, v5);
}
},
/**
* 进入全屏
*/
fullScreen: function () {
try {
let docElm = document.documentElement;
//W3C
if (docElm.requestFullscreen) {
docElm.requestFullscreen();
}
//FireFox
else if (docElm.mozRequestFullScreen) {
docElm.mozRequestFullScreen();
}
//Chrome等
else if (docElm.webkitRequestFullScreen) {
docElm.webkitRequestFullScreen();
}
//IE11
else if (docElm.msRequestFullscreen) {
docElm.msRequestFullscreen();
}
} catch (error) {
console.error("执行全屏失败:", error);
}
},
/**
* 节点全屏
* @author LiuHuiYu
* Created DateTime 2021-03-05 15:01
* @param element 元素
* @return
*/
launchFullScreen: function (element) {
if (element.requestFullscreen) {
element.requestFullscreen();
}
else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();//火狐
}
else if (element.msRequestFullscreen) {
element.msRequestFullscreen();//ie浏览器
document.getElementById("fullScreen").style.height = window.screen.height + "px";
document.getElementById("fullScreen").style.width = document.documentElement.clientWidth + "px";
}
else if (element.webkitRequestFullscreen) {
element.webkitRequestFullScreen();//谷歌浏览器
}
},
/**
* 退出全屏
*/
exitFullScreen: function () {
if (document.exitFullscreen) {
document.exitFullscreen();
}
else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
}
else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen();
}
else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
},
/**
* 当前是否全屏
* @returns {boolean, undefined}ie11检测不到是否全屏
*/
isFullScreen: function () {
if (document.fullscreen !== undefined) {
return document.fullscreen;
}
else if (document.mozFullScreen !== undefined) {
return document.mozFullScreen;
}
else if (document.webkitIsFullScreen !== undefined) {
return document.webkitIsFullScreen;
}
else if (document.webkitFullScreen !== undefined) {
return document.webkitFullScreen;
}
else if (document.msFullScreen !== undefined) {
return document.msFullScreen;
}
else {
return undefined;
}
},
/**
* 最大的 zIndex 获取
* @returns {number}
*/
getMaxZIndex: function () {
return Math.max.apply(null,
$.map($('body *'), function (e) {
if ($(e).css('position') !== 'static')
return parseInt($(e).css('z-index')) || -1;
}));
},
/**
* 对象合并
* @param target 原始对象
* @param sources 加入的合并数据
* @returns {*}
*/
assignObj: function (target, sources) {
let obj = target;
if (typeof target != 'object' || typeof sources == 'function') {
if (typeof target == 'function' && typeof sources != 'function') {
return target;
}
return sources;
}
if (typeof sources != 'object') {
return target;
}
for (let key in sources) {
if (sources.hasOwnProperty(key)) {
// 如果target也存在 那就再次合并
obj[key] = target.hasOwnProperty(key) ? progFunction.assignObj(target[key], sources[key]) : sources[key];
}
}
return obj;
},
/**
* 深层复制
* @author LiuHuiYu
* Created DateTime 2021-03-03 10:22
* @param obj
* @return
*/
clone: function (obj) {
let o;
if (typeof obj == "object") {
if (obj === null) {
o = null;
}
else {
if (obj instanceof Array) {
o = [];
for (let i = 0, len = obj.length; i < len; i++) {
o.push(progFunction.clone(obj[i]));
}
}
else {
o = {};
for (let j in obj) {
if (obj.hasOwnProperty(j)) {
o[j] = progFunction.clone(obj[j]);
}
}
}
}
}
else {
o = obj;
}
return o;
},
stringIsEmpty: function (obj) {
return typeof (obj) == "string" && obj.trim() === "";
},
stringIsNotEmpty: function (obj) {
return typeof (obj) == "string" && obj.trim() !== "";
},
/**
* 默认赋值
* @author LiuHuiYu
* Created DateTime 2021-03-16 9:56
* @param inputValue 传入值
* @param defValue 默认返回值(null)
* @param allowNull 允许null
* @return
*/
defaultValue: function (inputValue, defValue, allowNull) {
if (allowNull === undefined) {
allowNull = false;
}
if (inputValue === undefined || (inputValue === null && !allowNull)) {
if (defValue === undefined) {
return null;
}
return defValue;
}
return inputValue;
},
/**
* 是否是ie浏览器
* @returns {boolean}
* @constructor
*/
isIE: function () {
return (!!window.ActiveXObject || "ActiveXObject" in window);
},
/**
* 遍历循环map
* @author LiuHuiYu
* Created DateTime 2021-03-06 15:07
* @param map 循环map
* @param func(value,key) 对象操作
* @return 是否结束循环
*/
forMap: function (map, func) {
for (let key in map) {
if (map.hasOwnProperty(key)) {
let res = func(map[key], key);
if (res === true) {
break;
}
}
}
},
/**
* 遍历循环 array反向循环
* @author LiuHuiYu
* Created DateTime 2021-03-08 11:12
* @param array 数组
* @param func 便利函数返回true 结束循环
* @return boolean 是否终止循环跳出
*/
forArray: function (array, func) {
let isBreak = false;
for (let index = array.length - 1; index >= 0; index--) {
isBreak = func(array[index], index);
if (isBreak === true) {
break;
}
}
return isBreak;
},
formatDate: function (date, fmt) {
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
};
for (let k in o) {
let t = new RegExp('(' + k + ')');
if (t.test(fmt)) {
let str = o[k] + '';
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
}
}
function padLeftZero(str) {
return ('00' + str).substr(str.length)
}
return fmt;
},
formatTimestamp: function (timestamp, fmt) {
let date = new Date(timestamp);
return progFunction.formatDate(date, fmt)
},
/**
* 获取随机数
* @author LiuHuiYu
* Created DateTime 2021-03-18 15:02
* @param start 最小数
* @param end 最大数
* @param fixed 是整数
* @return
*/
getRandom: function (start, end, fixed) {
if (fixed === undefined) {
fixed = 0;
}
let differ = end - start
let random = Math.random()
return (start + differ * random).toFixed(fixed)
},
/**
* 消息管理
* @author LiuHuiYu
* Created DateTime 2021-04-29 16:07
* @param null
* @return null
*/
messageQueueManagement: {
/**
* 创建消息管理
* @author LiuHuiYu
* Created DateTime 2021-04-29 16:07
* @param props {backFunction}
* @return null
*/
create: function (props) {
let baseProps = {backFunction: null};
let nowProps = progFunction.assignObj(baseProps, props);
let mqList = [];
function newInfo(info) {
let newV = {
id: "mq" + performance.now() + "_" + Math.floor(Math.random() * 100000000),
info: info,
}
mqList.push(newV);
change("D", message);
return newV;
}
function removeInfo(message) {
for (let i = mqList.length - 1; i >= 0; i--) {
if (mqList[i].id === message.id) {
mqList.splice(i, 1);
change("D", message);
return;
}
}
}
function change(type, data) {
if (nowProps.backFunction) {
nowProps.backFunction(type, data);
}
}
return {
putInfo: function (info) {
return newInfo(info);
},
removeInfo: function (message) {
removeInfo(message)
},
getList: function () {
return mqList;
}
};
}
},
}

@ -0,0 +1,156 @@
//
// 需要 progFunction.js 支持
//
/**
* 数据加载蒙版
* @param property
* @constructor
*/
function ProgLoading(property) {
let baseProperty = {
style: {
masking: {
'width': '100%',
'height': '100%',
'top': '0',
'left': '0',
'position': 'absolute',
'text-align': 'center',
'background-color': '#00000077',
},
box: {
'background-color': '#000161',
'color': '#00ffd5',
'top': '50%',
'left': '50%',
'position': 'absolute',
'transform': 'translate(-50%, -50%)',
'padding': '20px 50px',
},
},
class: {
masking_class: [],
masking_style: {},
box_class: [],
box_style: {},
},
info: {
div: null,
content: "数据加载中......",
},
};
let div;//蒙版
let box;//对话框
let autoClose = false;
let nowShowState = false;
initProgLoading(property);
/**
* 参数合并初始化
* @param property
*/
function initProgLoading(property) {
console.log('参数初始化');
if (typeof (property) === "string") {
//字符串的话就作为信息传入处理
property = {info: {content: property,}}
}
progFunction.assignObj(baseProperty, property);
}
/**
* 添加class
* @param item
* @param classList
*/
function addClass(item, classList) {
for (let i = 0, len = classList.length; i < len; i++) {
item.classList.add(classList[i]);
}
}
/**
* 添加样式
* @param item
* @param styles
*/
function addStyle(item, styles) {
for (let key in styles) {
if (styles.hasOwnProperty(key)) {
item.style[key] = styles[key];
}
}
}
/**
* 显示loading
* @param property
*/
this.show = function (property) {
initProgLoading(property);
if (div === undefined) {
let nowZIndex = progFunction.getMaxZIndex() + 100;
div = document.createElement("div");
div.style['z-index'] = nowZIndex;
if (autoClose) {
let _this = this;
div.onclick = _this.close;
}
}
else {
//样式 class 清理
}
//region 蒙版样式设定
if (baseProperty.class.masking_class.length > 0) {
addClass(div, baseProperty.class.masking_class);
addStyle(div, baseProperty.class.masking_style);
}
else {
addStyle(div, baseProperty.style.masking);
}
//endregion
if (box === undefined) {
box = document.createElement("div");
div.appendChild(box);
}
//region box样式设定
if (baseProperty.class.box_class.length > 0) {
addClass(box, baseProperty.class.box_class);
addStyle(box, baseProperty.class.box_style);
}
else {
addStyle(box, baseProperty.style.box);
}
box.innerHTML = baseProperty.info.content;
document.body.append(div);
nowShowState = true;
return this;
}
this.close = function () {
if (div !== undefined && nowShowState) {
nowShowState = false;
document.body.removeChild(div);
}
}
/**
* 显示信息到时间自动关闭
* @author LiuHuiYu
* Created DateTime 2021-02-18 10:36
* @param property 参数
* @param time 显示时间毫秒
* @return
*/
this.showInfo = function (property, time) {
autoClose = true;
if (time === undefined) {
time = 3000;
}
this.show(property);
let _this = this;
setTimeout(function () {
_this.close();
}, time);
return this;
}
}

@ -0,0 +1,154 @@
/**
* 分页功能
* @author LiuHuiYu
* Created DateTime 2021-02-07 10:09
* @param property {showFirstPage,showLastPage,parentDiv,outerDiv,outPage,goPageEvent}
* @return
*/
function ProgPaging(property) {
let baseProperty = {
/**
* 显示首页
*/
showFirstPage: true,
/**
* 显示末页
*/
showLastPage: true,
/**
* 分页上级节点如果没有默认是body
*/
parentDiv: null,
/**
* 分页Dom
*/
outerDiv: null,
/**
* 输出函数
* 参数页面索引是否是当前页是否是首页是否是末页
*/
outPage: setPage,
/**
* 跳转事件函数
*/
goPageEvent: goPage,
};
Object.assign(baseProperty, property);
/**
* 设置更新分页页面
* @param index 当前索引
* @param isNowPage 是否是当前页面
* @param isFirst 是否是第一页
* @param isLast 是否是最后一页
*/
function setPage(index, isNowPage, isFirst, isLast) {
let li = document.createElement("li");
if (!isNowPage) {
(function (index) {
//监听点击事件 object比如google地图中的Maker对象
li.addEventListener("click", function () {
baseProperty.goPageEvent(index); //调用方法
});
})(index);
}
if (isNowPage) {
li.innerHTML = '(' + (index + 1) + ')';
}
else if (isFirst) {
li.innerHTML = '首页';
}
else if (isLast) {
li.innerHTML = '末页';
}
else {
li.innerHTML = (index + 1);
}
baseProperty.outerDiv.appendChild(li);
}
function goPage(index) {
console.info("跳转" + index);
}
/**
* 功能描述
* @author LiuHuiYu
* Created DateTime 2021-02-07 10:15
* @param countPageNum 总页数
* @param nowPageIndex 当前页索引0开始
* @param showPageNum 显示页面数量(最少显示3页)
* @param property 更新设定参数
* @return
*/
this.showPage = function page(countPageNum, nowPageIndex, showPageNum, property) {
Object.assign(baseProperty, property);
//region 异常数据处理
if (countPageNum <= 1) {
//不显示
return;
}
if (showPageNum < 3) {
showPageNum = 3;
}
//页面异常数据处理
if (nowPageIndex < 0) {
nowPageIndex = 0;
}
else if (nowPageIndex > countPageNum - 1) {
nowPageIndex = countPageNum - 1
}
if (showPageNum > countPageNum) {
showPageNum = countPageNum;
}
//endregion
//显示页面的中间位置//取丢弃小数部分,保留整数部分
let halfAmount = Math.ceil((showPageNum - 1) / 2);
let amendmentsNum = (showPageNum + 1) % 2;
let frontNumber, afterNumber;
//当前页面位置判断。
if (nowPageIndex <= halfAmount) {
frontNumber = nowPageIndex;
afterNumber = showPageNum - 1 - frontNumber;
}
else if ((countPageNum - nowPageIndex - 1) <= halfAmount) {
afterNumber = countPageNum - nowPageIndex - 1;
frontNumber = showPageNum - 1 - afterNumber;
}
else {
if (countPageNum / 2 > nowPageIndex) {
frontNumber = halfAmount + amendmentsNum;
afterNumber = showPageNum - 1 - frontNumber;
}
else {
afterNumber = halfAmount + amendmentsNum;
frontNumber = showPageNum - 1 - afterNumber;
}
}
if (baseProperty.parentDiv == null) {
baseProperty.parentDiv = document.createElement("div");
document.body.append(baseProperty.parentDiv);
}
if (baseProperty.outerDiv == null) {
baseProperty.outerDiv = document.createElement("lu");
baseProperty.parentDiv.appendChild(baseProperty.outerDiv);
}
baseProperty.outerDiv.innerHTML = '';
if ((nowPageIndex - frontNumber) > 0) {
setPage(0, (nowPageIndex === 0), true, false);
}
for (let i = frontNumber; i > 0; i--) {
setPage(nowPageIndex - i, false, false, false);
}
setPage(nowPageIndex, true, false, false);
for (let i = 0; i < afterNumber; i++) {
setPage(nowPageIndex + i + 1, false, false, false);
}
if ((nowPageIndex + afterNumber) < countPageNum - 1) {
setPage(countPageNum - 1, (nowPageIndex === (countPageNum - 1)), false, true);
}
}
}

@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liuhuiyu</groupId>
<artifactId>okhttp3util</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0-RC1</version>
</dependency>
<!--因为okhttp jar冲突 添加 -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>1.3.70</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>19.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
<!--项目部署和发布的话需要配置: 在pom.xml中添加 distributionManagement 配置-->
<!-- maven仓库配置 deploy时可推送到配置的仓库中 -->
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>Nexus Releases Repository Pro</name>
<url>http://47.242.184.139:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>Nexus Snapshots Repository Pro</name>
<url>http://47.242.184.139:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>1.3.70</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,12 @@
package com.liuhuiyu.okhttp;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-01-09 17:30
*/
public class OkHttpException extends RuntimeException{
public OkHttpException(String message) {
super(message);
}
}

@ -0,0 +1,600 @@
package com.liuhuiyu.okhttp;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Collection;
import java.util.function.Supplier;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-01-09 15:06
*/
public class OkHttpUtil {
private final OkHttpClient.Builder client;
// private final Request.Builder builder;
/**
* bodyform-datax-www-form-urlencoded
* Created DateTime 2021-03-29 22:00
*/
// private final Map<String, String> bodyMap;
private final List<String[]> bodyMap;
/**
* params
* Created DateTime 2021-03-29 21:58
*/
private final Map<String, String> paramMap;
private final List<String[]> headerList;
private String method;
private String bodyString = "";
private Object tag;
public static final String MEDIA_TYPE_APPLICATION_JSON_UTF_8 = "application/json;charset=utf-8";
public static int CONNECT_TIMEOUT = 15;
public static int READ_TIMEOUT = 15;
public static int WRITE_TIMEOUT = 15;
private OkHttpUtil(int connectTimeout, int readTimeout, int writeTimeout) {
this.client = new OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.connectTimeout(connectTimeout, TimeUnit.SECONDS)
.readTimeout(readTimeout, TimeUnit.SECONDS)
.writeTimeout(writeTimeout, TimeUnit.SECONDS)
.connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS));
//.build();
this.paramMap = new HashMap<>(0);
// this.bodyMap = new HashMap<>(0);
this.bodyMap = new ArrayList<>(0);
this.headerList = new ArrayList<>();
this.method = "";
}
// public void https(){
// this.client.hostnameVerifier(new HostnameVerifier() {
// @Override
// public boolean verify(String hostname, SSLSession session) {
// return true;
// }
// }).sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager);
// }
/**
*
*
* @return
*/
public static OkHttpUtil create() {
return create(CONNECT_TIMEOUT, READ_TIMEOUT, WRITE_TIMEOUT);
}
/**
*
*
* @param connectTimeout
* @param readTimeout
* @param writeTimeout
* @return com.liuhuiyu.okhttp.OkHttpUtil
* @author LiuHuiYu
* Created DateTime 2021-03-30 9:39
*/
public static OkHttpUtil create(int connectTimeout, int readTimeout, int writeTimeout) {
return new OkHttpUtil(connectTimeout, readTimeout, writeTimeout);
}
public void setMethod(String value) {
this.method = value == null ? "" : value.trim();
}
public void setBodyString(String bodyString) {
this.bodyString = bodyString;
}
public void setTag(Object tag) {
this.tag = tag;
}
/**
* body
*
* @param key
* @param value
* @return OkHttpUtil
*/
public OkHttpUtil addBody(String key, String value) {
this.bodyMap.add(new String[]{key, value});
// this.bodyMap.put(key, value);
return this;
}
/**
* uri
*
* @param key
* @param value
* @return OkHttpUtil
*/
public OkHttpUtil addQueryParameter(String key, String value) {
this.paramMap.put(key, value);
return this;
}
/**
* Header
*
* @param key
* @param value
* @return OkHttpUtil
*/
public OkHttpUtil addHeader(String key, String value) {
this.headerList.add(new String[]{key, value});
return this;
}
/**
*
*
* @param username
* @param password
* @return OkHttpUtil
*/
public OkHttpUtil headerAuthorizationByBasicAuth(String username, String password) {
String base64AppMsg;
String appMsg = username + ":" + password;
base64AppMsg = Base64.getEncoder().encodeToString(appMsg.getBytes(StandardCharsets.UTF_8));
return this.addHeader("Authorization", "Basic " + base64AppMsg);
}
public OkHttpUtil headerAuthorizationByBearerToken(String token) {
return this.addHeader("Authorization", "Bearer " + token);
}
//region 生成基本数据
private HttpUrl getHttpUrl(String url) {
HttpUrl.Builder urlBuilder = Objects.requireNonNull(HttpUrl.parse(url)).newBuilder();
this.paramMap.forEach(urlBuilder::addQueryParameter);
return urlBuilder.build();
}
private RequestBody getRequestBody() {
RequestBody body;
if (this.bodyMap.size() > 0) {
// if ("".equals(this.method)) {
FormBody.Builder bodyBuilder = new FormBody.Builder();
// this.bodyMap.forEach(bodyBuilder::add);
this.bodyMap.forEach((infos) -> bodyBuilder.add(infos[0], infos[1]));
body = bodyBuilder.build();
// bodyBuilder.add("a", "1");
// }
// else {
// Gson gson = new Gson();
// String jsonData = gson.toJson(bodyMap);
// body = RequestBody.create(jsonData, MediaType.parse(this.method));
// }
}
else if (StringUtils.isNotBlank(this.bodyString)) {
body = RequestBody.create(this.bodyString, MediaType.parse(this.method));
}
else {
body = new FormBody.Builder().build();
}
return body;
}
private void addHeader(Request.Builder builder) {
this.headerList.forEach(keyValue -> builder.addHeader(keyValue[0], keyValue[1]));
}
private void addTag(Request.Builder builder) {
if (this.tag != null) {
builder.tag(this.tag);
}
}
//endregion
//region post申请
/**
* post
*
* @param url
* @return Response
*/
public Response executePost(String url) {
try {
HttpUrl httpUrl = this.getHttpUrl(url);
RequestBody body = this.getRequestBody();
Request.Builder builder = new Request.Builder();
this.addHeader(builder);
this.addTag(builder);
builder.post(body);
builder.url(httpUrl);
/* builder.method(this.method,body); */
Request request = builder.build();
return this.client.build().newCall(request).execute();
}
catch (IOException e) {
throw new OkHttpException(e.getMessage());
}
}
public String executePostToString(String url) {
try (Response response = this.executePost(url)) {
return Objects.requireNonNull(response.body()).string();
}
catch (IOException e) {
throw new OkHttpException(e.getMessage());
}
}
public Map<String, Object> executePostToMap(String url) {
String strJson = this.executePostToString(url);
return getStringObjectMap(strJson);
}
public List<Object> executePostToList(String url) {
return executeList(() -> this.executePostToString(url));
}
//endregion
//region get申请
/**
* @param url
* @return Response
*/
public Response executeGet(String url) {
try {
HttpUrl httpUrl = this.getHttpUrl(url);
Request.Builder builder = new Request.Builder();
this.addHeader(builder);
this.addTag(builder);
builder.get();
builder.url(httpUrl);
Request request = builder.build();
return this.client.build().newCall(request).execute();
}
catch (IOException e) {
throw new OkHttpException(e.getMessage());
}
}
public String executeGetToString(String url) {
String resData;
try (Response response = this.executeGet(url)) {
resData = Objects.requireNonNull(response.body()).string();
}
catch (IOException e) {
throw new OkHttpException(e.getMessage());
}
return resData;
}
public Map<String, Object> executeGetToMap(String url) {
return executeMap(() -> this.executeGetToString(url));
}
public List<Object> executeGetList(String url) {
return executeList(() -> this.executeGetToString(url));
}
//endregion
//region put申请
/**
* @param url
* @return Response
*/
public Response executePut(String url) {
try {
HttpUrl httpUrl = this.getHttpUrl(url);
RequestBody body = this.getRequestBody();
Request.Builder builder = new Request.Builder();
this.addHeader(builder);
this.addTag(builder);
builder.put(body);
builder.url(httpUrl);
Request request = builder.build();
return this.client.build().newCall(request).execute();
}
catch (IOException e) {
throw new OkHttpException(e.getMessage());
}
}
public String executePutToString(String url) {
try (Response response = this.executePut(url)) {
return Objects.requireNonNull(response.body()).string();
}
catch (IOException e) {
throw new OkHttpException(e.getMessage());
}
}
public Map<String, Object> executePutToMap(String url) {
String strJson = this.executePutToString(url);
return getStringObjectMap(strJson);
}
public List<Object> executePutList(String url) {
return executeList(() -> this.executePutToString(url));
}
//endregion
//region webSocket
public static WebSocket webSocket(String url, WebSocketListener webSocketListener) {
OkHttpClient client = new OkHttpClient.Builder().retryOnConnectionFailure(true).build();
Request request = new Request.Builder().url(url).build();
client.dispatcher().cancelAll();//清理一次
return client.newWebSocket(request, webSocketListener);
}
//endregion
/**
* 使StringMap
*
* @param supplier
* @return java.util.Map<java.lang.String, java.lang.Object>
* @author LiuHuiYu
* Created DateTime 2021-04-16 9:08
*/
private static Map<String, Object> executeMap(Supplier<String> supplier) {
String strJson = supplier.get();
return getStringObjectMap(strJson);
}
/**
* 使StringList
*
* @param supplier
* @return java.util.List<java.lang.Object>
* @author LiuHuiYu
* Created DateTime 2021-04-16 9:09
*/
private static List<Object> executeList(Supplier<String> supplier) {
String strJson = supplier.get();
return getStringObjectList(strJson);
}
/**
* jsonMap
*
* @param strJson json
* @return java.util.Map<java.lang.String, java.lang.Object>
* @author LiuHuiYu
* Created DateTime 2021-03-30 9:04
*/
@NotNull
public static Map<String, Object> getStringObjectMap(String strJson) {
try {
Gson gson = new Gson();
Map<String, Object> resultMap = gson.fromJson(strJson, new TypeToken<Map<String, Object>>() {
}.getType());
return mapDoubleToInt(resultMap);
}
catch (JsonSyntaxException e) {
throw new OkHttpException(e.getMessage());
}
}
public static List<Object> getStringObjectList(String strJson) {
try {
Gson gson = new Gson();
List<Object> resultList = gson.fromJson(strJson, new TypeToken<List<Object>>() {
}.getType());
return listDoubleToInt(resultList);
}
catch (JsonSyntaxException e) {
throw new OkHttpException(e.getMessage());
}
}
public static Map<String, Object> mapDoubleToInt(Map<?, ?> resultMap) {
Map<String, Object> res = new HashMap<>(resultMap.size());
for (Object keyObj : resultMap.keySet()) {
String key = keyObj.toString();
if (resultMap.get(key) instanceof Double) {
Double value = (Double) resultMap.get(key);
if (value.intValue() == value) {
res.put(key, ((Double) resultMap.get(key)).intValue());
}
else {
res.put(key, resultMap.get(key));
}
}
else if (resultMap.get(key) instanceof List<?>) {
res.put(key, listDoubleToInt((List<?>) resultMap.get(key)));
}
else if (resultMap.get(key) instanceof Map<?, ?>) {
res.put(key, mapDoubleToInt((Map<?, ?>) resultMap.get(key)));
}
else {
res.put(key, resultMap.get(key));
}
}
return res;
}
public static List<Object> listDoubleToInt(List<?> list) {
List<Object> res = new ArrayList<>(list.size());
for (Object o : list) {
if (o instanceof Number) {
Double value = (Double) o;
if (value.intValue() == value) {
Object v = value.intValue();
res.add(v);
}
else {
res.add(value);
}
}
else if (o instanceof Map<?, ?>) {
res.add(mapDoubleToInt((Map<?, ?>) o));
}
else if (o instanceof List<?>) {
res.add(listDoubleToInt((List<?>) o));
}
else {
res.add(o);
}
}
return res;
}
//region https支持
private MyTrustManager mMyTrustManager;
public OkHttpUtil https() {
this.client.sslSocketFactory(createSSLSocketFactory(), mMyTrustManager)
.hostnameVerifier(new TrustAllHostnameVerifier());
return this;
}
private SSLSocketFactory createSSLSocketFactory() {
SSLSocketFactory ssfFactory = null;
try {
mMyTrustManager = new MyTrustManager();
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, new TrustManager[]{mMyTrustManager}, new SecureRandom());
ssfFactory = sc.getSocketFactory();
}
catch (Exception ignored) {
ignored.printStackTrace();
}
return ssfFactory;
}
//实现X509TrustManager接口
public class MyTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
//实现HostnameVerifier接口
private class TrustAllHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
/**
* okhttp
*
* @param certificate
* @return
*/
public OkHttpClient getTrusClient(InputStream certificate) {
X509TrustManager trustManager;
SSLSocketFactory sslSocketFactory;
try {
trustManager = trustManagerForCertificates(certificate);
SSLContext sslContext = SSLContext.getInstance("TLS");
//使用构建出的trustManger初始化SSLContext对象
sslContext.init(null, new TrustManager[]{trustManager}, null);
//获得sslSocketFactory对象
sslSocketFactory = sslContext.getSocketFactory();
}
catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
return new OkHttpClient.Builder()
.sslSocketFactory(sslSocketFactory, trustManager)
.build();
}
/**
* trustManager
*
* @param in
* @return trustManager
* @throws GeneralSecurityException
*/
private X509TrustManager trustManagerForCertificates(InputStream in)
throws GeneralSecurityException {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
//通过证书工厂得到自签证书对象集合
Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(in);
if (certificates.isEmpty()) {
throw new IllegalArgumentException("expected non-empty set of trusted certificates");
}
//为证书设置一个keyStore
char[] password = "password".toCharArray(); // Any password will work.
KeyStore keyStore = newEmptyKeyStore(password);
int index = 0;
//将证书放入keystore中
for (Certificate certificate : certificates) {
String certificateAlias = Integer.toString(index++);
keyStore.setCertificateEntry(certificateAlias, certificate);
}
// Use it to build an X509 trust manager.
//使用包含自签证书信息的keyStore去构建一个X509TrustManager
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(
KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, password);
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:"
+ Arrays.toString(trustManagers));
}
return (X509TrustManager) trustManagers[0];
}
private KeyStore newEmptyKeyStore(char[] password) throws GeneralSecurityException {
try {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream in = null; // By convention, 'null' creates an empty key store.
keyStore.load(null, password);
return keyStore;
}
catch (IOException e) {
throw new AssertionError(e);
}
}
//endregion
}

@ -0,0 +1,278 @@
package com.liuhuiyu.okhttp;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import javax.net.ssl.*;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.*;
import java.util.function.Supplier;
/**
* OkHttpUtil
*
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-07-06 10:05
*/
public class OkHttpUtil2 {
private final OkHttpClient.Builder client;
private final Request.Builder request;
private final HttpUrl.Builder httpUrl;
private final FormBody.Builder bodyBuilder;
private MethodModel methodModel;
private RequestBody requestBody;
public static OkHttpUtil2 create(String url) {
return new OkHttpUtil2(url);
}
private OkHttpUtil2(String url) {
this.httpUrl = Objects.requireNonNull(HttpUrl.parse(url)).newBuilder();
this.request = new Request.Builder();
this.client = new OkHttpClient.Builder();
this.bodyBuilder = new FormBody.Builder();
this.methodModel = MethodModel.GET;
}
/**
*
*
* @param name
* @param value
* @return com.liuhuiyu.okhttp.OkHttpUtil2
* @author LiuHuiYu
* Created DateTime 2021-07-06 10:20
*/
public OkHttpUtil2 addQueryParameter(String name, String value) {
this.httpUrl.addQueryParameter(name, value);
return this;
}
/**
* body
*
* @param name
* @param value
* @return com.liuhuiyu.okhttp.OkHttpUtil2
* @author LiuHuiYu
* Created DateTime 2021-07-06 11:06
*/
public OkHttpUtil2 addBody(String name, String value) {
this.bodyBuilder.add(name, value);
return this;
}
public OkHttpUtil2 setBody(String bodyString, String method) {
this.requestBody = RequestBody.create(bodyString, MediaType.parse(method));
return this;
}
/**
*
*
* @author LiuHuiYu
* Created DateTime 2021-07-06 10:33
*/
public enum MethodModel {
/**
* get
* Created DateTime 2021-07-06 10:40
*/
GET,
/**
* post
* Created DateTime 2021-07-06 10:40
*/
POST,
/**
* delete
* Created DateTime 2021-07-06 10:40
*/
DELETE,
/**
* put
* Created DateTime 2021-07-06 10:40
*/
PUT,
/**
* patch
* Created DateTime 2021-07-06 10:40
*/
PATCH,
/**
* head
* Created DateTime 2021-07-06 10:40
*/
HEAD,
;
public String getName() {
return this.name();
}
protected void builder(Request.Builder builder, RequestBody requestBody) {
builder.method(this.name(), requestBody);
}
}
//region 执行模式设定
public OkHttpUtil2 setMethodModel(OkHttpUtil2.MethodModel methodModel) {
this.methodModel = methodModel;
return this;
}
public OkHttpUtil2 get() {
this.methodModel = MethodModel.GET;
return this;
}
public OkHttpUtil2 post() {
this.methodModel = MethodModel.POST;
return this;
}
public OkHttpUtil2 delete() {
this.methodModel = MethodModel.DELETE;
return this;
}
public OkHttpUtil2 put() {
this.methodModel = MethodModel.PUT;
return this;
}
//endregion
//region 执行查询
public Response execute() {
this.request.url(httpUrl.build());
OkHttpClient client = this.client.build();
Request request = this.request.build();
if (this.requestBody == null) {
this.requestBody = this.bodyBuilder.build();
}
this.methodModel.builder(this.request, this.requestBody);
try {
return client.newCall(request).execute();
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
public String executeToString() {
try {
return Objects.requireNonNull(this.execute().body()).string();
}
catch (IOException e) {
throw new OkHttpException(e.getMessage());
}
}
public Map<String, Object> executeToMap() {
return executeMap(this::executeToString);
}
//endregion
//region 返回信息转换
/**
* 使StringMap
*
* @param supplier
* @return java.util.Map<java.lang.String, java.lang.Object>
* @author LiuHuiYu
* Created DateTime 2021-04-16 9:08
*/
private static Map<String, Object> executeMap(Supplier<String> supplier) {
String strJson = supplier.get();
return getStringObjectMap(strJson);
}
/**
* jsonMap
*
* @param strJson json
* @return java.util.Map<java.lang.String, java.lang.Object>
* @author LiuHuiYu
* Created DateTime 2021-03-30 9:04
*/
@NotNull
private static Map<String, Object> getStringObjectMap(String strJson) {
try {
Gson gson = new Gson();
Map<String, Object> resultMap = gson.fromJson(strJson, new TypeToken<Map<String, Object>>() {
}.getType());
return mapDoubleToInt(resultMap);
}
catch (JsonSyntaxException e) {
throw new OkHttpException(e.getMessage());
}
}
private static Map<String, Object> mapDoubleToInt(Map<?, ?> resultMap) {
Map<String, Object> res = new HashMap<>(resultMap.size());
for (Object keyObj : resultMap.keySet()) {
String key = keyObj.toString();
if (resultMap.get(key) instanceof Double) {
Double value = (Double) resultMap.get(key);
if (value.intValue() == value) {
res.put(key, ((Double) resultMap.get(key)).intValue());
}
else {
res.put(key, resultMap.get(key));
}
}
else if (resultMap.get(key) instanceof List<?>) {
res.put(key, listDoubleToInt((List<?>) resultMap.get(key)));
}
else if (resultMap.get(key) instanceof Map<?, ?>) {
res.put(key, mapDoubleToInt((Map<?, ?>) resultMap.get(key)));
}
else {
res.put(key, resultMap.get(key));
}
}
return res;
}
private static List<Object> listDoubleToInt(List<?> list) {
List<Object> res = new ArrayList<>(list.size());
for (Object o : list) {
if (o instanceof Number) {
Double value = (Double) o;
if (value.intValue() == value) {
Object v = value.intValue();
res.add(v);
}
else {
res.add(value);
}
}
else if (o instanceof Map<?, ?>) {
res.add(mapDoubleToInt((Map<?, ?>) o));
}
else if (o instanceof List<?>) {
res.add(listDoubleToInt((List<?>) o));
}
else {
res.add(o);
}
}
return res;
}
//endregion
}

@ -0,0 +1,178 @@
package com.liuhuiyu.okhttp;
import com.google.gson.Gson;
import lombok.extern.log4j.Log4j2;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-03-31 17:33
*/
public final class WebSocketManager {
boolean err = false;
boolean run = false;
/**
*
*
* @param url
* @param webSocketListener
* @return com.liuhuiyu.okhttp.WebSocketManager
* @author LiuHuiYu
* Created DateTime 2021-04-01 8:20
*/
public static WebSocketManager create(String url, WebSocketListener webSocketListener) {
return new WebSocketManager(url, webSocketListener);
}
String url;
WebSocketListener webSocketListener;
WebSocket webSocket;
int restartMaxNum = 5;
int restartNum = 0;
private WebSocketManager(String url, WebSocketListener webSocketListener) {
this.url = url;
this.webSocketListener = webSocketListener;
}
/**
*
* @author LiuHuiYu
* Created DateTime 2021-04-01 9:36
* @param num
*/
public void setRestartMaxNum(int num) {
this.restartMaxNum = Math.max(num, 0);
}
public boolean start() {
if (!run) {
this.restartNum = 0;
this.run(this.url);
return true;
}
else {
return false;
}
}
public boolean stop() {
if (run) {
this.webSocket.close(0, "主动关闭");
return true;
}
else {
return false;
}
}
/**
*
*
* @param message
* @author LiuHuiYu
* Created DateTime 2021-04-01 9:30
*/
public void send(String message) {
if (this.run) {
this.webSocket.send(message);
}
}
/**
*
*
* @param jsonObject json
* @author LiuHuiYu
* Created DateTime 2021-04-01 9:32
*/
public void sendJson(Object jsonObject) {
Gson gson = new Gson();
String json = gson.toJson(jsonObject);
this.send(json);
}
private void restart(){
if(this.restartMaxNum==0 || this.restartNum<this.restartMaxNum){
this.run(this.url);
}
}
private void run(String url) {
WebSocketListener selfWebSocketListener = new WebSocketListener() {
/**
*
* @author LiuHuiYu
* Created DateTime 2021-04-01 8:26
* @param webSocket webSocket
* @param code code
* @param reason reason
*/
@Override
public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
run = false;
webSocketListener.onClosed(webSocket, code, reason);
}
/**
*
* @author LiuHuiYu
* Created DateTime 2021-04-01 8:27
* @param webSocket webSocket
* @param code code
* @param reason reason
*/
@Override
public void onClosing(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
run = false;
webSocketListener.onClosing(webSocket, code, reason);
}
/**
*
* @author LiuHuiYu
* Created DateTime 2021-04-01 8:28
* @param webSocket webSocket
* @param t
* @param response response
*/
@Override
public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, @Nullable Response response) {
run = false;
err = true;
webSocketListener.onFailure(webSocket, t, response);
restart();//重新启动
}
@Override
public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
webSocketListener.onMessage(webSocket, text);
}
@Override
public void onMessage(@NotNull WebSocket webSocket, @NotNull ByteString bytes) {
webSocketListener.onMessage(webSocket, bytes);
}
/**
*
* @author LiuHuiYu
* Created DateTime 2021-04-01 8:29
* @param webSocket webSocket
* @param response response
*/
@Override
public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response) {
run = true;
webSocketListener.onOpen(webSocket, response);
}
};
this.webSocket = OkHttpUtil.webSocket(url, selfWebSocketListener);
}
}

@ -0,0 +1,20 @@
package com.liuhuiyu.okhttp;
import lombok.extern.log4j.Log4j2;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-07-06 10:44
*/
public class OkHttpUtil2Test {
@Test
public void t() {
OkHttpUtil2.MethodModel methodModel = OkHttpUtil2.MethodModel.DELETE;
System.out.println(methodModel.getName());
}
}

@ -0,0 +1,283 @@
package com.liuhuiyu.okhttp;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-01-12 19:11
*/
public class OkHttpUtilTest {
//region get测试
private String getUrl(String url) {
return "http://192.168.2.7:8006" + url;
}
@Test
public void testGetForLogin() {
String url = getUrl("/Authentication/Login");
OkHttpUtil okHttpUtil = OkHttpUtil.create();
okHttpUtil.addQueryParameter("userid", "ZH_Admin");
okHttpUtil.addQueryParameter("password", "UmVob21lLnpocHNAMjAyMA==");
Response res = okHttpUtil.executeGet(url);
String resString = okHttpUtil.executeGetToString(url);
Map<String, Object> resMap = okHttpUtil.executeGetToMap(url);
}
@Test
public void testTime() {
for (int i = 0; i < 100; i++) {
testGetForLogin();
}
}
private String getToken() {
String url = getUrl("/Authentication/Login");
OkHttpUtil okHttpUtil = OkHttpUtil.create();
okHttpUtil.addQueryParameter("userid", "ZH_Admin");
okHttpUtil.addQueryParameter("password", "UmVob21lLnpocHNAMjAyMA==");
Map<String, Object> resMap = okHttpUtil.executeGetToMap(url);
return resMap.get("token").toString();
}
@Test
public void testGet02() {
String url = getUrl("/U_RS_BASICINFO/GetUserInfoByManID");
String token = getToken();
OkHttpUtil okHttpUtil = OkHttpUtil.create();
okHttpUtil.headerAuthorizationByBearerToken(token);
okHttpUtil.headerAuthorizationByBearerToken(token);
Response res = okHttpUtil.executeGet(url);
String resString = okHttpUtil.executeGetToString(url);
Map<String, Object> resMap = okHttpUtil.executeGetToMap(url);
}
@Test
public void testGet03() {
String url = getUrl("/U_RS_BASICINFO/GetTreeUser");
String token = getToken();
OkHttpUtil okHttpUtil = OkHttpUtil.create();
okHttpUtil.addQueryParameter("dc", "ZH");
okHttpUtil.headerAuthorizationByBearerToken(token);
okHttpUtil.headerAuthorizationByBearerToken(token);
Response res = okHttpUtil.executeGet(url);
String resString = okHttpUtil.executeGetToString(url);
Map<String, Object> resMap = okHttpUtil.executeGetToMap(url);
}
//endregion
@Test
public void test() {
Map<String, Object> m1 = new HashMap<>();
Map<String, Object> m2 = new HashMap<>();
Map<String, Object> m3 = new HashMap<>();
List<Number> list1 = new ArrayList<>();
list1.add(1.0);
list1.add(2.0);
list1.add(3.0);
list1.add(4.1);
list1.add(5.0);
m3.put("m3_1", list1);
m3.put("m3_2", 2.0);
m3.put("m3_3", 3.0);
m2.put("m2_1", m3);
m2.put("m2_2", 5.0);
m2.put("m2_3", 6.1);
m1.put("m1_1", m2);
m1.put("m1_2", 4.0);
m1.put("m1_3", 1.1);
Map<String, Object> map = OkHttpUtil.mapDoubleToInt(m1);
}
@Test
public void testHeadGet() {
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiWkhfQWRtaW4iLCJleHAiOjE2MTI2MzM4MzAsImlzcyI6Imh0dHA6Ly8xOTIuMTY4LjIuNzo4MDA2IiwiYXVkIjoiaHR0cDovLzE5Mi4xNjguMi43OjgwMDYifQ.E6R3ei7UyUK3UnLAKbbLj9vIO9frdUtekT1UD4hHB6A";
String url = "http://192.168.2.7:8006/U_SYSTREE/GetUserTreeStr";
Map<String, Object> map = OkHttpUtil.create().headerAuthorizationByBearerToken(token).addQueryParameter("dc", "ZH").executeGetToMap(url);
if (Integer.parseInt(map.get("code").toString()) == 200) {
System.out.println(map.get("data"));
}
else {
System.out.println(map);
}
}
@Test
public void testPut01() {
String url = "http://192.168.2.7:8006/YJYAJX/QYSX";
String token = this.getToken();
String id = "b7af1ab0-0719-4e3e-93f0-c81dba07e9cb";
OkHttpUtil okHttpUtil = OkHttpUtil.create();
okHttpUtil.addQueryParameter("id", id);
okHttpUtil.headerAuthorizationByBearerToken(token);
Response response = okHttpUtil.executePut(url);
Map<String, Object> map = okHttpUtil.executePutToMap(url);
/*
{
"code": 200,
"msg": "",
"id": "b7af1ab0-0719-4e3e-93f0-c81dba07e9cb",
"JSR": "管理员",
"JSSJ": "2021-03-26 10:35:25"
}
*/
}
@Test
public void testPut02() {
String token = this.getToken();
String url = "http://192.168.2.7:8006/YJYAJX/UpDate";
Map<String, String> queryParameter = new HashMap<>(5);
queryParameter.put("id", "aa30fe55-746c-45a1-8bc1-ef91cd7566c9");
queryParameter.put("yjyaid", "96f3065d-8006-4758-a784-b12f147594b6");
queryParameter.put("title", "2021年122号台风天鸽应急预案");
queryParameter.put("type", "防风");
queryParameter.put("dz", "200002");
OkHttpUtil okHttpUtil = OkHttpUtil.create();
okHttpUtil.headerAuthorizationByBearerToken(token);
for (String key : queryParameter.keySet()) {
okHttpUtil.addBody(key, queryParameter.get(key));
}
okHttpUtil.setMethod(OkHttpUtil.MEDIA_TYPE_APPLICATION_JSON_UTF_8);
Response response = okHttpUtil.executePut(url);
Map<String, Object> map = okHttpUtil.executePutToMap(url);
}
static CountDownLatch countDownLatch = new CountDownLatch(1);
@Test
public void testWebSocket() throws InterruptedException {
String url = "ws://localhost:8111/ws1";
WebSocketListener webSocketListener = new WebSocketListener() {
@Override
public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
super.onClosed(webSocket, code, reason);
}
@Override
public void onClosing(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
super.onClosing(webSocket, code, reason);
}
@Override
public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, @Nullable Response response) {
super.onFailure(webSocket, t, response);
}
@Override
public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
super.onMessage(webSocket, text);
}
@Override
public void onMessage(@NotNull WebSocket webSocket, @NotNull ByteString bytes) {
super.onMessage(webSocket, bytes);
}
@Override
public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response) {
super.onOpen(webSocket, response);
}
};
OkHttpUtil.webSocket(url, webSocketListener);
countDownLatch.await();
}
@Test
public void testHttps() {
String url = "http://10.0.21.227/WebService.asmx/GetToken";
OkHttpUtil okHttpUtil = OkHttpUtil.create();
okHttpUtil.setMethod("application/json");
okHttpUtil.addBody("userName", "zhAdmin");
okHttpUtil.addBody("userPwd", "zh@2021");
okHttpUtil.https();
Map<String, Object> map = okHttpUtil.executePostToMap(url);
String token = map.get("token").toString();
String url2 = "https://safety-vsmapi.geg.com.cn/WebService.asmx/GetDataRequest";
OkHttpUtil okHttpUtil2 = OkHttpUtil.create();
okHttpUtil2.setMethod("application/json");
String[] names = {"", "巫中建"};//"巫中建";
String name = names[0];//"巫中建";
String[] ids = {"", "51162119900401647X", "510231197208103576", "511621"};
String idCards = ids[3];//""510231197208103576";
String pageSize = "5";
okHttpUtil2.setBodyString("{\n" +
" \"token\":\"" + token + "\",\n" +
" \"dataType\":\"C_VENUSER\",\n" +
" \"company\":\"ZHP\",\n" +
" \"pageSize\":" + pageSize + ",\n" +
" \"data\":{" +
" \"code\":\"\"," +
" \"name\":\"" + name + "\"," +
" \"idcards\":\"" + idCards + "\"," +
" \"suCode\":\"\"," +
" \"mobile\":\"\"," +
" \"suName\":\"\"" +
" }\n" +
"}\n");
// String info=okHttpUtil.executePostToString(url);
Map<String, Object> map2 = okHttpUtil2.executePostToMap(url2);
}
@Test
public void testHttpsGet() {
String url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13614600169";
OkHttpUtil okHttpUtil = OkHttpUtil.create();
// okHttpUtil.https();
String info = okHttpUtil.executeGetToString(url);
}
@Test
public void testHttpsGet2(){
String url="https://127.0.0.1:8125/api/v1.0/system/authentication/login_do";
Map<String, String> queryParameter = new HashMap<>(2);
String account="ZH_AF_Admin";
String password="UmVob21lLnpoYWZAMjEwNg==";
queryParameter.put("account", account);
queryParameter.put("password", password);
OkHttpUtil okHttpUtil = OkHttpUtil.create();
for (String key : queryParameter.keySet()) {
okHttpUtil.addQueryParameter(key, queryParameter.get(key));
}
okHttpUtil.https();
Map<String, Object> map = okHttpUtil.executeGetToMap(url);
}
@Test
public void testFor() {
int num = 100_000;
int n = 0;
long m = 0;
for (int k = 0; k <= 50; k++) {
for (int j = 0; j <= 10_000; j++) {
n = 0;
for (int i = 0; i <= num; i++) {
n += i * 2 - 1;
m++;
}
}
}
System.out.println(m + "," + n);
}
@Test
public void testVoid() {
String id = "f592b1eac6fb4d1091041bf1dab89aa2";
String url = "http://10.19.0.114:8354/partnerRequest/hik8700/queryHls.shtml";
String str = OkHttpUtil.create().addQueryParameter("indexCode", id).executeGetToString(url);
}
}

@ -0,0 +1,75 @@
//package com.liuhuiyu.okhttp
//
//import org.junit.Test
//
///**
// * @author LiuHuiYu
// * @version v1.0.0.0
// * Created DateTime 2021-04-01 10:00
// */
//class OkHttpUtilTestKt {
// @Test
// fun test01() {
// print("this is my first Kotlin.\n")
// }
//
// private fun getUrl(url: String): String {
// return "http://192.168.2.7:8006$url"
// }
//
// @Test
// fun testGetForLogin() {
// val url: String = this.getUrl("/Authentication/Login")
// val okHttpUtil = OkHttpUtil.create()
// okHttpUtil.addQueryParameter("userid", "ZH_Admin")
// okHttpUtil.addQueryParameter("password", "UmVob21lLnpocHNAMjAyMA==")
// val res = okHttpUtil.executeGet(url)
// val resString = okHttpUtil.executeGetToString(url)
// val resMap = okHttpUtil.executeGetToMap(url)
// }
//
// @Test
// fun testTime() {
// for (i in 0..99) {
// testGetForLogin()
// }
// }
//
// private fun getToken(): String {
// val url = getUrl("/Authentication/Login")
// val okHttpUtil = OkHttpUtil.create()
// okHttpUtil.addQueryParameter("userid", "ZH_Admin")
// okHttpUtil.addQueryParameter("password", "UmVob21lLnpocHNAMjAyMA==")
// val resMap = okHttpUtil.executeGetToMap(url)
// return resMap["token"].toString()
// }
//
// @Test
// fun testGet02() {
// val url = getUrl("/U_RS_BASICINFO/GetUserInfoByManID")
// val token = getToken()
// val okHttpUtil = OkHttpUtil.create()
// okHttpUtil.headerAuthorizationByBearerToken(token)
// okHttpUtil.headerAuthorizationByBearerToken(token)
// val res = okHttpUtil.executeGet(url)
// val resString = okHttpUtil.executeGetToString(url)
// val resMap = okHttpUtil.executeGetToMap(url)
// }
//
// @Test
// fun testFor() {
// val num = 100000
// var n = 0
// var m: Long = 0
// for (k in 0..50) {
// for (j in 0..10000) {
// n = 0
// for (i in 0..num) {
// n += i * 2 - 1
// m++
// }
// }
// }
// println("$m,$n")
// }
//}

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springcloud</groupId>
<artifactId>utils</artifactId>
<version>1.0.0</version>
<name>utils</name>
<packaging>pom</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<modules>
<module>jpa</module>
<module>okhttp3util</module>
<module>spring-util</module>
<module>test</module>
<module>util</module>
<module>web</module>
</modules>
<!--项目部署和发布的话需要配置: 在pom.xml中添加 distributionManagement 配置-->
<!-- maven仓库配置 deploy时可推送到配置的仓库中 -->
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>Nexus Releases Repository Pro</name>
<url>http://47.242.184.139:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>Nexus Snapshots Repository Pro</name>
<url>http://47.242.184.139:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>

@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liuhuiyu</groupId>
<artifactId>spring-util</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>liuhuiyu-spring-util</name>
<description>spring个人工具类</description>
<url>https://github.com/liuhuiyu2004/utils</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>19.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!--项目部署和发布的话需要配置: 在pom.xml中添加 distributionManagement 配置-->
<!-- maven仓库配置 deploy时可推送到配置的仓库中 -->
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>Nexus Releases Repository Pro</name>
<url>http://47.242.184.139:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>Nexus Snapshots Repository Pro</name>
<url>http://47.242.184.139:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>

@ -0,0 +1,54 @@
package com.liuhuiyu.spring_util;
import lombok.extern.log4j.Log4j2;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* @Bean public SpringUtil springUtilBean(){ return new SpringUtil(); }
*
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2020-06-24 14:55
*/
@Log4j2
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
if (SpringUtil.applicationContext == null) {
SpringUtil.applicationContext = applicationContext;
}
log.info("===ApplicationContext配置成功,在普通类可以通过调用SpringUtils.getAppContext()获取applicationContext对象,applicationContext=%1$s==={}", SpringUtil.applicationContext);
log.info("--------------------------------------------------------------");
}
//获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
//通过name获取 Bean.
public static @NotNull Object getBean(String name) {
return getApplicationContext().getBean(name);
}
//通过class获取Bean.
public static <T> @NotNull T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
/**
* name,ClazzBean
*
* @param name
* @param clazz class
* @param <T>
* @return
*/
public static <T> @NotNull T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}

@ -0,0 +1,24 @@
package com.liuhuiyu.spring_util.run_timer;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-03-02 11:56
*/
@Aspect
@Component
@Log4j2
public class RunTimeAspect {
@Around(value = "@annotation(timer)")
public Object logAround(ProceedingJoinPoint pjp, RunTimer timer) throws Throwable {
TimerUtil tu = new TimerUtil();
return tu.runTime(pjp, timer);
}
}

@ -0,0 +1,15 @@
package com.liuhuiyu.spring_util.run_timer;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
*
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-03-02 11:22
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface RunTimer {
String explain() default "";
}

@ -0,0 +1,68 @@
package com.liuhuiyu.spring_util.run_timer;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-03-02 11:23
*/
@Log4j2
public class TimerUtil {
public void runTime(Method m, Object obj) throws InvocationTargetException, IllegalAccessException {
long start = System.currentTimeMillis();
m.invoke(obj);
long end = System.currentTimeMillis();
log.info("{}执行时间:{}", m.getName(), (end - start));
}
public Object runTime(ProceedingJoinPoint pjp, RunTimer runTimer) throws Throwable {
Signature sig = pjp.getSignature();
MethodSignature msig;// = null;
if (!(sig instanceof MethodSignature)) {
throw new IllegalArgumentException("该注解只能用于方法");
}
msig = (MethodSignature) sig;
Object target = pjp.getTarget();
Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
long start = System.currentTimeMillis();
Object res = pjp.proceed();
long end = System.currentTimeMillis();
log.info("{}.{} {}执行时间:{}", currentMethod.getDeclaringClass().getName(), currentMethod.getName(), runTimer.explain(), (end - start));
return res;
}
public void getTime() {
// 获取当前类型名字
String className = Thread.currentThread().getStackTrace()[2].getClassName();
System.out.println("current className(expected): " + className);
try {
Class<?> c = Class.forName(className);
Object obj = c.newInstance();
Method[] methods = c.getDeclaredMethods();
for (Method m : methods) {
// 判断该方法是否包含Timer注解
if (m.isAnnotationPresent(RunTimer.class)) {
m.setAccessible(true);
long start = System.currentTimeMillis();
// 执行该方法
m.invoke(obj);
long end = System.currentTimeMillis();
System.out.println(m.getName() + "() time consumed: " + (end - start) + "\\\\n");
}
}
}
catch (ReflectiveOperationException e) {
log.error(e.toString());
}
}
}

@ -0,0 +1,3 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.liuhuiyu.spring_util.SpringUtil,\
com.liuhuiyu.spring_util.run_timer.RunTimeAspect

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liuhuiyu</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>测试基础类</name>
<!-- FIXME change it to the project's website -->
<url>http://www.liuhuiyu.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- <dependency>-->
<!-- <groupId>junit</groupId>-->
<!-- <artifactId>junit</artifactId>-->
<!-- <version>4.11</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<!--region web 引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<!--endregion -->
<!--region 注解-->
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>19.0.0</version>
</dependency>
<!--endregion -->
</dependencies>
<!--项目部署和发布的话需要配置: 在pom.xml中添加 distributionManagement 配置-->
<!-- maven仓库配置 deploy时可推送到配置的仓库中 -->
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>Nexus Releases Repository Pro</name>
<url>http://47.242.184.139:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>Nexus Snapshots Repository Pro</name>
<url>http://47.242.184.139:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

@ -0,0 +1,103 @@
package com.liuhuiyu.test;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.io.UnsupportedEncodingException;
import java.util.function.Consumer;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2020-07-21 9:26
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc//网站测试
@Log4j2
public class BaseControllerTest {
@Autowired
protected WebApplicationContext wac;
protected MockMvc mvc;
@Before()
public void setup() {
mvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
protected void printHtml(@NotNull ResultActions resultActions) throws UnsupportedEncodingException {
this.responseInfo(resultActions);
this.printInfo("html信息");
log.info(resultActions.andReturn().getResponse().getContentAsString());
this.printInfo("html end");
}
protected void printJsonResult(@NotNull ResultActions resultActions) throws UnsupportedEncodingException {
this.responseInfo(resultActions);
this.printInfo("json信息");
log.info(resultActions.andReturn().getResponse().getContentAsString());
this.printInfo("json end");
}
/**
* Response
* Created DateTime 2021-02-23 9:10
*
* @param resultActions ResultActions
* @author LiuHuiYu
*/
private void responseInfo(@NotNull ResultActions resultActions) throws UnsupportedEncodingException {
String json = resultActions.andReturn().getResponse().getContentAsString();
int size = json.getBytes().length;
this.printInfo("访问地址");
log.info("{}{}", resultActions.andReturn().getRequest().getLocalAddr(), resultActions.andReturn().getRequest().getRequestURI());
this.printInfo("参数信息");
for (String key : resultActions.andReturn().getRequest().getParameterMap().keySet()) {
log.info("{}:{}", key, resultActions.andReturn().getRequest().getParameterMap().get(key));
}
this.printInfo("字节Byte" + size);
this.printInfo("流量bit" + (size * 8));
}
protected void printInfo(String info) {
log.info("/******************************* " + info + " *******************************/");
}
@SneakyThrows
protected MockHttpSession adminLoginSession(String adminAttribute, Object value) {
return setSession((session) -> session.setAttribute(adminAttribute, value));
}
@SneakyThrows
protected MockHttpSession userLoginSession(String userAttribute, Object value) {
return setSession((session) -> session.setAttribute(userAttribute, value));
}
/**
* session
* session.setAttribute(admin,admin);//对session进行登陆设置
* Created DateTime 2021-02-23 9:19
*
* @param consumer session
* @author LiuHuiYu
*/
@SneakyThrows
protected MockHttpSession setSession(Consumer<MockHttpSession> consumer) {
MockHttpSession session = new MockHttpSession();
consumer.accept(session);
return session;
}
}

@ -0,0 +1,62 @@
package com.liuhuiyu.test;
import lombok.extern.log4j.Log4j2;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2020-07-06 10:43
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Log4j2
public abstract class BaseServiceTest {
@Before
public void login() {
}
@After
public void after() {
}
protected void printList(@NotNull List<?> list) {
for (Object obj : list) {
log.info(obj.toString());
}
log.info("size = " + list.size());
}
protected void printObject(Object obj) {
if (obj == null) {
log.info("Obj=null");
} else {
log.info(obj.toString());
}
}
protected void printPageImpl(@NotNull Page<?> pageImpl) {
log.info("总记录数:" + pageImpl.getTotalElements());
log.info("当前页号索引:" + pageImpl.getNumber());
log.info("每页数据:" + pageImpl.getSize());
log.info("当前页记录数:" + pageImpl.getNumberOfElements());
log.info("当前页号:" + (pageImpl.getNumber() + 1));
log.info("总页数:" + pageImpl.getTotalPages());
printList(pageImpl.getContent());
}
}

@ -0,0 +1,20 @@
package com.liuhuiyu.test;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}

@ -0,0 +1,41 @@
# util 简介
My util, Common tools collection
工具集合类
# 包说明
|包|说明|
|---|---|
|asserts|断言|
|constant.enums|常量枚举|
|exception|异常|
|spring|spring工具|
|web|web工具|
#导入方法
1.将项目maven打jar包
Maven->Util->生命周期->package
2.在要引用的项目中建立lib文件夹
3.复制jar包到lib文件夹
4.在pom.xml中
dependencies 标签中加入
~~~
<dependency>
<groupId>com.liuhuiyu</groupId>
<artifactId>util</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/util-1.0-SNAPSHOT.jar</systemPath>
</dependency>
~~~
build 标签中加入
~~~
<resources>
<resource>
<directory>lib</directory>
<targetPath>/BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</resources>
~~~
本地仓库安装
mvn install:install-file -Dfile=util-1.0.0.jar -DgroupId=com.liuhuiyu -DartifactId=util -Dversion=1.0-SNAPSHOT -Dpackaging=jar

@ -0,0 +1,269 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liuhuiyu</groupId>
<artifactId>util</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>liuhuiyu-util</name>
<description>个人工具类</description>
<url>https://github.com/liuhuiyu2004/util</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--项目部署和发布的话需要配置: 在pom.xml中添加 distributionManagement 配置-->
<!-- maven仓库配置 deploy时可推送到配置的仓库中 -->
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>Nexus Releases Repository Pro</name>
<url>http://47.242.184.139:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>Nexus Snapshots Repository Pro</name>
<url>http://47.242.184.139:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<!--region 配置-->
<licenses>
<license>
<name>MIT License</name>
<url>https://github.com/everitoken/evt4j/blob/master/LICENSE</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<url>https://github.com/liuhuiyu2004/util</url>
<connection>scm:git:git@github.com:liuhuiyu2004/util</connection>
<developerConnection>scm:git:git@github.com:liuhuiyu2004/util</developerConnection>
</scm>
<developers>
<developer>
<name>liuhuiyu</name>
<email>liuhuiyu2004@outlook.com</email>
</developer>
</developers>
<!--endregion-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--region 测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<!--endregion -->
<!--region web 引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--endregion -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--region 注解-->
<!-- https://mvnrepository.com/artifact/org.jetbrains/annotations -->
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>19.0.0</version>
</dependency>
<!--endregion -->
<!--region thymeleaf 模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--endregion -->
<!--region 使thymeleaf不强制闭合标签 -->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
<!--endregion -->
<!--region json jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!--endregion-->
<!--region 自定义属性支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--endregion -->
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.codec</artifactId>
<version>1.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
<!-- <dependency>-->
<!-- <groupId>org.apache.commons</groupId>-->
<!-- <artifactId>commons-io</artifactId>-->
<!-- <version>1.3.2</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<!--region lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<optional>true</optional>
</dependency>
<!--endregion -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>false</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<failOnError>false</failOnError>
<doclint>none</doclint>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>org.junit.platform</groupId>-->
<!-- <artifactId>junit-platform-surefire-provider</artifactId>-->
<!-- <version>1.0.3</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.junit.jupiter</groupId>-->
<!-- <artifactId>junit-jupiter-engine</artifactId>-->
<!-- <version>5.0.3</version>-->
<!-- </dependency>-->
<!-- </dependencies>-->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

@ -0,0 +1,74 @@
package com.liuhuiyu.util;
/**
* 使
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-01-28 8:23
*/
public class ObjectUtil {
public static boolean isNull(Object o) {
return o == null;
}
public static boolean equals(Object o1, Object o2) {
if (o1 == null) {
return o2 == null;
}
return o1.equals(o2);
}
public static Integer parseInteger(Object o) {
return parseInteger(o, 0, 0);
}
public static Integer parseInteger(Object o, Integer defValue, Integer nullValue) {
if (isNull(o)) {
return nullValue;
}
if (o instanceof Integer) {
return (Integer) o;
} else {
try {
return Integer.parseInt(o.toString());
} catch (Exception ex) {
return defValue;
}
}
}
public static Long parseLong(Object o) {
return parseLong(o.toString(), 0L, 0L);
}
public static Long parseLong(Object o, Long defValue, Long nullValue) {
if (isNull(o)) {
return nullValue;
}
if (o instanceof Long) {
return (Long) o;
} else {
try {
return Long.parseLong(o.toString());
} catch (Exception ex) {
return defValue;
}
}
}
public static String parseString(Object o) {
return parseString(o, "", "");
}
public static String parseString(Object o, String defValue, String nullValue) {
if (isNull(o)) {
return nullValue;
} else {
if (o instanceof String) {
return (String) o;
} else {
return o.toString();
}
}
}
}

@ -0,0 +1,102 @@
package com.liuhuiyu.util.asserts;
import com.liuhuiyu.util.constant.enums.ResultEnum;
import com.liuhuiyu.util.exception.LhyException;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2020-09-10 8:41
*/
public class LhyAssert {
//region assertTrue
public static void assertTrue(boolean value, RuntimeException exception) {
if (!value) {
throw exception;
}
}
public static void assertTrue(boolean value, String message) {
assertTrue(value, new RuntimeException(message));
}
/**
*
*
* @param value
* @param resultEnum
*/
public static void assertTrue(boolean value, ResultEnum resultEnum) {
if (!value) {
throw new LhyException(resultEnum);
}
}
//endregion
//region assertFalse
public static void assertFalse(boolean value, String message) {
assertTrue(!value, message);
}
public static void assertFalse(boolean value, RuntimeException exception) {
assertTrue(!value, exception);
}
//endregion
//region assertNotNull
public static void assertNotNull(Object object, RuntimeException exception) {
assertTrue(object != null, exception);
}
/**
*
*
* @param object
*/
public static void assertNotNull(Object object, String message) {
assertTrue(object != null, message);
}
/**
*
*
* @param object
* @param resultEnum
*/
public static void assertNotNull(Object object, ResultEnum resultEnum) {
assertTrue(object != null, resultEnum);
}
//endregion
//region assertNull
/**
*
*
* @param object
*/
public static void assertNull(Object object, RuntimeException exception) {
assertTrue(object == null, exception);
}
/**
*
*
* @param object
*/
public static void assertNull(Object object, String message) {
assertTrue(object == null, message);
}
/**
*
*
* @param object
* @param resultEnum
*/
public static void assertNull(Object object, ResultEnum resultEnum) {
assertTrue(object == null, resultEnum);
}
//endregion
}

@ -0,0 +1,33 @@
package com.liuhuiyu.util.constant.enums;
/**
*
*
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2020-07-08 11:45
*/
public enum ResultEnum {
/**
*
*/
SUCCESS(0, "成功"),
CUSTOM_ERROR(-1,"自定义错误"),
;
private Integer code;
private String message;
ResultEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
}

@ -0,0 +1,25 @@
package com.liuhuiyu.util.date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
*
*
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-05-22 14:39
*/
public class LocalDateUtil {
public static String dateToString(LocalDate date, String format) {
return date.format(DateTimeFormatter.ofPattern(format));
}
public static String dateToString(LocalDateTime date, String format) {
return date.format(DateTimeFormatter.ofPattern(format));
}
public static String dateToString(LocalTime date, String format) {
return date.format(DateTimeFormatter.ofPattern(format));
}
}

@ -0,0 +1,54 @@
package com.liuhuiyu.util.exception;
import com.liuhuiyu.util.constant.enums.ResultEnum;
import org.jetbrains.annotations.NotNull;
/**
*
*
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2020-06-23 13:27
*/
public class LhyException extends RuntimeException {
private Integer errId;
/*
public ArchivesManagementException(Integer code, String message) {
super(message);
this.code = code;
}
*/
public LhyException(@NotNull ResultEnum resultEnum) {
super(resultEnum.getMessage());
this.errId = resultEnum.getCode();
}
public LhyException(@NotNull ResultEnum resultEnum, String message) {
super(resultEnum.getMessage() + ":" + message);
this.errId = resultEnum.getCode();
}
public LhyException(String message, @NotNull ResultEnum resultEnum) {
super(message + resultEnum.getMessage());
this.errId = resultEnum.getCode();
}
public LhyException(String message, @NotNull ResultEnum resultEnum, String message1) {
super(message + resultEnum.getMessage() + message1);
this.errId = resultEnum.getCode();
}
public LhyException(String message) {
super(message);
this.errId = 0;
}
public Integer getErrId() {
return errId;
}
public void setErrId(Integer errId) {
this.errId = errId;
}
}

@ -0,0 +1,101 @@
package com.liuhuiyu.util.exception;
import lombok.extern.log4j.Log4j2;
import java.util.concurrent.Callable;
/**
*
*
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-07-05 15:44
*/
@Log4j2
public class RetryUtil {
/**
*
*
* @param num
* @param callFunctions
* @author LiuHuiYu
* Created DateTime 2021-07-05 16:58
*/
public static <R> R retry(int num, Callable<R> callFunctions) {
return retry(num, false, callFunctions);
}
/**
*
*
* @param num ()
* @param callFunctions
* @param failedCall
* @author LiuHuiYu
* Created DateTime 2021-07-05 16:58
*/
public static <R> R retry(int num, Callable<R> callFunctions, Runnable failedCall) {
return retry(num, true, callFunctions, failedCall);
}
/**
*
*
* @param callFunctions
* @param failedCall
* @author LiuHuiYu
* Created DateTime 2021-07-05 16:58
*/
public static <R> R retry(Callable<R> callFunctions, Runnable failedCall) {
return retry(2, true, callFunctions, failedCall);
}
/**
*
*
* @param num
* @param callFunctions [0]>0 1 1
* @author LiuHuiYu
* Created DateTime 2021-07-05 17:13
*/
public static <R> R retry(int num, Callable<R> callable, Runnable... callFunctions) {
return retry(num, true, callable, callFunctions);
}
/**
*
*
* @param num
* @param callFunctions [0]>0 1 1
* @author LiuHuiYu
* Created DateTime 2021-07-05 17:13
*/
public static <R> R retry(int num, boolean loop, Callable<R> callable, Runnable... callFunctions) {
int pointer = -1;
try {
for (int i = 0; i < num - 1; i++) {
try {
return callable.call();
}
catch (Exception e) {
if (pointer > callFunctions.length) {
if (loop) {
pointer = 0;
}
else {
continue;
}
}
else {
pointer++;
}
callFunctions[pointer].run();
}
}
return callable.call();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
}

@ -0,0 +1,18 @@
package com.liuhuiyu.util.functional;
import java.util.Map;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-03-23 16:49
*/
@FunctionalInterface
public interface MapToT<T> {
/**
* Object T
* @param map Map<String,Object>
* @return T
*/
T mapToT(Map<String,Object> map);
}

@ -0,0 +1,16 @@
package com.liuhuiyu.util.functional;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-03-23 16:48
*/
@FunctionalInterface
public interface ObjectToT<T> {
/**
* Object T
* @param o Object
* @return T
*/
T objectToT(Object o);
}

@ -0,0 +1,7 @@
/**
*
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-03-23 16:47
*/
package com.liuhuiyu.util.functional;

@ -0,0 +1,249 @@
package com.liuhuiyu.util.list;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* List
*
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-04-14 10:59
*/
public class ListUtil {
public static final String ARRAY_HEAD = "[";
/**
* List
*
* @param obj List array
* @return java.util.List<java.lang.Object>
* @author LiuHuiYu
* Created DateTime 2021-07-17 9:26
*/
public static List<Object> objectToList(Object obj) {
return objectToListT(obj, (o) -> o);
// if (obj == null) {
// return new ArrayList<>(0);
// }
// else if (obj instanceof List<?>) {
// List<Object> resList;
// List<?> list = (List<?>) obj;
// resList = new ArrayList<>(list.size());
// resList.addAll(list);
// return resList;
// }
// else if (obj instanceof Collection) {
// List<Object> resList;
// Collection<?> list = (Collection<?>) obj;
// resList = new ArrayList<>(list.size());
// resList.addAll(list);
// return resList;
// }
// else if (obj instanceof Object[]) {
// Object[] objects = (Object[]) obj;
// List<Object> resList = new ArrayList<>(objects.length);
// Collections.addAll(resList, objects);
// return resList;
// }
// else if (obj.getClass().getName().indexOf(ARRAY_HEAD) == 0) {
// return new ArrayList<>(0);
// }
// else {
// throw new RuntimeException("无法转换");
// }
}
/**
* List
*
* @param obj List
* @param function
* @return java.util.List<T>
* @author LiuHuiYu
* Created DateTime 2021-07-17 9:26
*/
public static <T> List<T> objectToListT(Object obj, Function<Object, T> function) {
if (obj == null) {
return new ArrayList<>(0);
}
else if (obj instanceof List<?>) {
List<T> resList;
List<?> list = (List<?>) obj;
resList = new ArrayList<>(list.size());
list.forEach((o) -> {
T t = function.apply(o);
if (t != null) {
resList.add(t);
}
});
return resList;
}
else if (obj instanceof Collection) {
List<T> resList;
Collection<?> list = (Collection<?>) obj;
resList = new ArrayList<>(list.size());
list.forEach((o) -> {
T t = function.apply(o);
if (t != null) {
resList.add(t);
}
});
return resList;
}
else if (obj instanceof Object[]) {
Object[] objects = (Object[]) obj;
if (objects.length == 0) {
return new ArrayList<>(0);
}
else {
List<T> resList = new ArrayList<>(objects.length);
for (Object object : objects) {
T t = function.apply(object);
if (t != null) {
resList.add(function.apply(t));
}
}
return resList;
}
}
else if (obj.getClass().getName().indexOf(ARRAY_HEAD) == 0) {
return new ArrayList<>(0);
}
else {
throw new RuntimeException("无法转换");
}
}
/**
* t
*
* @param t
* @param list
* @param comparable
* @return java.lang.Integer
* @author LiuHuiYu
* Created DateTime 2021-05-19 22:48
*/
public static <T, U> Integer getIndex(T t, U[] list, BiFunction<T, U, Boolean> comparable) {
for (int i = 0; i < list.length; i++) {
if (comparable == null) {
if (t.equals(list[i])) {
return i;
}
}
else if (comparable.apply(t, list[i])) {
return i;
}
}
return -1;
}
/**
*
*
* @param list
* @param sorts
* @param compare
* @return java.util.List<T>
*/
public static <T, L> List<T> getSortList(List<T> list, L[] sorts, BiFunction<T, L, Boolean> compare) {
return getSortList(list, sorts, compare, true, true, true);
}
/**
*
*
* @param list
* @param sorts
* @param compare
* @param asc
* @param notMatchedPostfix
* @param notMatchedAsc
* @return java.util.List<T>
* @author LiuHuiYu
* Created DateTime 2021-05-25 19:13
*/
public static <T, L> List<T> getSortList(List<T> list, L[] sorts, BiFunction<T, L, Boolean> compare, Boolean asc, Boolean notMatchedPostfix, Boolean notMatchedAsc) {
List<T> resList = new ArrayList<>(list.size());
list.stream().sorted((a1, a2) -> {
Integer index1 = ListUtil.getIndex(a1, sorts, compare);
Integer index2 = ListUtil.getIndex(a2, sorts, compare);
if (index1 == -1 && index2 == -1) {
return notMatchedPostfix ? 1 : -1;
}
else if (index1 == -1) {
return notMatchedAsc ? 1 : -1;
}
else if (index2 == -1) {
return notMatchedAsc ? -1 : 1;
}
else if (asc) {
return index1.compareTo(index2);
}
else {
return index2.compareTo(index1);
}
}).forEach(resList::add);
return resList;
}
/**
* List
*
* @param tList list
* @param function
* @return java.util.List<R>
* @author LiuHuiYu
* Created DateTime 2021-06-04 16:08
*/
public static <T, R> List<R> listToList(List<T> tList, Function<T, R> function) {
List<R> rList = new ArrayList<>(0);
for (T item : tList) {
rList.add(function.apply(item));
}
return rList;
}
/**
* list
*
* @param list List
* @return T[]
* @author LiuHuiYu
* Created DateTime 2021-07-17 9:17
*/
public static <T> T[] listToArray(List<T> list) {
if (list.size() == 0) {
@SuppressWarnings("unchecked") final T[] a1 = (T[]) new Object[0];
return a1;
}
Class<?> z = list.get(0).getClass();
@SuppressWarnings("unchecked") final T[] a = (T[]) Array.newInstance(z, list.size());
for (int i = 0; i < list.size(); i++) {
a[i] = list.get(i);
}
return a;
}
/**
* List
*
* @param array
* @return java.util.List<T>
* @author LiuHuiYu
* Created DateTime 2021-07-17 9:17
*/
public static <T> List<T> arrayToList(T[] array) {
List<T> list = new ArrayList<>(array.length);
Collections.addAll(list, array);
return list;
}
}

@ -0,0 +1,485 @@
package com.liuhuiyu.util.map;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.liuhuiyu.util.date.LocalDateUtil;
import org.apache.commons.beanutils.BeanUtils;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-01-20 14:53
*/
public class MapUtil {
boolean throwException = false;
public boolean isThrowException() {
return throwException;
}
public void setThrowException(boolean throwException) {
this.throwException = throwException;
}
//region 静态方法
/**
* MapMap<String, Object>
*
* @param obj Map
* @return Map<String, Object>
*/
public static Map<String, Object> mapObjectToStringKeyMap(Object obj) {
if (obj == null) {
return null;
}
else if (obj instanceof Map<?, ?>) {
Map<?, ?> map = (Map<?, ?>) obj;
Map<String, Object> resMap = new HashMap<>(map.size());
map.forEach((k, v) -> resMap.put(k.toString(), v));
return resMap;
}
else {
return new HashMap<>(0);
}
}
private Object getMapObjectValue(Map<String, Object> map, String key) {
return getMapObjectValue(map, key, null);
}
private Object getMapObjectValue(Map<String, Object> map, String key, Object defValue) {
return map.getOrDefault(key, defValue);
}
public static String getMapStringValue(Map<String, Object> map, String key) {
return getMapStringValue(map, key, "");
}
/**
* (key )
*
* @param map map
* @param key key
* @return
*/
public static String getMapStringValue(Map<String, Object> map, String key, String defValue) {
if (map.containsKey(key)) {
Object obj = map.get(key);
return obj == null ? defValue : obj.toString();
}
else {
return defValue;
}
}
public static Integer getMapIntegerValue(Map<String, Object> map, String key) {
return getMapIntegerValue(map, key, 0);
}
public static Integer getMapIntegerValue(Map<String, Object> map, String key, Integer defValue) {
Object obj = map.getOrDefault(key, defValue);
if (obj == null) {
return defValue;
}
else if (obj instanceof Number) {
return ((Number) obj).intValue();
}
else {
String value = obj.toString();
try {
return Integer.parseInt(value);
}
catch (NumberFormatException ex) {
return defValue;
}
}
}
public static Float getMapFloatValue(Map<String, Object> map, String key) {
return getMapFloatValue(map, key, 0F);
}
public static Float getMapFloatValue(Map<String, Object> map, String key, Float defValue) {
Object obj = map.getOrDefault(key, defValue);
if (obj == null) {
return defValue;
}
else if (obj instanceof Number) {
return ((Number) obj).floatValue();
}
else {
String value = obj.toString();
try {
return Float.parseFloat(value);
}
catch (NumberFormatException ex) {
return defValue;
}
}
}
public static Long getMapLongValue(Map<String, Object> map, String key) {
return getMapLongValue(map, key, 0L);
}
public static Long getMapLongValue(Map<String, Object> map, String key, Long defValue) {
Object obj = map.getOrDefault(key, defValue);
if (obj == null) {
return defValue;
}
else if (obj instanceof Number) {
return ((Number) obj).longValue();
}
else {
String value = obj.toString();
try {
return Long.parseLong(value);
}
catch (NumberFormatException ex) {
return defValue;
}
}
}
public static Boolean getMapBooleanValue(Map<String, Object> map, String key) {
return getMapBooleanValue(map, key, false);
}
public static Boolean getMapBooleanValue(Map<String, Object> map, String key, boolean defValue) {
Object obj = map.getOrDefault(key, defValue);
if (obj instanceof Boolean) {
return (Boolean) obj;
}
try {
return Boolean.parseBoolean(obj.toString());
}
catch (Exception ex) {
return defValue;
}
}
/**
* map int int
*
* @param resultMap map
* @return map
*/
public static Map<String, Object> mapNumberToInt(Map<?, ?> resultMap) {
Map<String, Object> res = new HashMap<>(resultMap.size());
for (Object keyObj : resultMap.keySet()) {
String key = keyObj.toString();
if (resultMap.get(key) instanceof Double) {
Double value = (Double) resultMap.get(key);
if (value.intValue() == value) {
res.put(key, ((Double) resultMap.get(key)).intValue());
}
else {
res.put(key, resultMap.get(key));
}
}
else if (resultMap.get(key) instanceof List<?>) {
res.put(key, listNumberToInt((List<?>) resultMap.get(key)));
}
else if (resultMap.get(key) instanceof Map<?, ?>) {
res.put(key, mapNumberToInt((Map<?, ?>) resultMap.get(key)));
}
else {
res.put(key, resultMap.get(key));
}
}
return res;
}
/**
* list int int
*
* @param list list
* @return list
*/
public static List<Object> listNumberToInt(List<?> list) {
List<Object> res = new ArrayList<>(list.size());
for (Object o : list) {
if (o instanceof Number) {
Double value = (Double) o;
if (value.intValue() == value) {
Object v = value.intValue();
res.add(v);
}
else {
res.add(value);
}
}
else if (o instanceof Map<?, ?>) {
res.add(mapNumberToInt((Map<?, ?>) o));
}
else if (o instanceof List<?>) {
res.add(listNumberToInt((List<?>) o));
}
else {
res.add(o);
}
}
return res;
}
public static <T> T mapToObject(Map<String, Object> map, T t) {
if (map == null) {
return null;
}
try {
BeanUtils.populate(t, map);
}
catch (Exception e) {
throw new RuntimeException(e);
}
return t;
}
//endregion
public static Map<String, Object> mapOfJsonString(String jsonString) {
try {
Gson gson = new Gson();
Map<String, Object> resultMap = gson.fromJson(jsonString, new TypeToken<Map<String, Object>>() {
}.getType());
return mapDoubleToInt(resultMap);
}
catch (JsonSyntaxException e) {
throw new RuntimeException("无法解析成Map格式数据");
}
}
public static Map<String, Object> mapDoubleToInt(Map<?, ?> resultMap) {
Map<String, Object> res = new HashMap<>(resultMap.size());
for (Object keyObj : resultMap.keySet()) {
String key = keyObj.toString();
if (resultMap.get(key) instanceof Double) {
Double value = (Double) resultMap.get(key);
if (value.intValue() == value) {
res.put(key, ((Double) resultMap.get(key)).intValue());
}
else {
res.put(key, resultMap.get(key));
}
}
else if (resultMap.get(key) instanceof List<?>) {
res.put(key, listDoubleToInt((List<?>) resultMap.get(key)));
}
else if (resultMap.get(key) instanceof Map<?, ?>) {
res.put(key, mapDoubleToInt((Map<?, ?>) resultMap.get(key)));
}
else {
res.put(key, resultMap.get(key));
}
}
return res;
}
public static List<Object> listDoubleToInt(List<?> list) {
List<Object> res = new ArrayList<>(list.size());
for (Object o : list) {
if (o instanceof Number) {
Double value = (Double) o;
if (value.intValue() == value) {
Object v = value.intValue();
res.add(v);
}
else {
res.add(value);
}
}
else if (o instanceof Map<?, ?>) {
res.add(mapDoubleToInt((Map<?, ?>) o));
}
else if (o instanceof List<?>) {
res.add(listDoubleToInt((List<?>) o));
}
else {
res.add(o);
}
}
return res;
}
public static MapUtil ofJsonString(String jsonString) {
Map<String, Object> map = mapOfJsonString(jsonString);
return new MapUtil(map);
}
private final Map<String, Object> map;
public MapUtil(Map<String, Object> map) {
this.map = map;
}
public String getStringValue(String key) {
return getMapStringValue(this.map, key);
}
public String getStringValue(String key, String defValue) {
return getMapStringValue(this.map, key, defValue);
}
public Integer getIntegerValue(String key) {
return getMapIntegerValue(map, key);
}
public Integer getIntegerValue(String key, Integer defValue) {
return getMapIntegerValue(map, key, defValue);
}
public Float getFloatValue(String key) {
return getMapFloatValue(map, key);
}
public Float getFloatValue(String key, Float defValue) {
return getMapFloatValue(map, key, defValue);
}
public Long getLongValue(String key) {
return getMapLongValue(map, key);
}
public Long getLongValue(String key, Long defValue) {
return getMapLongValue(map, key, defValue);
}
public Object getObjectValue(String key) {
return getMapObjectValue(map, key);
}
public Object getObjectValue(String key, Object defValue) {
return getMapObjectValue(map, key, defValue);
}
public <T> T getValue(String key, T defValue) {
Object obj = map.getOrDefault(key, defValue);
if (obj.getClass().equals(defValue.getClass())) {
return (T) obj;
}
else if (throwException) {
throw new RuntimeException("类型转换失败。");
}
else {
return defValue;
}
}
public Boolean getBooleanValue(String key) {
return getMapBooleanValue(map, key);
}
public Boolean getBooleanValue(String key, boolean defValue) {
return getMapBooleanValue(map, key, defValue);
}
public Timestamp getTimestampValue(String key) {
return getTimestampValue(key, Timestamp.valueOf(LocalDateTime.now()));
}
public Timestamp getTimestampValue(String key, Timestamp defValue) {
Object obj = map.get(key);
if (obj == null) {
return defValue;
}
else if (obj instanceof Timestamp) {
return (Timestamp) obj;
}
else if (obj instanceof Number) {
return new Timestamp(((Number) obj).longValue());
}
else if (obj instanceof String) {
try {
return Timestamp.valueOf((String) obj);
}
catch (Exception ex) {
return defValue;
}
}
else {
return defValue;
}
}
public <T> T getValue(String key, Function<Object, T> function) {
return function.apply(map.getOrDefault(key, null));
}
public <T> List<T> getListValue(String key, Function<Object, T> function) {
Function<Object, ResInfo<T>> function2 = (obj) -> new ResInfo<>(true, function.apply(obj));
return getCollectionValueAllowJudgment(key, function2, () -> new ArrayList<>(0));
}
/**
* Collection(List;Set)
*
* @param key
* @param function
* @param initializeCollection Collection
* @return java.util.Collection<T>
* @author LiuHuiYu
* Created DateTime 2021-08-06 9:50
*/
public <T, R extends Collection<T>> R getCollectionValue(String key, Function<Object, T> function, Supplier<R> initializeCollection) {
Function<Object, ResInfo<T>> function2 = (obj) -> new ResInfo<>(true, function.apply(obj));
return getCollectionValueAllowJudgment(key, function2, initializeCollection);
}
/**
* Collection(List;Set)
*
* @param key
* @param function
* @param initializeCollection Collection
* @return java.util.Collection<T>
* @author LiuHuiYu
* Created DateTime 2021-08-06 9:50
*/
public <T, R extends Collection<T>> R getCollectionValueAllowJudgment(String key, Function<Object, ResInfo<T>> function, Supplier<R> initializeCollection) {
R resList = initializeCollection.get();
if (this.map.containsKey(key)) {
Object obj = this.map.get(key);
if (obj instanceof Collection<?>) {
Collection<?> list = (Collection<?>) obj;
list.forEach(item -> {
ResInfo<T> resInfo = function.apply(item);
if (resInfo.res) {
resList.add(resInfo.resData);
}
});
return resList;
}
else if (this.throwException) {
throw new RuntimeException("无法解析非List数据");
}
else {
return resList;
}
}
else if (this.throwException) {
throw new RuntimeException("不存在的键值。");
}
else {
return resList;
}
}
public static class ResInfo<T> {
Boolean res;
T resData;
public ResInfo(Boolean res, T resData) {
this.res = res;
this.resData = resData;
}
}
}

@ -0,0 +1,125 @@
package com.liuhuiyu.util.model;
import lombok.Data;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.io.Serializable;
import java.util.Map;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2020-07-08 11:44
*/
@Data
public class Result<T> implements Serializable {
public static final int OK = 0;
public static final int ERROR = -1;
/**
*
*/
private Integer flag;
/**
*
*/
private String msg;
/**
*
*/
private T data;
public Result() {
this.flag = OK;
this.msg = "操作成功";
this.data = null;
}
private Result(T data) {
this();
this.data = data;
}
private Result(T data, Integer flag) {
this();
this.data = data;
this.flag = flag;
}
private Result(T data, Integer flag, String msg) {
this();
this.data = data;
this.flag = flag;
this.msg = msg;
}
public boolean isSuccess() {
return this.flag.equals(OK);
}
/**
*
*/
@Contract(value = "_ -> new", pure = true)
public static <T> @NotNull Result<T> of(T data) {
return new Result<>(data);
}
@Contract(value = "_, _ -> new", pure = true)
public static <T> @NotNull Result<T> of(T data, Integer flag) {
return new Result<>(data, flag);
}
@Contract(value = "_, _, _ -> new", pure = true)
public static <T> @NotNull Result<T> of(T data, Integer flag, String msg) {
return new Result<>(data, flag, msg);
}
@Contract(value = "_ -> new", pure = true)
public static <T> @NotNull Result<T> error(String msg) {
return new Result<>(null, ERROR, msg);
}
@Contract(value = "_, _-> new", pure = true)
public static <T> @NotNull Result<T> error(String msg, int errorCode) {
return new Result<>(null, errorCode, msg);
}
@Contract(value = " -> new", pure = true)
public static <T> @NotNull Result<T> success() {
return new Result<>(null, OK, "");
}
private static String FLAG_KEY = "flag";
private static String MSG_KEY = "msg";
private static String DATA_KEY = "data";
public static <T> @NotNull Result<T> ofMap(Map<String, Object> map, Class<T> clazz) {
Result<T> result = new Result<>();
if (map.containsKey(FLAG_KEY)) {
result.flag = ((Number) map.get(FLAG_KEY)).intValue();
}
else {
throw new RuntimeException("缺少关键字" + FLAG_KEY);
}
if (map.containsKey(MSG_KEY)) {
result.msg = map.get(MSG_KEY).toString();
}
else {
throw new RuntimeException("缺少关键字" + MSG_KEY);
}
if (map.containsKey(DATA_KEY)) {
if (clazz.isInstance(map.get(DATA_KEY))) {
result.data = clazz.cast(map.get(DATA_KEY));
}
else {
throw new RuntimeException("Map 关键字‘" + DATA_KEY + "’无法转换为当前类型。");
}
}
else {
throw new RuntimeException("缺少关键字" + DATA_KEY);
}
return result;
}
}

@ -0,0 +1,179 @@
package com.liuhuiyu.util.security;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.*;
import java.security.interfaces.RSAKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-02-18 9:00
*/
public class RsaUtil {
public static final String CHARSET = "UTF-8";
public static final String RSA_ALGORITHM = "RSA";
public static final String MAP_KEY_PUBLIC = "publicKey";
public static final String MAP_KEY_PRIVATE = "privateKey";
public static Map<String, String> createKeys(int keySize) {
//为RSA算法创建一个KeyPairGenerator对象
KeyPairGenerator kpg;
try {
kpg = KeyPairGenerator.getInstance(RSA_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException("没有[" + RSA_ALGORITHM + "]的算法");
}
//初始化KeyPairGenerator对象,密钥长度
kpg.initialize(keySize);
//生成密匙对
KeyPair keyPair = kpg.generateKeyPair();
//得到公钥
Key publicKey = keyPair.getPublic();
String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded());
//得到私钥
Key privateKey = keyPair.getPrivate();
String privateKeyStr = Base64.encodeBase64URLSafeString(privateKey.getEncoded());
Map<String, String> keyPairMap = new HashMap<>(2);
keyPairMap.put(MAP_KEY_PUBLIC, publicKeyStr);
keyPairMap.put(MAP_KEY_PRIVATE, privateKeyStr);
return keyPairMap;
}
/**
*
*
* @param publicKey base64
* @throws NoSuchAlgorithmException
*/
public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
//通过X509编码的Key指令获得公钥对象
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
return (RSAPublicKey) keyFactory.generatePublic(x509KeySpec);
}
/**
*
*
* @param privateKey base64
* @throws NoSuchAlgorithmException
*/
public static RSAPrivateKey getPrivateKey(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
//通过PKCS#8编码的Key指令获得私钥对象
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
return (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
}
/**
*
*
* @param data
* @param publicKey
* @return
*/
public static String publicEncrypt(String data, RSAPublicKey publicKey) {
return encrypt(data, publicKey);
}
private static String encrypt(String data, Key key) {
try {
RSAKey rsaKey = (RSAKey) key;
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), rsaKey.getModulus().bitLength()));
} catch (Exception e) {
throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e);
}
}
/**
*
*
* @param data
* @param privateKey
* @return
*/
public static String privateDecrypt(String data, RSAPrivateKey privateKey) {
return decrypt(data, privateKey);
}
/**
*
*
* @param data
* @param privateKey
* @return
*/
public static String privateEncrypt(String data, RSAPrivateKey privateKey) {
return encrypt(data, privateKey);
}
/**
*
*
* @param data
* @param publicKey
* @return
*/
public static String publicDecrypt(String data, RSAPublicKey publicKey) {
return decrypt(data, publicKey);
}
private static String decrypt(String data, Key key) {
try {
RSAKey rsaKey = (RSAKey) key;
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data), rsaKey.getModulus().bitLength()), CHARSET);
} catch (Exception e) {
throw new RuntimeException("解密字符串[" + data + "]时遇到异常", e);
}
}
private static byte[] rsaSplitCodec(Cipher cipher, int opMode, byte[] dataBytes, int keySize) throws IOException {
int maxBlock;
if (opMode == Cipher.DECRYPT_MODE) {
maxBlock = keySize / 8;
}
else {
maxBlock = keySize / 8 - 11;
}
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
int offSet = 0;
byte[] buff;
int i = 0;
try {
while (dataBytes.length > offSet) {
if (dataBytes.length - offSet > maxBlock) {
buff = cipher.doFinal(dataBytes, offSet, maxBlock);
}
else {
buff = cipher.doFinal(dataBytes, offSet, dataBytes.length - offSet);
}
out.write(buff, 0, buff.length);
i++;
offSet = i * maxBlock;
}
} catch (Exception e) {
throw new RuntimeException("加解密阀值为[" + maxBlock + "]的数据时发生异常", e);
}
return out.toByteArray();
}
}
}

@ -0,0 +1,133 @@
package com.liuhuiyu.util.thread;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-07-17 10:29
*/
public class ExecutorBuilder {
/**
* cpu
* Created DateTime 2021-07-17 10:31
*/
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
/**
* 线(2-4)
* Created DateTime 2021-07-17 10:51
*/
private int corePoolSize = Math.max(2, Math.min(CPU_COUNT - 1, 4));
/**
* 线(cpu * 2 +1)
* Created DateTime 2021-07-17 10:51
*/
private int maxPoolSize = CPU_COUNT * 2 + 1;
/**
* (20)
* Created DateTime 2021-07-17 10:52
*/
private int workQueue = 20;
/**
* 线(30)
* Created DateTime 2021-07-17 10:52
*/
private int keepAliveSeconds = 30;
private String threadName = "taskExecutor-";
public static ExecutorBuilder create() {
return new ExecutorBuilder();
}
/**
* 线
*
* @param corePoolSize 线
* @return com.liuhuiyu.util.thread.ThreadUtil.ExecutorBuilder
* @author LiuHuiYu
* Created DateTime 2021-07-17 10:28
*/
public ExecutorBuilder corePoolSize(int corePoolSize) {
this.corePoolSize = corePoolSize;
return this;
}
/**
* 线线
*
* @param maxPoolSize 线线
* @return com.liuhuiyu.util.thread.ThreadUtil.ExecutorBuilder
* @author LiuHuiYu
* Created DateTime 2021-07-17 10:28
*/
public ExecutorBuilder maxPoolSize(int maxPoolSize) {
this.maxPoolSize = maxPoolSize;
return this;
}
/**
*
*
* @param workQueue
* @return com.liuhuiyu.util.thread.ThreadUtil.ExecutorBuilder
* @author LiuHuiYu
* Created DateTime 2021-07-17 10:28
*/
public ExecutorBuilder workQueue(int workQueue) {
this.workQueue = workQueue;
return this;
}
/**
* 线
*
* @param keepAliveSeconds 线
* @return com.liuhuiyu.util.thread.ThreadUtil.ExecutorBuilder
* @author LiuHuiYu
* Created DateTime 2021-07-17 10:27
*/
public ExecutorBuilder keepAliveSeconds(int keepAliveSeconds) {
this.keepAliveSeconds = keepAliveSeconds;
return this;
}
/**
* 线
*
* @param threadName 线
* @return com.liuhuiyu.util.thread.ThreadUtil.ExecutorBuilder
* @author LiuHuiYu
* Created DateTime 2021-07-17 10:27
*/
public ExecutorBuilder threadName(String threadName) {
this.threadName = threadName;
return this;
}
public ThreadPoolTaskExecutor builder() {
// return (r)->{
// Thread thread=new Thread(r);
// thread.setName(threadName);
// thread.start();
// };
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
//核心线程数
threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
//最大线程数
threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
//等待队列
threadPoolTaskExecutor.setQueueCapacity(workQueue);
//线程前缀
threadPoolTaskExecutor.setThreadNamePrefix(threadName);
//线程池维护线程所允许的空闲时间,单位为秒
threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveSeconds);
// 线程池对拒绝任务(无线程可用)的处理策略
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
}

@ -0,0 +1,89 @@
package com.liuhuiyu.util.thread;
import lombok.extern.log4j.Log4j2;
import org.jetbrains.annotations.NotNull;
import org.springframework.scheduling.concurrent.ExecutorConfigurationSupport;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* 线
*
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-04-14 14:02
*/
@Log4j2
public class ThreadUtil {
/**
* List()
*
* @param list list
* @param consumer list
* @author LiuHuiYu
* Created DateTime 2021-04-14 14:03
*/
public static <T> void asynchronousDataLoading(List<T> list, Consumer<T> consumer) {
asynchronousDataLoading(list, consumer, ExecutorBuilder.create().threadName("async-load-").builder());
}
/**
* List()
*
* @param list list
* @param consumer list
* @param executor 线
* @author LiuHuiYu
* Created DateTime 2021-04-14 14:03
*/
public static <T> void asynchronousDataLoading(@NotNull List<T> list, Consumer<T> consumer, ThreadPoolTaskExecutor executor) {
asynchronousDataLoading(list, consumer, executor, executor::shutdown);
}
/**
* List()
*
* @param list list
* @param consumer list
* @param executor 线
* @param closeExecutor 线
* @author LiuHuiYu
* Created DateTime 2021-04-14 14:03
*/
public static <T> void asynchronousDataLoading(@NotNull List<T> list, Consumer<T> consumer, Executor executor, Runnable closeExecutor) {
CompletableFuture.allOf(list.stream()
.map(item -> CompletableFuture.runAsync(() -> consumer.accept(item), executor))
.toArray(CompletableFuture[]::new)).join();
closeExecutor.run();
}
/**
* ()
*
* @param millis
* @author LiuHuiYu
* Created DateTime 2021-08-20 14:43
*/
public static boolean sleep(long millis) {
try {
Thread.sleep(millis);
return true;
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();//如果不加上这一句那么cd将会都是false因为在捕捉到InterruptedException异常的时候就会自动的中断标志置为了false
Boolean c = Thread.interrupted();
Boolean d = Thread.interrupted();
log.error("中断引发休眠异常:{};重置中断位{}->{}", e.getMessage(), c, d);
return false;
}
}
}

@ -0,0 +1,51 @@
package com.liuhuiyu.util.web;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
*
*
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2020-06-23 13:42
*/
public class AddressRoutingUtil {
/**
*
*
* @param request request
* @param root
* @param path
* @return 访
*/
public static String getFullAddress(HttpServletRequest request, String root, String path) {
return getFullAddress(request, root, path, null);
}
/**
*
*
* @param request request
* @param root
* @param path
* @param parameter
* @return 访
*/
public static String getFullAddress(HttpServletRequest request, String root, String path, Map<String, Object> parameter) {
String foot = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
String address;
address = foot + root + path;
if (null == parameter) {
return address;
} else {
String linkSymbol = "?";
for (String key : parameter.keySet()) {
address = String.format("%1$s%2$s%3$s=%4$s", address, linkSymbol, key, parameter.get(key).toString());
linkSymbol = "&";
}
return address;
}
}
}

@ -0,0 +1,29 @@
package com.liuhuiyu.util.web;
import com.liuhuiyu.util.asserts.*;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2020-06-23 13:23
*/
public class HttpUtil {
public static @NotNull HttpServletRequest getHttpServletRequest() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
LhyAssert.assertNotNull(attributes, "Spring配置错误");
return attributes.getRequest();
}
public static HttpServletResponse getHttpServletResponse() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
LhyAssert.assertNotNull(attributes, "Spring配置错误");
return attributes.getResponse();
}
}

@ -0,0 +1,107 @@
package com.liuhuiyu.util.web;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-03-09 11:35
*/
public class JsUtil {
private static final String BASE_64_HASH = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
public static final String VALUE_RANGE_REGEX = "([^\\u0000-\\u00ff])";
public static boolean isMatcher(String inStr, String reg) {
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(inStr);
return matcher.matches();
}
/**
* btoa method
*
* @param inStr
* @return
*/
public static String btoa(String inStr) {
if (inStr == null || isMatcher(inStr, VALUE_RANGE_REGEX)) {
return null;
}
StringBuilder result = new StringBuilder();
int i = 0;
int mod = 0;
int ascii;
int prev = 0;
while (i < inStr.length()) {
ascii = inStr.charAt(i);
mod = i % 3;
switch (mod) {
case 0:
result.append(BASE_64_HASH.charAt(ascii >> 2));
break;
case 1:
result.append(BASE_64_HASH.charAt((prev & 3) << 4 | (ascii >> 4)));
break;
case 2:
result.append(BASE_64_HASH.charAt((prev & 0x0f) << 2 | (ascii >> 6)));
result.append(BASE_64_HASH.charAt(ascii & 0x3f));
break;
default:
break;
}
prev = ascii;
i++;
}
if (mod == 0) {
result.append(BASE_64_HASH.charAt((prev & 3) << 4));
result.append("==");
}
else if (mod == 1) {
result.append(BASE_64_HASH.charAt((prev & 0x0f) << 2));
result.append("=");
}
return result.toString();
}
/**
* // atob method
* // 逆转encode的思路即可
*
* @param inStr
* @return
*/
public static String atob(String inStr) {
if (inStr == null) {
return null;
}
inStr = inStr.replaceAll("\\s|=", "");
StringBuilder result = new StringBuilder();
int cur;
int prev = -1;
int mod;
int i = 0;
while (i < inStr.length()) {
cur = BASE_64_HASH.indexOf(inStr.charAt(i));
mod = i % 4;
switch (mod) {
case 1:
result.append((char) (prev << 2 | cur >> 4));
break;
case 2:
result.append((char) ((prev & 0x0f) << 4 | cur >> 2));
break;
case 3:
result.append((char) ((prev & 3) << 6 | cur));
break;
default:
break;
}
prev = cur;
i++;
}
return result.toString();
}
}

@ -0,0 +1,39 @@
package com.liuhuiyu.util;
import junit.framework.TestCase;
import lombok.extern.log4j.Log4j2;
import sun.misc.Contended;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2020-12-29 9:16
*/
@Log4j2
public class HttpUtilTest extends TestCase {
public void testA1() {
log.info("Run OK");
}
// private volatile boolean s;
// private volatile String b(){
//
// }
// @Contended
// class b{
////LoadLoad;
////StoreStore;
// void s2(){
// ----StoreStoreBarrier----
// Load;
// Store;
// }
//// StoreLoad;
// }
public void testStringToInt() {
String s = "123s";
Integer i = Integer.getInteger(s, 101);
log.info("i = {}", i);
}
}

@ -0,0 +1,176 @@
package com.liuhuiyu.util.exception;
import com.liuhuiyu.util.thread.ThreadUtil;
import lombok.extern.log4j.Log4j2;
import org.junit.Test;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-07-05 15:59
*/
@Log4j2
public class RetryUtilTest {
// @Test
// public void t1() {
// time = System.currentTimeMillis();
// String s = s1(time, "abc");
// log.info("返回信息:{}", s);
// }
//
// long time;
// private String s1(long time, String s) {
// log.info("进入时间{}", time);
// getTime();
// long newTime = System.currentTimeMillis();
// log.info("{},{}", time, newTime);
// if (newTime > time + 1000) {
// return s;
// }
// Object o = RetryUtil.setRetryTimes(13).retry(s);
// if (o != null) {
// return o.toString();
// }
// throw new RuntimeException("错误");
// }
private long getTime() {
log.info("gettime");
return System.currentTimeMillis();
}
private int retryTimes = 3;
@Test
public void upperMethod() {
method("123", "456");
}
public void method(String param1, String param2) {
System.out.println(param1 + param2);
// 其他一些操作,但是没有得到预期的返回结果,或者抛出异常
boolean isException = true;
if (isException && retryTimes > 0) {
retryTimes--;
method(param1, param2);
}
}
@Test
public void upperMethod2() {
method(3, "123", "456");
}
public void method(int retryTimes, String param1, String param2) {
System.out.println(param1 + param2);
// 其他一些操作,但是没有得到预期的返回结果,或者抛出异常
boolean isException = true;
if (isException && retryTimes > 0) {
method(--retryTimes, param1, param2);
}
}
// @Test
// public void mainMethod() throws InterruptedException {
// String s = subMethod("123", "456");
// log.info(s);
// }
//
// public String subMethod(String param1, String param2) throws InterruptedException {
// long t = System.currentTimeMillis();
// log.info("begin:{},{},{}", param1, param2, t);
// Thread.sleep(100);
// new RetryUtil().retry(10, param1, param2);
// long t2 = System.currentTimeMillis();
// log.info("end:{},{},{}-{}", param1, param2, t, t2);
// return param1 + param2 + t;
// }
@Test
public void retry() {
Map<String, Object> map = new HashMap<>(1);
long time = System.currentTimeMillis();
RetryUtil.retry(3, () -> {
try {
Thread.sleep(600);
}
catch (InterruptedException e) {
e.printStackTrace();
}
map.put("res", s(time));
return false;
});
log.info(map);
}
@Test
public void retry2() {
Map<String, Object> map = new HashMap<>(1);
long time = System.currentTimeMillis();
RetryUtil.retry(10, () -> {
return time;
}, () -> {
try {
log.info("失败次执行");
Thread.sleep(300);
}
catch (InterruptedException e) {
e.printStackTrace();
}
});
log.info(map);
}
@Test
public void retry3() {
Map<String, Object> map = new HashMap<>(1);
long time = System.currentTimeMillis();
RetryUtil.retry(3, () -> {
map.put("res", s(time));
return time;
}, () -> {
try {
log.info("失败1次执行");
Thread.sleep(500);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}, () -> {
try {
log.info("失败2次执行");
Thread.sleep(500);
}
catch (InterruptedException e) {
e.printStackTrace();
}
});
log.info(map);
}
private String s(long o) {
long time = System.currentTimeMillis();
if (o > time - 1000) {
throw new RuntimeException("");
}
return "" + time;
}
@Test
public void testTime(){
LocalDateTime time=LocalDateTime.now().plusSeconds(5);
ThreadUtil.sleep(2000);
log.info("2秒后比较{}",time.isBefore(LocalDateTime.now()));
ThreadUtil.sleep(2000);
log.info("2秒后比较{}",time.isBefore(LocalDateTime.now()));
ThreadUtil.sleep(2000);
log.info("2秒后比较{}",time.isBefore(LocalDateTime.now()));
}
}

@ -0,0 +1,119 @@
package com.liuhuiyu.util.list;
import lombok.extern.log4j.Log4j2;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import static org.junit.Assert.*;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-05-25 19:16
*/
@Log4j2
public class ListUtilTest {
@Test
public void objectToList() {
Object o = getList();
List<Object> list = ListUtil.objectToList(o);
log.info("转换List:{}", list);
Object o1 = getArray();
List<Object> list1 = ListUtil.objectToList(o1);
log.info("转换List:{}", list1);
Object o2 = new int[]{};
List<Object> list2 = ListUtil.objectToList(o2);
log.info("转换List:{}", list2);
}
@Test
public void objectToListT() {
Object o = getList();
List<Integer> list = ListUtil.objectToListT(o, (obj) -> {
if (obj instanceof Integer) {
return (Integer) obj;
}
else {
return 0;
}
});
log.info("转换List:{}", list);
Object o1 = getArray();
List<Integer> list1 = ListUtil.objectToListT(o1, (obj) -> {
if (obj instanceof Integer) {
return (Integer) obj;
}
else {
return 0;
}
});
log.info("转换List:{}", list1);
}
@Test
public void getSortList() {
List<Integer> list = new ArrayList<>();
Collections.addAll(list, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9);
Integer[] sorts = {1, 3, 5, 7, 9, 4, 8};
BiFunction<Integer, Integer, Boolean> compare = Integer::equals;
List<Integer> res;
res = ListUtil.getSortList(list, sorts, compare);
log.info(res);
res = ListUtil.getSortList(list, sorts, compare, true, false, false);
log.info(res);
res = ListUtil.getSortList(list, sorts, compare, true, false, true);
log.info(res);
res = ListUtil.getSortList(list, sorts, compare, true, true, false);
log.info(res);
res = ListUtil.getSortList(list, sorts, compare, true, true, true);
log.info(res);
res = ListUtil.getSortList(list, sorts, compare, false, false, false);
log.info(res);
res = ListUtil.getSortList(list, sorts, compare, false, false, true);
log.info(res);
res = ListUtil.getSortList(list, sorts, compare, false, true, false);
log.info(res);
res = ListUtil.getSortList(list, sorts, compare, false, true, true);
log.info(res);
}
@Test
public void listToList() {
List<Integer> integerList = this.getList();
List<Long> longList = ListUtil.listToList(integerList, Integer::longValue);
List<String> strList = ListUtil.listToList(integerList, (i) -> "'" + i.toString() + "'");
log.info("{}->{}->{}", integerList, longList, strList);
}
@Test
public void listToArray() {
List<Integer> integerList = this.getList();
Integer[] is = ListUtil.listToArray(integerList);
log.info("{}->{}", integerList, is);
}
@Test
public void arrayToList() {
Integer[] array = getArray();
List<Integer> list = ListUtil.arrayToList(array);
log.info("{}->{}", array, list);
}
private List<Integer> getList() {
List<Integer> integerList = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
integerList.add(i);
}
return integerList;
}
private Integer[] getArray() {
return new Integer[]{1, 2, 3, 4, 5, 6, 7};
}
}

@ -0,0 +1,24 @@
package com.liuhuiyu.util.map;
import lombok.extern.log4j.Log4j2;
import org.junit.Test;
import java.util.Map;
import static org.junit.Assert.*;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-06-07 9:02
*/
@Log4j2
public class MapUtilTest {
@Test
public void test() {
String json = "{a:1,b:1.5,c:[{j:1,k:1.6},{j:2,k:1.7}],d:8088.0,16}";
Map<String, Object> map = MapUtil.mapOfJsonString(json);
log.info(map);
}
}

@ -0,0 +1,82 @@
package com.liuhuiyu.util.security;
import lombok.extern.log4j.Log4j2;
import org.junit.Test;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.util.Map;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-02-18 9:22
*/
@Log4j2
public class RSAUtilTest {
@Test
public void createKeys() {
int keySize = 512;
Map<String, String> map = RsaUtil.createKeys(keySize);
log.info(map);
}
@Test
public void getPublicKey() throws InvalidKeySpecException, NoSuchAlgorithmException {
int keySize = 512;
Map<String, String> map = RsaUtil.createKeys(keySize);
RSAPublicKey key = RsaUtil.getPublicKey(map.get(RsaUtil.MAP_KEY_PUBLIC));
log.info(key);
}
@Test
public void getPrivateKey() throws InvalidKeySpecException, NoSuchAlgorithmException {
int keySize = 512;
Map<String, String> map = RsaUtil.createKeys(keySize);
RSAPrivateKey key = RsaUtil.getPrivateKey(map.get(RsaUtil.MAP_KEY_PRIVATE));
log.info(key);
}
@Test
public void publicEncrypt() throws InvalidKeySpecException, NoSuchAlgorithmException {
int keySize = 512;
String info = "abcd";
Map<String, String> map = RsaUtil.createKeys(keySize);
String encryptInfo = RsaUtil.publicEncrypt(info, RsaUtil.getPublicKey(map.get(RsaUtil.MAP_KEY_PUBLIC)));
log.info(encryptInfo);
}
@Test
public void privateDecrypt() throws InvalidKeySpecException, NoSuchAlgorithmException {
int keySize = 512;
String info = "abcd";
Map<String, String> map = RsaUtil.createKeys(keySize);
String encryptInfo = RsaUtil.publicEncrypt(info, RsaUtil.getPublicKey(map.get(RsaUtil.MAP_KEY_PUBLIC)));
log.info(encryptInfo);
String decryptInfo=RsaUtil.privateDecrypt(encryptInfo,RsaUtil.getPrivateKey(map.get(RsaUtil.MAP_KEY_PRIVATE)));
log.info(decryptInfo);
}
@Test
public void privateEncrypt() throws InvalidKeySpecException, NoSuchAlgorithmException {
int keySize = 512;
String info = "abcd";
Map<String, String> map = RsaUtil.createKeys(keySize);
String encryptInfo = RsaUtil.privateEncrypt(info, RsaUtil.getPrivateKey(map.get(RsaUtil.MAP_KEY_PRIVATE)));
log.info(encryptInfo);
}
@Test
public void publicDecrypt() throws InvalidKeySpecException, NoSuchAlgorithmException {
int keySize = 512;
String info = "abcd";
Map<String, String> map = RsaUtil.createKeys(keySize);
String encryptInfo = RsaUtil.privateEncrypt(info, RsaUtil.getPrivateKey(map.get(RsaUtil.MAP_KEY_PRIVATE)));
log.info(encryptInfo);
String decryptInfo=RsaUtil.publicDecrypt(encryptInfo,RsaUtil.getPublicKey(map.get(RsaUtil.MAP_KEY_PUBLIC)));
log.info(decryptInfo);
}
}

@ -0,0 +1,51 @@
package com.liuhuiyu.util.thread;
import lombok.extern.log4j.Log4j2;
import org.junit.Test;
import java.util.concurrent.CompletableFuture;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-07-30 20:25
*/
@Log4j2
public class ThreadCpu {
private static int x = 0, y = 0;
private static int a = 0, b = 0;
int i = 0;
@Test
public void test() throws InterruptedException {
int i = 0;
for (; ; ) {
i++;
x = 0;
y = 0;
a = 0;
b = 0;
Thread one = new Thread(() -> {
a = 1;
x = b;
});
Thread other = new Thread(() -> {
b = 1;
y = a;
});
one.start();
other.start();
one.join();
other.join();
String result = "第" + i + "次 (" + x + "," + y + "";
// 出现00组合 证明cpu乱序执行了
if (x == 0 && y == 0) {
System.err.println(result);
break;
} else {
//System.out.println(result);
}
}
}
}

@ -0,0 +1,132 @@
package com.liuhuiyu.util.thread;
import lombok.extern.log4j.Log4j2;
import org.junit.Test;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-07-17 10:36
*/
@Log4j2
public class ThreadUtilTest {
@Test
public void asynchronousDataLoading() {
List<Integer> list = new ArrayList<>(5);
Collections.addAll(list, 5, 1, 6, 4, 3, 2, 7, 5, 6);
ThreadUtil.asynchronousDataLoading(list, this::testExe);
}
@Test
public void asynchronousDataLoading2() {
List<Integer> list = new ArrayList<>(5);
Collections.addAll(list, 5, 1, 6, 4, 3, 2, 7, 5, 6);
for (int i = 0; i < 100; i++) {
ThreadPoolTaskExecutor threadPoolTaskExecutor = ExecutorBuilder.create().corePoolSize(list.size()).threadName("test-" + i + "-").builder();
ThreadUtil.asynchronousDataLoading(list, this::testExe, threadPoolTaskExecutor);
}
}
private void testExe(Integer i) {
try {
log.info("开始:{}", i);
Thread.sleep(i * 1000);
log.info("完成:{}", i);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void s() {
List<String> list = new ArrayList<String>() {{
this.add("1");
this.add("2");
this.add("3");
this.add("3");
this.add("32");
}};
String[] array = list.toArray(new String[0]);
log.info("{},{}", list, list.size());
log.info(":{};{};", array, array.length);
}
private List<Integer> getIntegerList() {
return new ArrayList<Integer>() {{
this.add(1);
this.add(2);
this.add(3);
this.add(4);
this.add(5);
}};
}
@Test
@SuppressWarnings({"rawtypes", "unused"})
public void testCompletableFuture() throws ExecutionException, InterruptedException {
List<Integer> integerList = getIntegerList();
// AtomicReference<CompletableFuture<Integer>> future = new AtomicReference<>();
integerList.forEach(i -> {
CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
sleep(i);
return i;
});
// future.set(future01);
});
CompletableFuture.supplyAsync(() -> integerList.stream().map((item) -> CompletableFuture.runAsync(() -> sleep(item))).collect(Collectors.toList()));
CompletableFuture[] s2 = integerList.stream().map((item) -> CompletableFuture.runAsync(() -> sleep(item))).toArray(CompletableFuture[]::new);
CompletableFuture.anyOf(s2).thenAccept((a) -> {
}).get();
}
private void sleep(Integer i) {
// log.info("进入{}",i);
log.info("任务{}线程:", Thread.currentThread().getId());
if (i < 3)
throw new RuntimeException();
try {
Thread.sleep(i * 1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
log.info("离开{}", Thread.currentThread().getId());
}
@Test
public void testSleep() {
Runnable runnable = () -> {
long millis = 10_000;
log.info("开始执行:{}毫秒延时。", millis);
if (ThreadUtil.sleep(millis)) {
log.info("正常结束。");
}
else {
log.info("线程内异常,中断休眠");
}
};
Thread thread = new Thread(runnable);
thread.start();
log.info("3秒后引发中断。");
if (ThreadUtil.sleep(3_000)) {
log.info("引发中断。");
thread.interrupt();
log.info("引发中断完成。");
}
long millis = 100_000;
log.info("程序完成。");
if (!ThreadUtil.sleep(millis)) {
log.info("异常中断休眠");
}
}
}

@ -0,0 +1,23 @@
package com.liuhuiyu.util.web;
import junit.framework.TestCase;
import lombok.extern.log4j.Log4j2;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-03-09 11:42
*/
@Log4j2
public class WebUtilTest extends TestCase {
public void testBtoA() {
String b = "abcd";
String a = JsUtil.btoa(b);
log.info("bToA a={};b={}", a, b);
}
public void testAtoB() {
String a = "YWJjZA==";
String b = JsUtil.atob(a);
log.info("aToB a={};b={}", a, b);
}
}

@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liuhuiyu</groupId>
<artifactId>web</artifactId>
<version>1.0-SNAPSHOT</version>
<name>web</name>
<url>http://com.liuhuiyu.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
</dependencies>
<!--项目部署和发布的话需要配置: 在pom.xml中添加 distributionManagement 配置-->
<!-- maven仓库配置 deploy时可推送到配置的仓库中 -->
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>Nexus Releases Repository Pro</name>
<url>http://47.242.184.139:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>Nexus Snapshots Repository Pro</name>
<url>http://47.242.184.139:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

@ -0,0 +1,34 @@
package com.liuhuiyu.web;
import javax.servlet.http.HttpServletRequest;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-02-23 21:05
*/
public class IpUtil {
public static final String UNKNOWN="unknown";
public static String getIpAddress(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (isNullIp(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (isNullIp(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (isNullIp(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (isNullIp(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (isNullIp(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
public static boolean isNullIp(String ip){
return ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip);
}
}

@ -0,0 +1,78 @@
package com.liuhuiyu.web;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-07-20 13:53
*/
public class RequestParametersUtil {
public static final String GET = "GET";
public static final String POST = "POST";
/**
* request
*
* @param request
*/
public static Map<String, Object> getRequestParameters(HttpServletRequest request) {
//请求参数
String parameters = "";
//GET请求时的参数
if (GET.equals(request.getMethod())) {
//网址中的参数
String urlParameter = request.getQueryString();
if (urlParameter != null && !"".equals(urlParameter)) {
try {
urlParameter = URLDecoder.decode(urlParameter, "UTF-8");
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
else {
urlParameter = "";
}
parameters = urlParameter;
}
//POST请求时的参数
else if (POST.equals(request.getMethod())) {
//表单及网址中全部参数
StringBuilder totalParameter = new StringBuilder();
Map<String, String[]> params = request.getParameterMap();
//参数个数
int parametersNum = request.getParameterMap().size();
int flag = 1;
for (String key : params.keySet()) {
String[] values = params.get(key);
for (String value : values) {
totalParameter.append(key).append("=").append(value);
}
if (flag < parametersNum) {
totalParameter.append("&");
}
flag += 1;
}
parameters = totalParameter.toString();
}
Map<String, Object> map = new HashMap<>(0);
String[] arr = parameters.split("&");
for (String s : arr) {
int index = s.indexOf("=");
//=不能是首位或者是末尾
if (index < 1 || index >= (s.length() - 1)) {
break;
}
String key = s.substring(0, index);
String value = s.substring(index + 1);
map.put(key, value);
}
return map;
}
}

@ -0,0 +1,51 @@
package com.liuhuiyu.web.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
/**
*
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2018-06-28 16:49
*/
public abstract class BaseAspect {
protected Boolean isResponseBody (ProceedingJoinPoint pjp){
MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
Method method = methodSignature.getMethod();
return method.getAnnotation(ResponseBody.class)!=null;
}
/**
* HttpServletRequest
* @return HttpServletRequest
*/
protected HttpServletRequest getHttpServletRequest() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
throw new RuntimeException("ServletRequestAttributes 空指针异常。");
}
return attributes.getRequest();
}
/**
* HttpServletResponse
* @return HttpServletResponse
*/
protected HttpServletResponse getHttpServletResponse() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
throw new RuntimeException("ServletRequestAttributes 空指针异常。");
}
return attributes.getResponse();
}
}

@ -0,0 +1,35 @@
package com.liuhuiyu.web.controller;
import javax.servlet.http.HttpServletRequest;
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2020-12-29 8:48
*/
public abstract class AbsBaseController {
/**
*
*/
protected static final String PATH_INDEX = "/index";
/**
*
*/
protected static final String PATH_DEFAULT = "/";
/**
*
*/
public static final String ATTRIBUTE_PAGE_INDEX = "page_index";
/**
*
*/
public static final String ATTRIBUTE_PAGE_SIZE = "page_size";
/**
* id
*/
public static final String ATTRIBUTE_ID = "id";
/**
*
*/
protected static final int DEF_PAGE_SIZE = 20;
}

@ -0,0 +1,6 @@
/**
* @author LiuHuiYu
* @version v1.0.0.0
* Created DateTime 2021-01-12 11:13
*/
package com.liuhuiyu.web;
Loading…
Cancel
Save