Skip to content

Commit b507114

Browse files
committed
docs: 完善文档 使用指南
1 parent b288548 commit b507114

File tree

3 files changed

+141
-69
lines changed

3 files changed

+141
-69
lines changed

document/web-docs/docs/guide/getting-started.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Latest Version:
1818
<dependency>
1919
<groupId>org.hibernate.validator</groupId>
2020
<artifactId>hibernate-validator</artifactId>
21-
<version>${hibernate-validator.version}</version>
21+
<version>6.2.5.Final</version>
2222
</dependency>
2323

2424
<dependency>

document/web-docs/docs/guide/spel.md

+53-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,56 @@
11
# SpEL 表达式
22

3-
暂时没写
3+
本章只介绍一些重要的 SpEL 表达式用法,更详细的使用说明请参考官方文档。
44

5-
可以先看看官方文档:[Spring Expression Language (SpEL)](https://docs.spring.io/spring-framework/reference/core/expressions.html)
5+
官方文档:[Spring Expression Language (SpEL)](https://docs.spring.io/spring-framework/reference/core/expressions.html)
6+
7+
::: tip
8+
9+
如果你的 IDEA 版本比较新,那么不出意外的话,IDEA 能够识别到表达式,并且会给出提示,也具备引用的功能。
10+
11+
:::
12+
13+
## 调用静态方法
14+
15+
调用静态方法的语法为:`T(全类名).方法名(参数)`
16+
17+
```java
18+
@Data
19+
@SpelValid
20+
public class SimpleExampleParamVo {
21+
22+
/**
23+
* 枚举值字段校验
24+
*/
25+
@SpelAssert(assertTrue = " T(cn.sticki.enums.UserStatusEnum).getByCode(#this.userStatus) != null ", message = "用户状态不合法")
26+
private Integer userStatus;
27+
28+
// 中文算两个字符,英文算一个字符,要求总长度不超过 10
29+
// 调用外部静态方法进行校验
30+
@SpelAssert(assertTrue = "T(cn.sticki.util.StringUtil).getLength(#this.userName) <= 10", message = "用户名长度不能超过10")
31+
private String userName;
32+
33+
}
34+
```
35+
36+
## 调用 Spring Bean
37+
38+
[开启 Spring Bean 支持](user-guide.md#开启对-spring-bean-的支持)后,即可在 SpEL 表达式中调用 Spring Bean。
39+
40+
调用 Bean 的语法为:`@beanName.methodName(参数)`
41+
42+
```java
43+
@Data
44+
@SpelValid
45+
public class SimpleExampleParamVo {
46+
47+
/**
48+
* 调用 userService 的 getById 方法,判断用户是否存在
49+
* 校验失败时,提示信息为:用户不存在
50+
* 这里只是简单举例,实际开发中不建议这样判断用户是否存在
51+
*/
52+
@SpelAssert(assertTrue = "@userService.getById(#this.userId) != null", message = "用户不存在")
53+
private Long userId;
54+
55+
}
56+
```

document/web-docs/docs/guide/user-guide.md

+87-66
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,50 @@
11
# 使用指南
22

3-
::: tip
3+
::: warning
44

55
本组件的目的不是代替 `javax.validation` 的校验注解,而是作为一个扩展,方便某些场景下的参数校验。
66

77
原则上来说,能够使用 `javax.validation` 处理的场景就不应该使用 `spel-validator`
88

99
:::
1010

11+
## 添加依赖
12+
13+
Latest Version:
14+
[![Maven Central](https://img.shields.io/maven-central/v/cn.sticki/spel-validator.svg)](https://central.sonatype.com/search?q=g:cn.sticki%20a:spel-validator)
15+
16+
大多少情况下,你只需要添加以下两个依赖:
17+
18+
```xml
19+
<dependencys>
20+
<dependency>
21+
<groupId>cn.sticki</groupId>
22+
<artifactId>spel-validator</artifactId>
23+
<version>Latest Version</version>
24+
</dependency>
25+
26+
<!-- 基于 javax.validation 标准的校验器实现类 -->
27+
<dependency>
28+
<groupId>org.hibernate.validator</groupId>
29+
<artifactId>hibernate-validator</artifactId>
30+
<version>6.2.5.Final</version>
31+
</dependency>
32+
</dependencys>
33+
```
34+
35+
如果你的项目中没有任何对于Spring的依赖,那么你需要额外添加一个对SpEL的依赖:
36+
37+
```xml
38+
<dependencys>
39+
<!-- SpEL 依赖项 -->
40+
<dependency>
41+
<groupId>org.springframework</groupId>
42+
<artifactId>spring-expression</artifactId>
43+
<version>${spring.version}</version>
44+
</dependency>
45+
</dependencys>
46+
```
47+
1148
## 开启约束校验
1249

1350
需要满足以下两个条件,才会对带注解的元素进行校验:
@@ -46,6 +83,49 @@ public class SimpleExampleParamVo {
4683

4784
所以,如果需要使用 `SpEL Validator` 进行校验,需要同时满足上述两个条件。
4885

86+
### 设置开启条件
87+
88+
`@SpelValid` 注解包含一个属性 `condition`,支持 SpEL 表达式。
89+
90+
**表达式的算结果为true** 时,表示开启校验,默认情况下开启校验。
91+
92+
```java
93+
@Data
94+
@SpelValid(condition = "1 > 0") /*设置开启校验的条件*/
95+
public class SimpleExampleParamVo {
96+
// ...
97+
}
98+
```
99+
100+
这里的 `condition` 字段同样支持上下文引用,具体使用方式参考 [引用上下文字段](#引用上下文字段)
101+
102+
103+
## 引用上下文字段
104+
105+
设计这套组件的初衷,就是为了满足一些需要判断另一个字段的值来决定当前字段是否校验的场景。
106+
107+
在组件内部,将当前校验的整个类对象作为了 SpEL 表达式解析过程中的根对象,所以在表达式中可以直接引用类中的任意字段。
108+
109+
通过 `#this.fieldName` 的方式来引用当前类对象的字段。
110+
111+
```java
112+
@Data
113+
@SpelValid
114+
public class SimpleExampleParamVo {
115+
116+
private boolean switchAudio;
117+
118+
/**
119+
* 此处引用了上面的 switchAudio 字段
120+
* 当 switchAudio 字段的值为 true 时,才会校验 audioContent 是否为null
121+
*/
122+
@SpelNotNull(condition = "#this.switchAudio == true")
123+
private Object audioContent;
124+
125+
}
126+
```
127+
128+
49129
## 使用约束注解
50130

51131
目前支持的约束注解有:
@@ -85,31 +165,6 @@ public class SimpleExampleParamVo {
85165
}
86166
```
87167

88-
## 引用上下文字段
89-
90-
设计这套组件的初衷,就是为了满足一些需要判断另一个字段的值来决定当前字段是否校验的场景。
91-
92-
在组件内部,将当前校验的整个类对象作为了 SpEL 表达式解析过程中的根对象,所以在表达式中可以直接引用类中的任意字段。
93-
94-
通过 `#this.fieldName` 的方式来引用当前类对象的字段。
95-
96-
```java
97-
@Data
98-
@SpelValid
99-
public class SimpleExampleParamVo {
100-
101-
private boolean switchAudio;
102-
103-
/**
104-
* 此处引用了上面的 switchAudio 字段
105-
* 当 switchAudio 字段的值为 true 时,才会校验 audioContent 是否为null
106-
*/
107-
@SpelNotNull(condition = "#this.switchAudio == true")
108-
private Object audioContent;
109-
110-
}
111-
```
112-
113168
## 分组校验
114169

115170
待补充
@@ -170,34 +225,18 @@ public class TestParamVo2 {
170225
}
171226
```
172227

173-
## 调用静态方法
228+
## 处理异常
174229

175-
利用 SpEL 的特性,可以调用静态方法进行校验。
230+
待补充
176231

177-
调用静态方法的格式为:`T(全类名).方法名(参数)`
232+
当校验失败时,本组件会将异常信息上报到 `javax.validation` 的异常体系中
178233

179-
如果你的 IDEA 版本比较新,那么不出意外的话,IDEA 能够识别到表达式,并且会给出提示,也具备引用的功能
234+
所以正常情况下,你无需添加额外的异常处理逻辑
180235

181236
```java
182-
@Data
183-
@SpelValid
184-
public class SimpleExampleParamVo {
185-
186-
/**
187-
* 枚举值字段校验
188-
*/
189-
@SpelAssert(assertTrue = " T(cn.sticki.enums.UserStatusEnum).getByCode(#this.userStatus) != null ", message = "用户状态不合法")
190-
private Integer userStatus;
191-
192-
// 中文算两个字符,英文算一个字符,要求总长度不超过 10
193-
// 调用外部静态方法进行校验
194-
@SpelAssert(assertTrue = "T(cn.sticki.util.StringUtil).getLength(#this.userName) <= 10", message = "用户名长度不能超过10")
195-
private String userName;
196-
197-
}
198237
```
199238

200-
## 调用 Spring Bean
239+
## 开启对 Spring Bean 的支持
201240

202241
默认情况下,解析器无法识别 SpEL 表达式中的 Spring Bean。
203242

@@ -216,22 +255,4 @@ public class Application {
216255
}
217256
```
218257

219-
开启 Spring Bean 支持后,即可在 SpEL 表达式中调用 Spring Bean。
220-
221-
> 表达式中的 Bean 名称需要以 `@` 开头
222-
223-
```java
224-
@Data
225-
@SpelValid
226-
public class SimpleExampleParamVo {
227-
228-
/**
229-
* 调用 userService 的 getById 方法,判断用户是否存在
230-
* 校验失败时,提示信息为:用户不存在
231-
* 这里只是简单举例,实际开发中不建议这样判断用户是否存在
232-
*/
233-
@SpelAssert(assertTrue = "@userService.getById(#this.userId) != null", message = "用户不存在")
234-
private Long userId;
235-
236-
}
237-
```
258+
关于使用方法,请参考 [调用 Spring Bean](spel.md#调用-spring-bean)

0 commit comments

Comments
 (0)