介绍
基础公共模块提供简单便捷的实用类及框架所依赖的公共类。
依赖库
名称 | 描述 |
---|---|
hutool-all | |
transmittable-thread-local | |
spring-boot | |
bcprov-jdk15on |
快速开始
如果引入了zebra-spring-boot-starter-web则不需要引入以下坐标
<dependency>
<groupId>io.github.zhanghongbin</groupId>
<artifactId>zebra-common-util</artifactId>
</dependency>
core 模块
GeneralYmlPropertySourceFactory 类
读取yml文件类,使用方式如下
- 在类上使用PropertySource注解
@PropertySource(value = "classpath:satoken-springboot.yml", factory = GeneralYmlPropertySourceFactory.class)
public class SaTokenConfig {
}
- 手动读取yml文件
Resource resource = new ClassPathResource("druid-springboot.yml");
// 加载成PropertySource对象,并添加到Environment环境中
GeneralYmlPropertySourceFactory generalYmlPropertySourceFactory = new GeneralYmlPropertySourceFactory();
PropertiesPropertySource propertySource = generalYmlPropertySourceFactory.load(resource);
PageQuery 类
分页抽象类,需要被继承,主要要是为了规范化分页参数,具体参见mybatisplus增强模块中中对PageQuery相关说明。
SortQuery 类
排序抽象类,需要被继承,主要要是为了规范化排序参数。
Result 类
controller 接口返回类,一般情况下不需要关心,有以下字段组成
字段名称 | 类型 | 描述 |
---|---|---|
code | Integer | |
msg | String | |
data | T | 返回结果 |
R 类
对Result的封装和简化异常处理,所有方法均为静态方法。
方法名称 | 参数 | 返回值 | 描述 |
---|---|---|---|
failed | FailCode | void | 抛出错误异常 |
failed | void | void | 抛出-1的异常 |
failed | void | String | 抛出-1的异常,并指定异常描述 |
failed | void | String | 抛出-1的异常,并指定异常描述 |
ok | void | Result | 返回状态码为0的result对象 |
ok | T | Result | 返回状态码为0的result对象,并指定结果 |
error | void | Result | 返回状态码为-1的result对象,并指定msg描述 |
error | FailCode | Result | 返回FailCode内容的result对象 |
ValidGroup 类
用于分组校验
public interface ValidGroup {
interface Update extends ValidGroup {}
interface Create extends ValidGroup {}
interface Delete extends ValidGroup {}
interface Query extends ValidGroup {}
}
exception 模块
FailCode 接口
自定义异常,具体使用方式参见web增强模块。
Failed 注解
自定义异常,具体使用方式参见web增强模块。
ResultException 类
建议Controller 层业务异常都使用此类,或者此类的子类。
StatusCode 枚举
http 响应状态码枚举,详细描述参见web增强模块。
crypto 模块
CryptoProvider 接口
加解密接口
方法名称 | 参数 | 返回值 | 描述 |
---|---|---|---|
encrypt | String | String | 加密 |
decrypt | String | String | 解密 |
AesCryptoProvider 类
aes加解密算法,实现了CryptoProvider接口,此类提供一个静态方法 generateSecretKey 用来生成 key, 加密使用的是hutool SecureUtil.aes 方法。
RsaCryptoProvider 类
rsa 加密算法,加密使用的是 hutool SecureUtil.rsa 方法,并提供以下静态方法生成公钥和私钥
public static Map<String, String> generateKey() {
Map<String, String> keyMap = new HashMap<>(2);
RSA rsa = SecureUtil.rsa();
keyMap.put("privateKey", rsa.getPrivateKeyBase64());
keyMap.put("publicKey", rsa.getPublicKeyBase64());
return keyMap;
}
Sm2CryptoProvider 类
Sm2 加密算法,加密使用的是 hutool SecureUtil.sm2 方法,并提供以下静态方法生成公钥和私钥
public static Map<String, String> generateKey() {
Map<String, String> keyMap = new HashMap<>(2);
SM2 sm2 = SmUtil.sm2();
keyMap.put("privateKey", sm2.getPrivateKeyBase64());
keyMap.put("publicKey", sm2.getPublicKeyBase64());
return keyMap;
}
util 模块
SignUtil 类
SignUtil提供对接口参数进行签名操作,用来验证调用者的合法性。
方法名称 | 参数 | 返回值 | 描述 |
---|---|---|---|
addSignParamsAndJoin | String secretKey, Map<String, Object> paramsMap | String | 返回 timestamp、nonce、sign 三个参数,并转换为参数字符串,列如: sign=9685352da6e70eaf2596a43bf2a41e12&nonce=M7eXmadIhz1bNFYBZgLyUN2OW4PxM4YS×tamp=1739757707610 |
createSign | String secretKey, Map<String, Object> paramsMap | String | 生成签名 |
具体示例参见web增强模块。
ReflectPlusUtil 类
反射操作,继承 hutool ReflectUtil类,并有以下方法:
方法名称 | 参数 | 返回值 | 描述 |
---|---|---|---|
invokeGetter | Object obj, String | E 泛型 | 调用Getter方法,支持多级,如:对象名.对象名.方法 |
invokeSetter | Object obj, String propertyName, E value | void | 调用Setter方法, 仅匹配方法名。支持多级,如:对象名.对象名.方法 |
ValidatorUtil 类
数据的有效性校验,静态方法如下:
方法名称 | 参数 | 返回值 | 描述 |
---|---|---|---|
validate | T object, Class<?>... groups | void | 对给定对象进行参数校验,并根据指定的校验组进行校验,如果不通过则抛出ConstraintViolationException异常 |
UriWhiteUtil 类
uri 白名单,用来处理一些框架不需要拦截的服务地址,默认白名单有以下uri不进行拦截:
uri |
---|
/koTime/** |
/instances/** |
/error/** |
/applications/** |
/actuator/** |
/swagger-resources/** |
/v2/** |
/webjars/** |
/doc.html |
url 编写规则参见spring PathMatcher 对uri处理方式。
只提供一个静态方法获取白名单
public static Set<String> getDefaultUriWhite() {
return white;
}
SpringElUtil 类
对springel进行简单封装,提供以下静态方法:
方法名称 | 参数 | 返回值 | 描述 |
---|---|---|---|
getStandardEvaluationContext | String name, Object value | StandardEvaluationContext | 获取StandardEvaluationContext对象 |
getStandardEvaluationContext | Map<String, Object> variable | StandardEvaluationContext | 获取StandardEvaluationContext对象 |
getMethodBasedEvaluationContext | Method method, Object[] args | MethodBasedEvaluationContext | 获取MethodBasedEvaluationContext对象 |
parse | String el, StandardEvaluationContext context, Class<T> type | T | 解析表达式 |
spring el 支持 #{#对象名.属性名} 和 #对象名.属性名 两种方式,相关示例如下:
@Test
void elTest1(){
// map 方式
Map<String,Object> map = new HashMap<>();
map.put("id",1);
StandardEvaluationContext standardEvaluationContext = SpringElUtil.getStandardEvaluationContext(map);
System.out.println(SpringElUtil.parse("#id",standardEvaluationContext,Integer.class));
}
@Test
void elTest2(){
// bean 方式
SysTest sysTest = new SysTest();
sysTest.setId("xxxx");
StandardEvaluationContext standardEvaluationContext = SpringElUtil.getStandardEvaluationContext("sysTest",sysTest);
System.out.println(SpringElUtil.parse("#sysTest.id",standardEvaluationContext,String.class));
System.out.println(SpringElUtil.parse("#{#sysTest.id}",standardEvaluationContext,String.class));
}
@Test
void elTest3(){
// 调用 spring bean 方法 , testServiceImpl 为 springbean 名称 getNameById 为方法 ,x 为方法参数
StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
System.out.println(SpringElUtil.parse("#{@testServiceImpl.getNameById('x')}", standardEvaluationContext, String.class));
//或
System.out.println(SpringElUtil.parse("@testServiceImpl.getNameById('x')", standardEvaluationContext, String.class));
}