mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-11-18 23:47:44 +08:00
Compare commits
14 Commits
1.4.1
...
1.5.0-jre8
| Author | SHA1 | Date | |
|---|---|---|---|
| 933eacc1c2 | |||
| 6ff859b4fb | |||
| 8741562463 | |||
| b97fdf65bc | |||
| 3987f982be | |||
| 556f2a49d2 | |||
| 3b0cdc832b | |||
| 5a96c23882 | |||
| d602d4370e | |||
| 471da3dbcb | |||
| 58d713e025 | |||
| 2cdcf4fc7c | |||
| 75f7aaeb77 | |||
| a7132e9b5b |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -22,4 +22,5 @@ build/
|
||||
nbbuild/
|
||||
dist/
|
||||
nbdist/
|
||||
.nb-gradle/
|
||||
.nb-gradle/
|
||||
*.iml~
|
||||
|
||||
16
README.md
16
README.md
@@ -5,9 +5,9 @@ github:https://github.com/yexuejc/yexuejc-base
|
||||
gitee:https://gitee.com/jzsw-it/yexuejc-base
|
||||
|
||||
### 说明
|
||||
>1. 支持环境:java8
|
||||
>2. 该工具包基于springboot提取,按理说适用于所有java工程
|
||||
>3. 其中依赖jjwt、validation-api,排除请使用
|
||||
1. 支持环境:java8(1.5.0开始支持java11,请使用`x.x.x-jre11`版本)
|
||||
2. 该工具包基于springboot提取,按理说适用于所有java工程
|
||||
3. 其中依赖jjwt相关、validation-api,排除请使用
|
||||
> ```
|
||||
> <exclusions>
|
||||
> <exclusion>
|
||||
@@ -17,13 +17,13 @@ gitee:https://gitee.com/jzsw-it/yexuejc-base
|
||||
> </exclusions>
|
||||
> ```
|
||||
>
|
||||
>4. `1.1.9` 升级JWT为单例类
|
||||
>5. `1.2.3` 修复RSA加密(签名)Base64Url 问题,如需使用RSA请使用1.2.3+
|
||||
>##### 6. 从`1.3.0`开始,变更组织`groupId`为`top.yexuejc`。使用者请尽快升级到`1.3.0`以上(1.3.0代码向下兼容)
|
||||
|
||||
4. `1.1.9` 升级JWT为单例类
|
||||
5. `1.2.3` 修复RSA加密(签名)Base64Url 问题,如需使用RSA请使用1.2.3+
|
||||
6. 从`1.3.0`开始,变更组织`groupId`为`top.yexuejc`。使用者请尽快升级到`1.3.0`以上(1.3.0代码向下兼容)
|
||||
7. 从`1.5.0`开始,版本分为`1.5.0-jre8`和`1.5.0-jre11`,分别对于jre8和jre11使用
|
||||
|
||||
### 使用
|
||||
>yexuejc.base.version=1.4.1
|
||||
>yexuejc.base.version=1.5.0-jre8
|
||||
|
||||
pom.xml
|
||||
```
|
||||
|
||||
56
UPDATE.md
56
UPDATE.md
@@ -1,6 +1,62 @@
|
||||
yexuejc-base 更新记录
|
||||
------------------
|
||||
|
||||
#### version :1.5.0-jre8
|
||||
**time:2022-5-9 13:37:31** <br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
1. 升级jwt相关包,去除`io.jsonwebtoken:jjwt:0.9.1`(不再维护升级),
|
||||
取而代之的是引入以下包,升级相关依赖
|
||||
```
|
||||
io.jsonwebtoken:jjwt-api:0.11.5
|
||||
io.jsonwebtoken:jjwt-impl:0.11.5
|
||||
io.jsonwebtoken:jjwt-jackson:0.11.5
|
||||
org.bouncycastle:bcprov-jdk15on:1.70
|
||||
```
|
||||
2. 升级
|
||||
```
|
||||
jakarta.validation:jakarta.validation-api:3.0.1
|
||||
org.apache.poi:poi:5.2.2
|
||||
com.google.guava:guava:31.1-jre
|
||||
commons-io:commons-io:2.11.0
|
||||
```
|
||||
4. 去除依赖
|
||||
```
|
||||
<commons-codec.version>1.15</commons-codec.version>
|
||||
<!--base64使用到的依赖-->
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
```
|
||||
#
|
||||
#### version :1.4.5
|
||||
**time:2022-5-9 13:37:31** <br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
>1. 扩展FileUtil,优化Base64的包
|
||||
#
|
||||
#### version :1.4.4
|
||||
**time:2021-4-24 00:41:31** <br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
>1. JsonUtil类修复格式化出现的时区差:现使用TimeZone.getDefault()时区
|
||||
#
|
||||
#### version :1.4.3
|
||||
**time:2021-2-6 11:42:49** <br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
>1. DateTimeUtil类增加时间格式
|
||||
#
|
||||
#### version :1.4.2
|
||||
**time:2021-2-3 11:40:11** <br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
>1. 优化时间转换格式
|
||||
>2. 增加注解 ToUeProperty 转换时间为时间戳的功能
|
||||
#
|
||||
#### version :1.4.1
|
||||
**time:2021-1-31 12:59:24** <br/>
|
||||
**branch:** master <br/>
|
||||
|
||||
50
pom.xml
50
pom.xml
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>top.yexuejc</groupId>
|
||||
<artifactId>yexuejc-base</artifactId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.5.0-jre8</version>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
<url>https://github.com/yexuejc/yexuejc-base</url>
|
||||
@@ -40,18 +40,17 @@
|
||||
|
||||
<properties>
|
||||
<repos.yexuejc.url>https://nexus.yexuejc.top/repository/</repos.yexuejc.url>
|
||||
<repos.hm.url>https://nexus.hm3981.com/repository/</repos.hm.url>
|
||||
<repos.aliyun.url>http://maven.aliyun.com/nexus/content/groups/public</repos.aliyun.url>
|
||||
<repos.jitpack.url>https://jitpack.io</repos.jitpack.url>
|
||||
<jjwt.version>0.9.1</jjwt.version>
|
||||
<jjwt.version>0.11.5</jjwt.version>
|
||||
<maven.compiler.verbose>true</maven.compiler.verbose>
|
||||
<java.version>1.8</java.version>
|
||||
<validation-api.version>3.0.0</validation-api.version>
|
||||
<commons-codec.version>1.15</commons-codec.version>
|
||||
<commons-io.version>2.8.0</commons-io.version>
|
||||
<bcprov-jdk15on.version>1.68</bcprov-jdk15on.version>
|
||||
<guava.version>30.1-jre</guava.version>
|
||||
<apache-poi.version>5.0.0</apache-poi.version>
|
||||
<validation-api.version>3.0.1</validation-api.version>
|
||||
|
||||
<commons-io.version>2.11.0</commons-io.version>
|
||||
<bcprov-jdk15on.version>1.70</bcprov-jdk15on.version>
|
||||
<guava.version>31.1-jre</guava.version>
|
||||
<apache-poi.version>5.2.2</apache-poi.version>
|
||||
<!-- 文件拷贝时的编码 -->
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
@@ -63,28 +62,26 @@
|
||||
<!-- JJWT -->
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt</artifactId>
|
||||
<artifactId>jjwt-api</artifactId>
|
||||
<version>${jjwt.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-impl</artifactId>
|
||||
<version>${jjwt.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-jackson</artifactId>
|
||||
<version>${jjwt.version}</version>
|
||||
</dependency>
|
||||
<!-- 数据校验框架 -->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>javax.validation</groupId>-->
|
||||
<!-- <artifactId>validation-api</artifactId>-->
|
||||
<!-- <version>${validation-api.version}</version>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- https://mvnrepository.com/artifact/jakarta.validation/jakarta.validation-api -->
|
||||
<dependency>
|
||||
<groupId>jakarta.validation</groupId>
|
||||
<artifactId>jakarta.validation-api</artifactId>
|
||||
<version>${validation-api.version}</version>
|
||||
</dependency>
|
||||
<!--base64使用到的依赖-->
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!--IOUtils使用到的依赖-->
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
@@ -114,10 +111,15 @@
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
<version>${apache-poi.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>5.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<plugins>
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.yexuejc.base.pojo;
|
||||
|
||||
import com.yexuejc.base.util.JsonUtil;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 分页 VO
|
||||
|
||||
@@ -13,6 +13,11 @@ import java.util.Date;
|
||||
* @date: 2018/3/27 10:44
|
||||
*/
|
||||
public class DateTimeUtil {
|
||||
public static String DATE_PATTERN = "yyyy-MM-dd";
|
||||
public static String TIME_PATTERN = "HH:mm:ss";
|
||||
public static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
||||
public static String DATE_TIME_MS_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";
|
||||
|
||||
private DateTimeUtil() {
|
||||
}
|
||||
|
||||
@@ -281,6 +286,45 @@ public class DateTimeUtil {
|
||||
Instant instant = localDateTime.atZone(zone).toInstant();
|
||||
return instant.toEpochMilli();
|
||||
}
|
||||
|
||||
/**
|
||||
* LocalDate 转 Long
|
||||
*
|
||||
* @param localDate
|
||||
* @return 13位(毫秒)最后3位为0
|
||||
*/
|
||||
public static long parseLong(LocalDate localDate) {
|
||||
ZoneId zone = ZoneId.systemDefault();
|
||||
Instant instant = localDate.atStartOfDay(zone).toInstant();
|
||||
return instant.toEpochMilli();
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间 <br/>
|
||||
* 格式 yyyy-MM-dd HH:mm:ss
|
||||
*
|
||||
* @param dateTime
|
||||
* @return
|
||||
*/
|
||||
public static String format(LocalDate dateTime) {
|
||||
return format(dateTime, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间
|
||||
*
|
||||
* @param dateTime
|
||||
* @param pattern 格式 默认:yyyy-MM-dd
|
||||
* @return
|
||||
*/
|
||||
public static String format(LocalDate dateTime, String pattern) {
|
||||
if (StrUtil.isEmpty(pattern)) {
|
||||
pattern = DATE_PATTERN;
|
||||
}
|
||||
DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
|
||||
return df.format(dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间 <br/>
|
||||
* 格式 yyyy-MM-dd HH:mm:ss
|
||||
@@ -300,8 +344,8 @@ public class DateTimeUtil {
|
||||
* @return
|
||||
*/
|
||||
public static String format(LocalDateTime dateTime, String pattern) {
|
||||
if (pattern.isEmpty()) {
|
||||
pattern = "yyyy-MM-dd HH:mm:ss";
|
||||
if (StrUtil.isEmpty(pattern)) {
|
||||
pattern = DATE_TIME_PATTERN;
|
||||
}
|
||||
DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
|
||||
return df.format(dateTime);
|
||||
|
||||
@@ -21,7 +21,7 @@ public class DateUtil {
|
||||
|
||||
public static DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
|
||||
public static DateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");
|
||||
public static DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
public static DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
public static String DAY_START_TIME = "00:00:00";
|
||||
public static String DAY_END_TIME = "23:59:59";
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import sun.misc.BASE64Encoder;
|
||||
|
||||
import java.io.*;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Base64;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.zip.CRC32;
|
||||
|
||||
@@ -286,7 +285,17 @@ public class FileUtil {
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
public static String base64(File file) {
|
||||
public static String base64ToStr(File file) {
|
||||
return new String(base64(file));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件base64
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
public static byte[] base64(File file) {
|
||||
FileInputStream fileInputStream = null;
|
||||
byte[] data = null;
|
||||
// 读取图片字节数组
|
||||
@@ -300,8 +309,22 @@ public class FileUtil {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 对字节数组Base64编码
|
||||
BASE64Encoder encoder = new BASE64Encoder();
|
||||
return encoder.encode(data);
|
||||
return Base64.getEncoder().encode(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* base64转文件
|
||||
* <p>
|
||||
* <i>
|
||||
* 文件转base64请使用 {@link FileUtil#base64(File)}
|
||||
* </i>
|
||||
*
|
||||
* @param decode {@link FileUtil#base64ToStr(File)} 的结果
|
||||
* @param fileName 文件名称(包含路径)
|
||||
* @return 返回保存地址
|
||||
*/
|
||||
public static String base64ToFile(String decode, String fileName) {
|
||||
return base64ToFile(Base64.getDecoder().decode(decode.getBytes()), fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import sun.misc.BASE64Decoder;
|
||||
import sun.misc.BASE64Encoder;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageReader;
|
||||
@@ -11,6 +9,7 @@ import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.Proxy;
|
||||
import java.net.URL;
|
||||
import java.util.Base64;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
@@ -47,8 +46,7 @@ public class ImgUtil {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 对字节数组Base64编码
|
||||
BASE64Encoder encoder = new BASE64Encoder();
|
||||
return encoder.encode(data.toByteArray());
|
||||
return new String(Base64.getEncoder().encode(data.toByteArray()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -237,7 +235,7 @@ public class ImgUtil {
|
||||
* @return String 编码后的字符串
|
||||
*/
|
||||
public static String encode(byte[] bytes) {
|
||||
return new BASE64Encoder().encode(bytes);
|
||||
return new String(Base64.getEncoder().encode(bytes));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -248,10 +246,7 @@ public class ImgUtil {
|
||||
* @throws IOException
|
||||
*/
|
||||
public static byte[] decode(String encodeStr) throws IOException {
|
||||
byte[] bt = null;
|
||||
BASE64Decoder decoder = new BASE64Decoder();
|
||||
bt = decoder.decodeBuffer(encodeStr);
|
||||
return bt;
|
||||
return Base64.getDecoder().decode(encodeStr);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.type.MapType;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
@@ -41,18 +44,30 @@ public class JsonUtil {
|
||||
* @param objectMapper
|
||||
*/
|
||||
private static void initDefaultObjectMapper(ObjectMapper objectMapper) {
|
||||
objectMapper.setSerializationInclusion(Include.NON_NULL);
|
||||
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
|
||||
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
|
||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
||||
//设置一下时区,可以和程序同步避免时区问题
|
||||
objectMapper.setTimeZone(TimeZone.getDefault());
|
||||
objectMapper.setDateFormat(DateUtil.DATE_TIME_FORMAT);
|
||||
}
|
||||
|
||||
//TODO 待优化
|
||||
public static void initSnakeCase() {
|
||||
//驼峰下划线互转
|
||||
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
|
||||
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 这个设置不能改变JsonUtil自带的objectMapper设置,只能修改传入objMapper的设置
|
||||
*
|
||||
* @param objMapper
|
||||
*/
|
||||
public static void initSnakeCase(ObjectMapper objMapper) {
|
||||
//驼峰下划线互转
|
||||
objMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,6 +81,15 @@ public class JsonUtil {
|
||||
return objectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 ObjectMapper对象,供外部设置特定参数
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static ObjectMapper getObjectMapper() {
|
||||
return objectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将json转换为某个类
|
||||
*
|
||||
@@ -106,7 +130,7 @@ public class JsonUtil {
|
||||
log.warning("json to Object JsonMappingException.\n" + e.getMessage());
|
||||
} catch (IOException e) {
|
||||
log.warning("json to Object IOException.\n" + e.getMessage());
|
||||
}catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -123,8 +147,7 @@ public class JsonUtil {
|
||||
*/
|
||||
public static <T> T json2Obj(String json, Class<T> parametrized, Class<?>... parameterClasses) {
|
||||
T pojo = null;
|
||||
JavaType javaType = objectMapper.getTypeFactory().constructParametrizedType(parametrized, parametrized,
|
||||
parameterClasses);
|
||||
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(parametrized, parameterClasses);
|
||||
try {
|
||||
pojo = objectMapper.readValue(json, javaType);
|
||||
} catch (JsonParseException e) {
|
||||
@@ -165,26 +188,18 @@ public class JsonUtil {
|
||||
/**
|
||||
* Json字符串转换为Java-Map对象
|
||||
*
|
||||
* @param json 字符串
|
||||
* @param mapClass Map 继承类
|
||||
* @param keyType Key 类
|
||||
* @param valueType Value 类
|
||||
* @param json 字符串
|
||||
* @param javaType 具体的java类型
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> T json2Obj(String json, Class<? extends Map> mapClass, JavaType keyType, JavaType valueType) {
|
||||
T pojo = null;
|
||||
MapType mapType = objectMapper.getTypeFactory().constructMapType(mapClass, keyType, valueType);
|
||||
public static <T extends Object> T json2Obj(String json, TypeReference<T> javaType) {
|
||||
try {
|
||||
pojo = objectMapper.readValue(json, mapType);
|
||||
} catch (JsonParseException e) {
|
||||
return objectMapper.readValue(json, javaType);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
||||
} catch (JsonMappingException e) {
|
||||
log.warning("json to Object JsonMappingException.\n" + e.getMessage());
|
||||
} catch (IOException e) {
|
||||
log.warning("json to Object IOException.\n" + e.getMessage());
|
||||
}
|
||||
return pojo;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,8 @@ package com.yexuejc.base.util;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Field;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@@ -110,6 +111,7 @@ public class ObjUtil {
|
||||
}
|
||||
boolean annotationPresent = f.isAnnotationPresent(ToUeProperty.class);
|
||||
boolean ignore = false;
|
||||
Class<?> toType = null;
|
||||
if (annotationPresent) {
|
||||
ToUeProperty annotation = f.getAnnotation(ToUeProperty.class);
|
||||
ignore = annotation.ignore();
|
||||
@@ -117,17 +119,57 @@ public class ObjUtil {
|
||||
if (StrUtil.isNotEmpty(value)) {
|
||||
fName = value;
|
||||
}
|
||||
if (!annotation.type().getClass().equals(ObjUtil.class)) {
|
||||
toType = annotation.type();
|
||||
}
|
||||
}
|
||||
//忽略
|
||||
if (ignore) {
|
||||
continue;
|
||||
}
|
||||
Object o = f.get(obj);
|
||||
if (null == o && !putNull) {
|
||||
continue;
|
||||
}
|
||||
if (null == o || isPrimitive(o) || o instanceof String || o instanceof Enum) {
|
||||
if (null == o && !putNull) {
|
||||
continue;
|
||||
objMap.put(fName, o);
|
||||
} else if (o instanceof Date) {
|
||||
if (toType != null) {
|
||||
Date date = (Date) o;
|
||||
if (toType.equals(Integer.class)) {
|
||||
objMap.put(fName, (int) date.getTime() / 1000);
|
||||
} else if (toType.equals(Long.class)) {
|
||||
objMap.put(fName, date.getTime());
|
||||
}
|
||||
} else {
|
||||
objMap.put(fName, o);
|
||||
}
|
||||
} else if (o instanceof LocalDate) {
|
||||
if (toType != null) {
|
||||
LocalDate date = (LocalDate) o;
|
||||
if (toType.equals(Integer.class)) {
|
||||
objMap.put(fName, (int) DateTimeUtil.parseLong(date) / 1000);
|
||||
} else if (toType.equals(Long.class)) {
|
||||
objMap.put(fName, DateTimeUtil.parseLong(date));
|
||||
} else if (toType.equals(String.class)) {
|
||||
objMap.put(fName, DateTimeUtil.format(date));
|
||||
}
|
||||
} else {
|
||||
objMap.put(fName, o);
|
||||
}
|
||||
} else if (o instanceof LocalDateTime) {
|
||||
if (toType != null) {
|
||||
LocalDateTime date = (LocalDateTime) o;
|
||||
if (toType.equals(Integer.class)) {
|
||||
objMap.put(fName, (int) DateTimeUtil.parseLong(date) / 1000);
|
||||
} else if (toType.equals(Long.class)) {
|
||||
objMap.put(fName, DateTimeUtil.parseLong(date));
|
||||
} else if (toType.equals(String.class)) {
|
||||
objMap.put(fName, DateTimeUtil.format(date));
|
||||
}
|
||||
} else {
|
||||
objMap.put(fName, o);
|
||||
}
|
||||
objMap.put(fName, f.get(obj));
|
||||
} else if (o instanceof List) {
|
||||
List list = (List) o;
|
||||
List bodyList = new ArrayList();
|
||||
@@ -181,80 +223,13 @@ public class ObjUtil {
|
||||
boolean b = obj.getClass().isPrimitive()
|
||||
|| obj instanceof Integer || obj instanceof Character || obj instanceof Boolean
|
||||
|| obj instanceof Number || obj instanceof String || obj instanceof Double || obj instanceof Float
|
||||
|| obj instanceof Short || obj instanceof Long || obj instanceof Byte || obj instanceof Date;
|
||||
|| obj instanceof Short || obj instanceof Long || obj instanceof Byte;
|
||||
if (b) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
B a = new B();
|
||||
a.nameFirst = "张三";
|
||||
a.ageInt = "5165458";
|
||||
a.testAss = "asdasdsad";
|
||||
a.setaM1("method1");
|
||||
a.setbM1("b1Mthod1");
|
||||
a.protectedStr = "protectedStr";
|
||||
C c = new C();
|
||||
c.ageInt = "test";
|
||||
a.c = c;
|
||||
a.list = new ArrayList<>();
|
||||
a.list.add(c);
|
||||
Map<String, Object> underlineMap = getUnderlineMap(a, false, false);
|
||||
System.out.println(JsonUtil.obj2Json(underlineMap));
|
||||
}
|
||||
|
||||
static class A implements Serializable {
|
||||
private static final long serialVersionUID = -8462118058721865488L;
|
||||
public String nameFirst;
|
||||
public String ageInt;
|
||||
private String aM1;
|
||||
@ToUeProperty("p_str")
|
||||
protected String protectedStr;
|
||||
|
||||
public String getaM1() {
|
||||
return aM1;
|
||||
}
|
||||
|
||||
public A setaM1(String aM1) {
|
||||
this.aM1 = aM1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
static class B extends A {
|
||||
private static final long serialVersionUID = -8462118058721865488L;
|
||||
public String testAss;
|
||||
private String bM1;
|
||||
private C c;
|
||||
List<C> list;
|
||||
|
||||
public String getbM1() {
|
||||
return bM1;
|
||||
}
|
||||
|
||||
public B setbM1(String bM1) {
|
||||
this.bM1 = bM1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
static class C extends A {
|
||||
private static final long serialVersionUID = -8462118058721865488L;
|
||||
public String testAss;
|
||||
private String bM1;
|
||||
|
||||
public String getbM1() {
|
||||
return bM1;
|
||||
}
|
||||
|
||||
public C setbM1(String bM1) {
|
||||
this.bM1 = bM1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>深度克隆对象</h2>
|
||||
* <p>
|
||||
@@ -283,56 +258,4 @@ public class ObjUtil {
|
||||
return outer;
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
//// test1();
|
||||
//// test2();
|
||||
//
|
||||
// }
|
||||
//
|
||||
// private static void test2() {
|
||||
// B t = new B();
|
||||
// t.sex = "男";
|
||||
// t.age = 18;
|
||||
// A test = new A();
|
||||
// test.name = "张三";
|
||||
// t.test = test;
|
||||
// B b = depthClone(t);
|
||||
// System.out.println(JsonUtil.obj2Json(b));
|
||||
// }
|
||||
//
|
||||
// static class A implements Serializable {
|
||||
// private static final long serialVersionUID = -8462118058721865488L;
|
||||
// public String name;
|
||||
// }
|
||||
//
|
||||
// static class B implements Serializable {
|
||||
// private static final long serialVersionUID = 3297717505428005316L;
|
||||
// public int age;
|
||||
// public String sex;
|
||||
// public A test;
|
||||
// }
|
||||
//
|
||||
// static class C implements Serializable {
|
||||
// private static final long serialVersionUID = 3297717505428005316L;
|
||||
// public int age;
|
||||
// public String sex;
|
||||
// public A test;
|
||||
// }
|
||||
//
|
||||
// private static void test1() {
|
||||
// ApiVO apiVO = new ApiVO(ApiVO.STATUS.S);
|
||||
// apiVO.setMsg("asdsadsad");
|
||||
// apiVO.setObject1("sadsadsad");
|
||||
//
|
||||
// Resps<String> obj = new Resps<>();
|
||||
// obj.setSucc("安达圣斗士", "ok");
|
||||
// System.out.println(obj);
|
||||
// apiVO.setObject2(obj);
|
||||
// ApiVO apiVO1 = depthClone(apiVO);
|
||||
// System.out.println(apiVO == apiVO1);
|
||||
// System.out.println(JsonUtil.obj2Json(apiVO1));
|
||||
// System.out.println(JsonUtil.obj2Json(apiVO1.getObject1(String.class)));
|
||||
// System.out.println(JsonUtil.obj2Json(apiVO1.getObject2(Resps.class)));
|
||||
// System.out.println(apiVO1.getObject2(Resps.class) == obj);
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -25,4 +25,25 @@ public @interface ToUeProperty {
|
||||
* @return
|
||||
*/
|
||||
boolean ignore() default false;
|
||||
|
||||
/**
|
||||
* 转换格式
|
||||
* <p>目前只支持</p>
|
||||
* <p> {@link java.util.Date}、{@link java.time.LocalDate}、{@link java.time.LocalDateTime}
|
||||
* 转{@link Integer}(10位长度)、{@link Long}(13位长度)</p>
|
||||
* 其余无效
|
||||
*
|
||||
* @return <pre>
|
||||
* this in out <br/>
|
||||
* {@link java.util.Date} {@link Integer} {@link Integer}10位长度时间戳<br/>
|
||||
* {@link java.util.Date} {@link Long} {@link Long}13位长度时间戳<br/>
|
||||
* {@link java.time.LocalDate} {@link Integer} {@link Integer}10位长度时间戳<br/>
|
||||
* {@link java.time.LocalDate} {@link Long} {@link Long}13位长度时间戳(后三位为000)<br/>
|
||||
* {@link java.time.LocalDateTime} {@link Integer} {@link Integer}10位长度时间戳<br/>
|
||||
* {@link java.time.LocalDateTime} {@link Long} {@link Long}13位长度时间戳
|
||||
*
|
||||
* </pre>
|
||||
*/
|
||||
Class<?> type() default ObjUtil.class;
|
||||
|
||||
}
|
||||
57
src/test/java/com/yexuejc/base/util/JsonUtilTest.java
Normal file
57
src/test/java/com/yexuejc/base/util/JsonUtilTest.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class JsonUtilTest {
|
||||
@Test
|
||||
public void test2() {
|
||||
// TestA testA = new TestA("张三", 1, false);
|
||||
// List<TestA> list = new ArrayList<>();
|
||||
// list.add(testA);
|
||||
// list.add(testA);
|
||||
// Map<String, List<TestA>> data = new HashMap<>();
|
||||
// data.put("data",list);
|
||||
// System.out.println(JsonUtil.obj2Json(data));
|
||||
// System.out.println("===========================");
|
||||
String readData="{\"data\":[{\"name\":\"张三\",\"id\":1,\"sex\":false},{\"name\":\"张三\",\"id\":1,\"sex\":false}]}";
|
||||
Map<String, List<TestA>> map = JsonUtil.json2Obj(readData, new TypeReference<Map<String, List<TestA>>>() {
|
||||
});
|
||||
System.out.println(JsonUtil.obj2Json(map));
|
||||
List<TestA> testAS = map.get("data");
|
||||
System.out.println(testAS.getClass().toString());
|
||||
System.out.println(testAS.get(0).getClass().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test1() {
|
||||
TestA testA = new TestA("张三", 1, false);
|
||||
System.out.println(JsonUtil.json2Obj(JsonUtil.obj2Json(testA), Map.class));
|
||||
|
||||
List<TestA> list = new ArrayList<>();
|
||||
list.add(testA);
|
||||
list.add(testA);
|
||||
list.add(testA);
|
||||
System.out.println(JsonUtil.json2Obj(JsonUtil.obj2Json(list), List.class, TestA.class));
|
||||
}
|
||||
|
||||
static class TestA implements Serializable {
|
||||
public String name;
|
||||
public Integer id;
|
||||
public Boolean sex;
|
||||
|
||||
public TestA() {
|
||||
}
|
||||
|
||||
public TestA(String name, Integer id, Boolean sex) {
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
this.sex = sex;
|
||||
}
|
||||
}
|
||||
}
|
||||
154
src/test/java/com/yexuejc/base/util/ObjUtilTest.java
Normal file
154
src/test/java/com/yexuejc/base/util/ObjUtilTest.java
Normal file
@@ -0,0 +1,154 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* json化输出,改变属性名称,输出类型 {@link ObjUtil} + {@link ToUeProperty} 测试类
|
||||
*
|
||||
* @author: yexuejc
|
||||
* @date: 2021-02-03 10:02:55
|
||||
*/
|
||||
class ObjUtilTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
start();
|
||||
}
|
||||
|
||||
public static void start(){
|
||||
B a = new B();
|
||||
a.nameFirst = "张三";
|
||||
a.ageInt = "5165458";
|
||||
a.testAss = "asdasdsad";
|
||||
a.setaM1("method1");
|
||||
a.setbM1("b1Mthod1");
|
||||
a.protectedStr = "protectedStr";
|
||||
a.amount=new BigDecimal("3");
|
||||
a.time = LocalDateTime.now();
|
||||
a.dateTime=new Date();
|
||||
C c = new C();
|
||||
c.ageInt = "test";
|
||||
a.c = c;
|
||||
a.list = new ArrayList<>();
|
||||
a.list.add(c);
|
||||
Map<String, Object> underlineMap = ObjUtil.getUnderlineMap(a, false, false);
|
||||
System.out.println(JsonUtil.formatPrinter(underlineMap));
|
||||
}
|
||||
|
||||
static class A implements Serializable {
|
||||
private static final long serialVersionUID = -8462118058721865488L;
|
||||
public String nameFirst;
|
||||
public String ageInt;
|
||||
private String aM1;
|
||||
@ToUeProperty("p_str")
|
||||
protected String protectedStr;
|
||||
|
||||
public String getaM1() {
|
||||
return aM1;
|
||||
}
|
||||
|
||||
public A setaM1(String aM1) {
|
||||
this.aM1 = aM1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
static class B extends A {
|
||||
private static final long serialVersionUID = -8462118058721865488L;
|
||||
public String testAss;
|
||||
private String bM1;
|
||||
|
||||
@ToUeProperty(type = Integer.class)
|
||||
public BigDecimal amount;
|
||||
|
||||
@ToUeProperty(type = String.class)
|
||||
public LocalDateTime time;
|
||||
|
||||
public Date dateTime;
|
||||
|
||||
private C c;
|
||||
List<C> list;
|
||||
|
||||
public String getbM1() {
|
||||
return bM1;
|
||||
}
|
||||
|
||||
public B setbM1(String bM1) {
|
||||
this.bM1 = bM1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
static class C extends A {
|
||||
private static final long serialVersionUID = -8462118058721865488L;
|
||||
public String testAss;
|
||||
private String bM1;
|
||||
|
||||
public String getbM1() {
|
||||
return bM1;
|
||||
}
|
||||
|
||||
public C setbM1(String bM1) {
|
||||
this.bM1 = bM1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
//// test1();
|
||||
//// test2();
|
||||
//
|
||||
// }
|
||||
//
|
||||
// private static void test2() {
|
||||
// B t = new B();
|
||||
// t.sex = "男";
|
||||
// t.age = 18;
|
||||
// A test = new A();
|
||||
// test.name = "张三";
|
||||
// t.test = test;
|
||||
// B b = depthClone(t);
|
||||
// System.out.println(JsonUtil.obj2Json(b));
|
||||
// }
|
||||
//
|
||||
// static class A implements Serializable {
|
||||
// private static final long serialVersionUID = -8462118058721865488L;
|
||||
// public String name;
|
||||
// }
|
||||
//
|
||||
// static class B implements Serializable {
|
||||
// private static final long serialVersionUID = 3297717505428005316L;
|
||||
// public int age;
|
||||
// public String sex;
|
||||
// public A test;
|
||||
// }
|
||||
//
|
||||
// static class C implements Serializable {
|
||||
// private static final long serialVersionUID = 3297717505428005316L;
|
||||
// public int age;
|
||||
// public String sex;
|
||||
// public A test;
|
||||
// }
|
||||
//
|
||||
// private static void test1() {
|
||||
// ApiVO apiVO = new ApiVO(ApiVO.STATUS.S);
|
||||
// apiVO.setMsg("asdsadsad");
|
||||
// apiVO.setObject1("sadsadsad");
|
||||
//
|
||||
// Resps<String> obj = new Resps<>();
|
||||
// obj.setSucc("安达圣斗士", "ok");
|
||||
// System.out.println(obj);
|
||||
// apiVO.setObject2(obj);
|
||||
// ApiVO apiVO1 = depthClone(apiVO);
|
||||
// System.out.println(apiVO == apiVO1);
|
||||
// System.out.println(JsonUtil.obj2Json(apiVO1));
|
||||
// System.out.println(JsonUtil.obj2Json(apiVO1.getObject1(String.class)));
|
||||
// System.out.println(JsonUtil.obj2Json(apiVO1.getObject2(Resps.class)));
|
||||
// System.out.println(apiVO1.getObject2(Resps.class) == obj);
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user