update at 2020-12-09 17:27:50 by ehlxr

dev
ehlxr 2020-12-09 17:27:50 +08:00
parent e764f36b1f
commit a552a39e4d
73 changed files with 813 additions and 101 deletions

82
pom.xml
View File

@ -11,6 +11,11 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!--目标编译的 Java 版本可以通过以下属性指定,不用配置 maven-compiler-plugin 插件-->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.2.6.RELEASE</spring.version>
<servlet.version>2.5</servlet.version>
<mybatis.version>3.2.7</mybatis.version>
@ -286,20 +291,35 @@
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.3</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.7</version>
</dependency>
</dependencies>
<build>
@ -325,15 +345,15 @@
</plugin>
<!-- 指定JDK编译版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${jdk.verion}</source>
<target>${jdk.verion}</target>
<encoding>${file_encoding}</encoding>
</configuration>
</plugin>
<!--<plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-compiler-plugin</artifactId>-->
<!-- <configuration>-->
<!-- <source>${jdk.verion}</source>-->
<!-- <target>${jdk.verion}</target>-->
<!-- <encoding>${file_encoding}</encoding>-->
<!-- </configuration>-->
<!--</plugin>-->
<!-- jetty 服务器; Maven build... -> jetty:run -->
<plugin>
@ -423,26 +443,26 @@
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<!--<plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-compiler-plugin</artifactId>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>compile</id>-->
<!-- <phase>compile</phase>-->
<!-- <goals>-->
<!-- <goal>compile</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- <execution>-->
<!-- <id>testCompile</id>-->
<!-- <phase>test-compile</phase>-->
<!-- <goals>-->
<!-- <goal>testCompile</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!--</plugin>-->
</plugins>
<defaultGoal>compile</defaultGoal>
</build>

View File

@ -1,7 +1,7 @@
package me.ehlxr;
/**
* Created by lixiangrong on 2017/8/3.
* Created by ehlxr on 2017/8/3.
*/
public class CharToHex {
private final static char[] hexArray = "0123456789ABCDEF".toCharArray();

View File

@ -3,7 +3,7 @@ package me.ehlxr;
import java.io.*;
/**
* Created by lixiangrong on 2017/11/17.
* Created by ehlxr on 2017/11/17.
* <p>
* Java CopyRight
*/

View File

@ -9,7 +9,7 @@ import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/**
* Created by lixiangrong on 2017/8/1.
* Created by ehlxr on 2017/8/1.
*/
public class DemoECDSA {

View File

@ -4,7 +4,7 @@ import java.util.Arrays;
import java.util.List;
/**
* Created by lixiangrong on 2017/6/20.
* Created by ehlxr on 2017/6/20.
*/
public class LambdaTest {
public static void main(String[] args) {

View File

@ -7,7 +7,7 @@ import java.io.File;
import java.io.IOException;
/**
* Created by lixiangrong on 2016/11/8.
* Created by ehlxr on 2016/11/8.
*/
public class MD2Json {
public static void main(String[] args) throws IOException {

View File

@ -1,7 +1,7 @@
package me.ehlxr;
/**
* Created by lixiangrong on 2018/2/13.
* Created by ehlxr on 2018/2/13.
*/
public class PrintMatrixClockWisely {
/**

View File

@ -1,7 +1,7 @@
package me.ehlxr;
/**
* @author lixiangrong
* @author ehlxr
* @date 2018/8/21
*/
public class ReferenceCountingGC {

View File

@ -3,7 +3,7 @@ package me.ehlxr;
import java.io.*;
/**
* @author lixiangrong
* @author ehlxr
* @date 2017/3/27
*/
public class Rename {

View File

@ -1,7 +1,7 @@
package me.ehlxr;
/**
* Created by lixiangrong on 2018/1/16.
* Created by ehlxr on 2018/1/16.
*
*/
public class VarArgsTest {

View File

@ -13,7 +13,7 @@ import javax.jms.TextMessage;
/**
*
* Created by lixiangrong on 2016-07-11
* Created by ehlxr on 2016-07-11
*/
public class JMSConsumer {

View File

@ -13,7 +13,7 @@ import javax.jms.TextMessage;
/**
*
* Created by lixiangrong on 2016-07-11.
* Created by ehlxr on 2016-07-11.
*/
public class JMSProducer {

View File

@ -9,7 +9,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author lixiangrong
* @author ehlxr
*
*/
@Target(ElementType.PACKAGE)

View File

@ -2,7 +2,7 @@
*
*/
/**
* @author lixiangrong
* @author ehlxr
*
*/
@PkgAnnotation

View File

@ -6,7 +6,7 @@ import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
/**
* Created by lixiangrong on 2017/4/11.
* Created by ehlxr on 2017/4/11.
*/
public class TestFutureCahe<K, V> {
private final ConcurrentHashMap<K, Future<V>> cacheMap = new ConcurrentHashMap<K, Future<V>>();

View File

@ -1,7 +1,7 @@
package me.ehlxr;
/**
* Created by lixiangrong on 2016/12/23.
* Created by ehlxr on 2016/12/23.
*/
public class dfd {

View File

@ -12,7 +12,7 @@ import java.util.Arrays;
import java.util.List;
/**
* @author lixiangrong
* @author ehlxr
* @since 2020/2/26.
*/
public class Main {

View File

@ -23,7 +23,7 @@ public class PackView extends JFrame {
private JButton jb1 = new JButton();
private JButton jb2 = new JButton();
private String inputPath = "D:\\wins-dsp";
private String outputPath = "C:\\Users\\lixiangrong\\Desktop";
private String outputPath = "C:\\Users\\ehlxr\\Desktop";
private JLabel jl0 = new JLabel();
private JButton cancel = new JButton("退出");
private JTextPane jText1 = new JTextPane();

View File

@ -1,7 +1,7 @@
package me.ehlxr.powermock;
/**
* Created by lixiangrong on 2017/11/3.
* Created by ehlxr on 2017/11/3.
*/
public class ClassDependency {
public static boolean isExist() {

View File

@ -1,13 +1,13 @@
package me.ehlxr.powermock;
/**
* Created by lixiangrong on 2017/11/3.
* Created by ehlxr on 2017/11/3.
*/
import java.io.File;
/**
* Created by lixiangrong on 2017/7/21.
* Created by ehlxr on 2017/7/21.
*/
public class ClassUnderTest {
private String f1 = "test";

View File

@ -14,7 +14,7 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.powermock.api.mockito.PowerMockito.*;
/**
* Created by lixiangrong on 2017/7/21.
* Created by ehlxr on 2017/7/21.
*/
@RunWith(PowerMockRunner.class)
public class TestClassUnderTest {

View File

@ -3,7 +3,7 @@ package me.ehlxr.proxy;
import java.util.Random;
/**
* @author lixiangrong
* @author ehlxr
* @since 2019-06-28.
*/
public class Bird implements Flyable {

View File

@ -1,7 +1,7 @@
package me.ehlxr.proxy;
/**
* @author lixiangrong
* @author ehlxr
* @since 2019-06-28.
*/
public interface Flyable {

View File

@ -3,7 +3,7 @@ package me.ehlxr.proxy;
import java.lang.reflect.Proxy;
/**
* @author lixiangrong
* @author ehlxr
* @since 2019-06-28.
*/
public class Main {

View File

@ -5,7 +5,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author lixiangrong
* @author ehlxr
* @since 2019-06-28.
*/
public class MyInvocationHandler implements InvocationHandler {

View File

@ -7,7 +7,7 @@ import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author lixiangrong
* @author ehlxr
* @since 2019-01-22.
*/
public class ConnectionUtil {

View File

@ -11,7 +11,7 @@ import java.util.concurrent.TimeoutException;
/**
* key key
*
* @author lixiangrong
* @author ehlxr
* @since 2019-01-22.
*/
public class Receiver1 {

View File

@ -11,7 +11,7 @@ import java.util.concurrent.TimeoutException;
/**
* key key
*
* @author lixiangrong
* @author ehlxr
* @since 2019-01-22.
*/
public class Receiver2 {

View File

@ -10,7 +10,7 @@ import java.util.concurrent.TimeoutException;
/**
* key key
*
* @author lixiangrong
* @author ehlxr
* @since 2019-01-22.
*/
public class Sender {

View File

@ -11,7 +11,7 @@ import java.util.concurrent.TimeoutException;
/**
*
*
* @author lixiangrong
* @author ehlxr
* @since 2019-01-22.
*/
public class Receiver {

View File

@ -10,7 +10,7 @@ import java.util.concurrent.TimeoutException;
/**
*
*
* @author lixiangrong
* @author ehlxr
* @since 2019-01-22.
*/

View File

@ -11,7 +11,7 @@ import java.util.concurrent.TimeoutException;
/**
* topic # *
*
* @author lixiangrong
* @author ehlxr
* @since 2019-01-22.
*/
@SuppressWarnings("ALL")

View File

@ -11,7 +11,7 @@ import java.util.concurrent.TimeoutException;
/**
* topic # *
*
* @author lixiangrong
* @author ehlxr
* @since 2019-01-22.
*/
public class Receiver2 {

View File

@ -10,7 +10,7 @@ import java.util.concurrent.TimeoutException;
/**
* topic # *
*
* @author lixiangrong
* @author ehlxr
* @since 2019-01-22.
*/
public class Sender {

View File

@ -11,7 +11,7 @@ import java.util.concurrent.TimeoutException;
/**
* work
*
* @author lixiangrong
* @author ehlxr
* @since 2019-01-22.
*/
public class Receiver1 {

View File

@ -11,7 +11,7 @@ import java.util.concurrent.TimeoutException;
/**
* work
*
* @author lixiangrong
* @author ehlxr
* @since 2019-01-22.
*/
public class Receiver2 {

View File

@ -10,7 +10,7 @@ import java.util.concurrent.TimeoutException;
/**
* work
*
* @author lixiangrong
* @author ehlxr
* @since 2019-01-22.
*/
public class Sender {

View File

@ -1,7 +1,7 @@
package me.ehlxr.rate;
/**
* @author lixiangrong
* @author ehlxr
* @since 2019-07-18.
*/
public class Main {

View File

@ -8,7 +8,7 @@ import java.util.concurrent.atomic.AtomicInteger;
/**
*
*
* @author lixiangrong
* @author ehlxr
* @since 2019-07-19.
*/
public class RateBarrier {

View File

@ -4,7 +4,7 @@ import rx.Observable;
import rx.Subscriber;
/**
* Created by lixiangrong on 2018/1/16.
* Created by ehlxr on 2018/1/16.
*/
public class HelloRxJava {

View File

@ -5,7 +5,7 @@ import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;
/**
* Created by lixiangrong on 2018/1/16.
* Created by ehlxr on 2018/1/16.
*/
public class SchedulerTest {
public static void main(String[] args) throws InterruptedException {

View File

@ -5,7 +5,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* @author lixiangrong
* @author ehlxr
* @date 2018/1/16
* <p>
* Future<V> Callable Runable 线

View File

@ -1,7 +1,7 @@
package me.ehlxr.reactive;
/**
* Created by lixiangrong on 2018/1/16.
* Created by ehlxr on 2018/1/16.
*/
public class TestLambda {
public static void main(String[] args) {

View File

@ -4,7 +4,7 @@ import rx.Observable;
import rx.schedulers.Schedulers;
/**
* Created by lixiangrong on 2018/1/16.
* Created by ehlxr on 2018/1/16.
*/
public class TestRx {
public static void main(String[] args) {

View File

@ -7,7 +7,7 @@ import rx.Subscriber;
import java.util.concurrent.CountDownLatch;
/**
* Created by lixiangrong on 2018/1/16.
* Created by ehlxr on 2018/1/16.
*
*/
public class TestRx01 {

View File

@ -4,7 +4,7 @@ import java.util.Arrays;
import java.util.concurrent.Callable;
/**
* Created by lixiangrong on 2018/1/16.
* Created by ehlxr on 2018/1/16.
*/
public class TimeConsumingService implements Callable<String> {

View File

@ -381,7 +381,7 @@ public class JedisUtil {
}
public static void main(String[] args) {
JedisUtil.set("lxr", "hah lixiangrong");
JedisUtil.set("lxr", "hah ehlxr");
System.out.println(JedisUtil.getStr("lxr"));
}
}

View File

@ -17,7 +17,7 @@ import com.jcraft.jsch.SftpException;
/**
* SFTP
*
* @author lixiangrong
* @author ehlxr
*
*/
public class SFTPUtil {

View File

@ -13,7 +13,7 @@ public class ExecBaics50Log {
public static void main(String[] args) throws Exception {
StringBuffer sb = new StringBuffer("");
FileReader reader = new FileReader("C:\\Users\\lixiangrong\\Desktop\\minisite\\20160606\\3\\2016-06-06(3、4、5).txt");
FileReader reader = new FileReader("C:\\Users\\ehlxr\\Desktop\\minisite\\20160606\\3\\2016-06-06(3、4、5).txt");
BufferedReader br = new BufferedReader(reader);
String str = null;
while ((str = br.readLine()) != null) {

View File

@ -3,7 +3,7 @@ package me.ehlxr.test;
import me.ehlxr.redis.JedisUtil;
/**
* Created by lixiangrong on 2016/6/14.
* Created by ehlxr on 2016/6/14.
*/
public class Main {
public static void main(String[] args) {

View File

@ -1,7 +1,7 @@
package me.ehlxr.test;
/**
* Created by lixiangrong on 2016/12/15.
* Created by ehlxr on 2016/12/15.
*/
public class Test {
public static void main(String[] args) {

View File

@ -4,7 +4,7 @@ import java.util.Calendar;
import java.util.TimeZone;
/**
* Created by lixiangrong on 2016/8/11.
* Created by ehlxr on 2016/8/11.
*/
public class TestCalendar {

View File

@ -168,19 +168,19 @@ public class TestCode {
// System.out.println(Integer.valueOf("11", 2));
// System.out.println(AESTool.encrypt("lixiangrong"));
// System.out.println(AESTool.decrypt(AESEncrypter.encrypt("lixiangrong")));
// System.out.println(AESTool.encrypt("ehlxr"));
// System.out.println(AESTool.decrypt(AESEncrypter.encrypt("ehlxr")));
// System.out.println(AESTool.encrypt("liixangrong","adjdjfjfjfjdkdkd"));
// System.out.println(AESTool.decrypt("bfb0c038342ffead45511879853279bf","adjdjfjfjfjdkdkd"));
// System.out.println(Base64.encodeToString(AESTool.encrypt("fa4d7d90618dcba5fa1d969cffc04def","002020202").getBytes(), false));
// byte[] bytes = "lixiangrong".getBytes();
// byte[] bytes = "ehlxr".getBytes();
// for (int i = 0; i < bytes.length; i++) {
// System.out.println(bytes[i]);
// }
// System.out.println(Base64.encodeToString("lixiangrong".getBytes(), false));
// System.out.println(Base64.encodeToString("ehlxr".getBytes(), false));
// double lon1 = 109.0145193759;
// double lat1 = 34.236080797698;

View File

@ -3,7 +3,7 @@ package me.ehlxr.test;
import org.apache.commons.codec.binary.Hex;
/**
* Created by lixiangrong on 2016/9/12.
* Created by ehlxr on 2016/9/12.
*/
public class TestDecodeHex {
// 十六进制转字符串

View File

@ -1,7 +1,7 @@
package me.ehlxr.test;
/**
* Created by lixiangrong on 2016/8/31.
* Created by ehlxr on 2016/8/31.
*/
public class TestException {

View File

@ -7,7 +7,7 @@ import java.io.IOException;
public class TestFile {
public static void main(String[] args) {
File file = new File("C:\\Users\\lixiangrong\\Desktop\\20160628\\161845");
File file = new File("C:\\Users\\ehlxr\\Desktop\\20160628\\161845");
BufferedReader reader = null;
try {

View File

@ -12,7 +12,7 @@ import java.util.Map;
public class TestReadFile {
public static void readFile() {
File file = new File("C:/Users/lixiangrong/Desktop/IPB.txt");
File file = new File("C:/Users/ehlxr/Desktop/IPB.txt");
BufferedReader reader = null;
Map<String, Object> resultMap = null;
List<Long> startList = null;
@ -52,7 +52,7 @@ public class TestReadFile {
}
public static void readFile1() {
File file = new File("C:\\Users\\lixiangrong\\Desktop\\白名单\\IMEI\\000000_0");
File file = new File("C:\\Users\\ehlxr\\Desktop\\白名单\\IMEI\\000000_0");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));

View File

@ -1,7 +1,7 @@
package me.ehlxr.test;
/**
* Created by lixiangrong on 2017/4/14.
* Created by ehlxr on 2017/4/14.
*/
public class TestReference {
public static void main(String[] args) {

View File

@ -8,7 +8,7 @@ import java.util.zip.CRC32;
/**
* IntelliJ IDEA 14.0.1
*
* @author lixiangrong
* @author ehlxr
*
*/
public class keygen {

View File

@ -4,7 +4,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author lixiangrong
* @author ehlxr
* @date 2016/12/22
*/
//@SuppressWarnings({"AlibabaRemoveCommentedCode", "Convert2Lambda", "UnnecessaryLocalVariable"})

View File

@ -5,7 +5,7 @@ package me.ehlxr.token;
import java.io.IOException;
/**
* Created by lixiangrong on 2018/2/2.
* Created by ehlxr on 2018/2/2.
*/
public class TokenServerUtil {

View File

@ -1,7 +1,7 @@
package me.ehlxr.utils;
/**
* Created by lixiangrong on 2017/3/18.
* Created by ehlxr on 2017/3/18.
*/
import java.io.File;

View File

@ -0,0 +1,67 @@
package me.ehlxr.utils;
/**
*
*
* @author ehlxr
* @since 2020/3/18.
*/
public enum Code {
/**
*
*/
SUCCESSFUL(200, "success"),
/**
*
*/
UNKNOWN_EXCEPTION(600, "unknown server exception"),
/**
*
*/
REQUEST_PARAM_NULL_EXCEPTION(601, "required param should not be null"),
/**
*
*/
SERVICE_EXCEPTION(602, "service exception");
private final int code;
private final String message;
Code(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
@Override
public String toString() {
return "Code{" +
"code=" + code +
", message='" + message + '\'' +
'}';
}
private static final Code[] CODES = Code.values();
public static Code code(int code) {
for (Code c : CODES) {
if (code == c.getCode()) {
return c;
}
}
return null;
}
}

View File

@ -0,0 +1,87 @@
package me.ehlxr.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Java shell
*
* @author ehlxr
* @since 2020/5/6.
*/
public class ExecShell {
private static final Logger log = LoggerFactory.getLogger(ExecShell.class);
public static void execShellCommand(final String command) {
Process process = null;
DataOutputStream dataOutputStream = null;
try {
process = Runtime.getRuntime().exec("/bin/sh");
dataOutputStream = new DataOutputStream(process.getOutputStream());
dataOutputStream.writeBytes(command + "\n");
dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
readStdStream(process);
process.waitFor();
} catch (IOException | InterruptedException | IllegalMonitorStateException e) {
log.error("execShellCommand: {} error", command, e);
throw new RuntimeException(e);
} finally {
try {
if (dataOutputStream != null) {
dataOutputStream.close();
}
if (process != null) {
process.destroy();
}
} catch (IOException e) {
log.error("execShellCommand: {} close stream error", command, e);
}
}
}
/**
* Shell
*/
private static void readStdStream(final Process process) {
String readLine;
BufferedReader stdInput = null;
BufferedReader stdError = null;
try {
// 读取Shell进程标准输出流
stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((readLine = stdInput.readLine()) != null) {
log.info("[stdInputStream]: {}", readLine);
}
// 读取Shell进程标准错误流
stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((readLine = stdError.readLine()) != null) {
log.error("[stdErrorStream]: {}", readLine);
}
} catch (IOException e) {
log.error("readStdStream error", e);
throw new RuntimeException(e);
} finally {
try {
if (stdInput != null) {
stdInput.close();
}
if (stdError != null) {
stdError.close();
}
} catch (IOException e) {
log.error("readStdStream close stream error", e);
}
}
}
public static void main(String[] args) {
ExecShell.execShellCommand("pwd");
}
}

View File

@ -0,0 +1,14 @@
package me.ehlxr.utils;
/**
* FORM
*
* @author ehlxr
* @since 2020/4/27.
*/
public enum FormType {
/**
* form
*/
FILE, STRING
}

View File

@ -7,7 +7,7 @@ import java.util.HashMap;
/**
* GeoHash
*
* @author lixiangrong
* @author ehlxr
*
* 1GeoHash
* 2GeoHashwx4g0ec19

View File

@ -0,0 +1,34 @@
package me.ehlxr.utils;
/**
* @author ehlxr
* @since 2020/5/19.
*/
public enum HttpContentType {
/**
* key
*/
KEY("Content-Type"),
/**
* JSON
*/
JSON("application/json; charset=utf-8"),
/**
* form data
*/
FORMBODY("application/x-www-form-urlencoded");
HttpContentType(String v) {
this.v = v;
}
private String v;
public String value() {
return v;
}
public void setV(String v) {
this.v = v;
}
}

View File

@ -0,0 +1,116 @@
package me.ehlxr.utils;
import com.google.common.collect.Maps;
import okhttp3.*;
import java.io.File;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @author ehlxr
* @since 2020/4/20.
*/
public class HttpUtil {
private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.MINUTES)
.readTimeout(1, TimeUnit.MINUTES)
.build();
public static String get(String url, Map<String, String> headers) {
String resp;
try {
Request request = new Request.Builder()
.url(url)
.headers(Headers.of(headers))
.build();
Response response = OK_HTTP_CLIENT.newCall(request).execute();
if (response.isSuccessful()) {
resp = response.body() != null ? response.body().string() : "";
} else {
throw new RuntimeException("send get http request failed. Unexpected code: " + response);
}
} catch (Exception e) {
throw new RuntimeException("send get http request failed.", e);
}
return resp;
}
public static String get(String url) {
return get(url, Maps.newHashMap());
}
public static String post(String url, Map<String, String> headers, String body) {
String resp;
try {
MediaType mediaType = MediaType.parse(headers.getOrDefault(HttpContentType.KEY.value(), HttpContentType.JSON.value()));
Request request = new Request.Builder()
.url(url)
.headers(Headers.of(headers))
.post(RequestBody.create(mediaType, body))
.build();
Response response = OK_HTTP_CLIENT.newCall(request).execute();
if (response.isSuccessful()) {
resp = response.body() != null ? response.body().string() : "";
} else {
throw new RuntimeException("send post http request failed. Unexpected code: " + response);
}
} catch (Exception e) {
throw new RuntimeException("send post http request failed.", e);
}
return resp;
}
public static String post(String url, String body) {
return post(url, Maps.newHashMap(), body);
}
public static String formPost(String url, Map<String, Map<FormType, Object>> params) {
return formPost(url, params, Maps.newHashMap());
}
public static String formPost(String url, Map<String, Map<FormType, Object>> params, Map<String, String> header) {
String resp;
try {
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
params.forEach((key, value) -> value.forEach((k, v) -> {
if (k.equals(FormType.FILE)) {
File file = (File) v;
builder.addFormDataPart(
key,
file.getName(),
RequestBody.create(MediaType.parse("application/octet-stream"), file));
} else if (k.equals(FormType.STRING)) {
builder.addFormDataPart(key, String.valueOf(v));
}
}));
Request request = new Request.Builder()
.url(url)
.post(builder.build())
.headers(Headers.of(header))
.build();
Response response = OK_HTTP_CLIENT.newCall(request).execute();
if (response.isSuccessful()) {
resp = response.body() != null ? response.body().string() : "";
} else {
throw new RuntimeException("send multipart post http request failed. Unexpected code: " + response);
}
} catch (Exception e) {
throw new RuntimeException("send multipart post http request failed.", e);
}
return resp;
}
public static String appendParam(Map<String, Object> param) {
StringBuilder sb = new StringBuilder();
param.forEach((k, v) -> sb.append(k).append("=").append(v).append("&"));
return sb.substring(0, sb.length() - 1);
}
}

View File

@ -0,0 +1,225 @@
package me.ehlxr.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* JSON
*
* @author ehlxr
* @since 2020/5/6.
*/
@SuppressWarnings("unchecked")
public class JsonUtils {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
static {
// 对象的所有字段全部列入
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.ALWAYS);
// 取消默认转换 timestamps 形式
OBJECT_MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
// 忽略空 bean 转 JSON 的错误
OBJECT_MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
// 所有的日期格式都统一为以下的样式yyyy-MM-dd HH:mm:ss
OBJECT_MAPPER.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT));
// 忽略在 JSON 字符串中存在,但是在 java 对象中不存在对应属性的情况
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public static ObjectMapper om() {
return OBJECT_MAPPER;
}
/**
* JsonNode
*
* @param obj
* @param <T>
* @return {@link JsonNode}
*/
public static <T> JsonNode obj2JsonNode(T obj) {
try {
return OBJECT_MAPPER.readTree(obj2String(obj));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
/**
* JSON
*
* @param obj
* @param <T>
* @return JSON
*/
public static <T> String obj2String(T obj) {
if (obj == null) {
return "";
}
try {
return obj instanceof String ? (String) obj : OBJECT_MAPPER.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
/**
* JSON
*
* @param obj
* @param <T>
* @return JSON
*/
public static <T> String obj2StringPretty(T obj) {
if (obj == null) {
return "";
}
try {
return obj instanceof String ? (String) obj : OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
/**
*
*
* @param str
* @param clazz class
* @param <T>
* @return
*/
public static <T> T string2Obj(String str, Class<T> clazz) {
if (Strings.isNullOrEmpty(str) || clazz == null) {
throw new RuntimeException("json string to obj param should not empty");
}
try {
return clazz.equals(String.class) ? (T) str : OBJECT_MAPPER.readValue(str, clazz);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
/**
*
*
* @param str
* @param typeReference typeReference
* @param <T>
* @return
*/
public static <T> T string2Obj(String str, TypeReference<T> typeReference) {
if (Strings.isNullOrEmpty(str) || typeReference == null) {
throw new RuntimeException("json string to obj param should not empty");
}
try {
return typeReference.getType().equals(String.class) ? (T) str : OBJECT_MAPPER.readValue(str, typeReference);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
/**
*
*
* @param str
* @param collectionClazz class
* @param elementClazzes class
* @param <T>
* @return
*/
public static <T> T string2Obj(String str, Class<?> collectionClazz, Class<?>... elementClazzes) {
JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametricType(collectionClazz, elementClazzes);
try {
return OBJECT_MAPPER.readValue(str, javaType);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
/**
*
*/
static class User {
private Integer id;
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
public static void main(String[] args) {
try {
User user1 = new User();
user1.setId(1);
user1.setEmail("xrv@live.com");
String userJsonstr = JsonUtils.obj2String(user1);
System.out.println(userJsonstr);
String userJsonPretty = JsonUtils.obj2StringPretty(user1);
System.out.println(userJsonPretty);
User user2 = JsonUtils.string2Obj(userJsonstr, User.class);
user2.setId(2);
user2.setEmail("ehlxr.me@gmail.com");
List<User> userList = new ArrayList<>();
userList.add(user1);
userList.add(user2);
String userListJson = JsonUtils.obj2String(userList);
System.out.println(userListJson);
List<User> userListBean = JsonUtils.string2Obj(userListJson, new TypeReference<List<User>>() {
});
if (userListBean != null) {
userListBean.forEach(user -> System.out.println(user.getId() + " : " + user.getEmail()));
}
List<User> userListBean2 = JsonUtils.string2Obj(userListJson, List.class, User.class);
if (userListBean2 != null) {
userListBean2.forEach(user -> System.out.println(user.getId() + " : " + user.getEmail()));
}
Map<String, String> body = ImmutableMap.of("mobile", "13211111222", "realName", "realName");
String obj2String = JsonUtils.obj2String(body);
System.out.println(obj2String);
Map<String, String> stringStringMap = JsonUtils.string2Obj(obj2String, new TypeReference<Map<String, String>>() {
});
stringStringMap.forEach((k, v) -> System.out.println(k + " : " + v));
JsonNode jsonNode = JsonUtils.obj2JsonNode(userList);
System.out.println(jsonNode.path(0));
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -1,7 +1,7 @@
package me.ehlxr.utils;
/**
* Created by lixiangrong on 2017/3/15.
* Created by ehlxr on 2017/3/15.
*/
import java.io.File;

View File

@ -0,0 +1,149 @@
package me.ehlxr.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Objects;
/**
*
*
* @author ehlxr
* @since 2020/3/18.
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {
private static final long serialVersionUID = 2247338010219468245L;
/**
*
*/
private int code;
/**
*
*/
private String message;
/**
*
*/
private T data;
private Result() {
}
private Result(int code, T data, String message) {
this.code = code;
this.data = data;
this.message = message;
}
public static <T> Result<T> success(T data, String message) {
return new Result<>(Code.SUCCESSFUL.getCode(), data, message);
}
public static <T> Result<T> success(T data) {
return success(data, null);
}
public static <T> Result<T> success() {
return success(null);
}
public static <T> Result<T> of(int c, T d, String m) {
return new Result<>(c, d, m);
}
public static <T> Result<T> of(Code c, T d, String m) {
return new Result<>(c.getCode(), d, m);
}
public static <T> Result<T> fail(Code code, String message) {
return of(code.getCode(), null, message);
}
public static <T> Result<T> fail(Code code) {
return fail(code, code.getMessage());
}
public static <T> Result<T> fail(Throwable e) {
// 格式化异常消息,防止输出异常栈信息到结果集
e = Throwables.getRootCause(e);
return of(Code.UNKNOWN_EXCEPTION.getCode(), null, String.format("%s: %s", e.getClass().getSimpleName(), e.getMessage()));
}
public String getMessage() {
// return Strings.isNullOrEmpty(m) ? c.getMessage() : m;
if (Strings.isNullOrEmpty(message)) {
Code code;
try {
code = Code.code(this.code);
} catch (Exception e) {
return message;
}
return Objects.isNull(code) ? "" : code.getMessage();
}
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getCode() {
// return Objects.nonNull(c) ? c.getCode() : Code.UNKNOWN_EXCEPTION.getCode();
return code;
}
public void setCode(int code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Result<?> result = (Result<?>) o;
return code == result.code &&
Objects.equals(message, result.message) &&
Objects.equals(data, result.data);
}
@Override
public String toString() {
try {
ObjectMapper om = new ObjectMapper();
// 取消时间的转化格式, 默认是时间戳
om.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
// 设置时间格式
om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
om.configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, false);
return om.writeValueAsString(this);
} catch (JsonProcessingException e) {
return "";
}
}
public static void main(String[] args) {
System.out.println(success());
}
}

View File

@ -1,7 +1,7 @@
package me.ehlxr.utils;
/**
* Created by lixiangrong on 2017/8/17.
* Created by ehlxr on 2017/8/17.
*
*/
public class StringAlign {

View File

@ -14,7 +14,7 @@
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- 配置扫描路径 -->
<context:component-scan base-package="osc.git.eh3.springamq">
<context:component-scan base-package="me.ehlxr.springamq">
<!-- 只扫描Service也可以添加Repostory但是要把Controller排除在外Controller由spring-mvc.xml去加载 -->
<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> -->
<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> -->