Skip to content

Commit 6e24a6f

Browse files
authored
merge: 修正readme文档 (#48)
2 parents 5cd4c8a + 7833ca4 commit 6e24a6f

File tree

1 file changed

+6
-139
lines changed

1 file changed

+6
-139
lines changed

README.md

+6-139
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44
[![Maven Central](https://img.shields.io/maven-central/v/cn.sticki/spel-validator-root.svg)](https://central.sonatype.com/search?q=g:cn.sticki%20a:spel-validator-root)
55
[![license](https://img.shields.io/github/license/stick-i/spel-validator)](https://github.com/stick-i/spel-validator/blob/main/LICENSE)
66

7-
一个强大的 Java 参数校验包,基于 SpEL 实现,扩展自 javax.validation 包,用于简化参数校验,几乎支持所有场景下的参数校验。
7+
一个强大的 Java 参数校验包,基于 SpEL 实现,扩展自 jakarta.validation-api 包,用于简化参数校验,几乎支持所有场景下的参数校验。
88

99
## 项目地址
1010

1111
- GitHub:https://github.com/stick-i/spel-validator
1212
- Gitee:https://gitee.com/sticki/spel-validator
13-
- 在线文档https://spel-validator.sticki.cn/
13+
- 项目文档https://spel-validator.sticki.cn/
1414

1515
## 特点
1616

17-
- 简单易用,使用方式几乎与 javax.validation 一致,学习成本低,上手快。
17+
- 简单易用,使用方式几乎与 jakarta.validation-api 一致,学习成本低,上手快。
1818
- 强大的参数校验功能,几乎支持所有场景下的参数校验。
19-
- 扩展自 javax.validation 包,只新增不修改,无缝集成到项目中。
19+
- 扩展自 jakarta.validation-api 包,只新增不修改,无缝集成到项目中。
2020
- 基于 SpEL(Spring Expression Language) 表达式,支持复杂的校验逻辑。
2121
- 支持调用 Spring Bean,可在表达式中使用注入过的 Spring Bean。
2222
- 校验时基于整个对象,支持对象内字段间的校验逻辑。
2323
- 支持自定义校验注解,可根据业务需求自定义校验逻辑。
24-
- 无需额外的异常处理,校验失败时会上报到 javax.validation 的异常体系中。
24+
- 无需额外的异常处理,校验失败时会上报到 jakarta.validation-api 的异常体系中。
2525

2626
## 支持的环境
2727

@@ -70,140 +70,7 @@ JDK8+
7070

7171
## 📦 快速开始
7272

73-
- 添加依赖
74-
75-
Latest Version:
76-
[![Maven Central](https://img.shields.io/maven-central/v/cn.sticki/spel-validator-root.svg)](https://central.sonatype.com/search?q=g:cn.sticki%20a:spel-validator-root)
77-
```xml
78-
<dependency>
79-
<groupId>cn.sticki</groupId>
80-
<artifactId>spel-validator</artifactId>
81-
<version>Latest Version</version>
82-
</dependency>
83-
```
84-
85-
- 在接口参数上使用 `@Valid``@Validated` 注解
86-
87-
```java
88-
@RestController
89-
@RequestMapping("/example")
90-
public class ExampleController {
91-
92-
/**
93-
* 简单校验示例
94-
*/
95-
@PostMapping("/simple")
96-
public Resp<Void> simple(@RequestBody @Valid SimpleExampleParamVo simpleExampleParamVo) {
97-
return Resp.ok(null);
98-
}
99-
100-
}
101-
```
102-
103-
- 在实体类上使用 `@SpelValid` 注解,同时在需要校验的字段上使用 `@SpelNotNull` 等约束注解
104-
105-
```java
106-
@Data
107-
@SpelValid // 添加启动注解
108-
public class SimpleExampleParamVo {
109-
110-
@NotNull
111-
private Boolean switchAudio;
112-
113-
/**
114-
* 此处开启了注解校验
115-
* 当 switchAudio 字段为 true 时,校验 audioContent,audioContent 不能为null
116-
*/
117-
@SpelNotNull(condition = "#this.switchAudio == true", message = "语音内容不能为空")
118-
private Object audioContent;
119-
120-
}
121-
```
122-
123-
- 添加全局异常处理器,处理校验异常
124-
125-
```java
126-
@RestControllerAdvice
127-
public class ControllerExceptionAdvice {
128-
129-
@ExceptionHandler({BindException.class, MethodArgumentNotValidException.class})
130-
public Resp<Void> handleBindException(BindException ex) {
131-
String msg = ex.getFieldErrors().stream()
132-
.map(error -> error.getField() + " " + error.getDefaultMessage())
133-
.reduce((s1, s2) -> s1 + "," + s2)
134-
.orElse("");
135-
return new Resp<>(400, msg);
136-
}
137-
138-
}
139-
```
140-
141-
- 发起请求,即可看到校验结果
142-
<details>
143-
<summary>示例一:@SpelNotNull 校验不通过</summary>
144-
145-
- 请求体:
146-
147-
```json
148-
{
149-
"switchAudio": true,
150-
"audioContent": null
151-
}
152-
```
153-
154-
- 响应体
155-
```json
156-
{
157-
"code": 400,
158-
"message": "audioContent 语音内容不能为空",
159-
"data": null
160-
}
161-
```
162-
163-
</details>
164-
165-
<details>
166-
<summary>示例二:校验通过</summary>
167-
168-
- 请求体
169-
```json
170-
{
171-
"switchAudio": false,
172-
"audioContent": null
173-
}
174-
```
175-
176-
- 响应体
177-
```json
178-
{
179-
"code": 200,
180-
"message": "成功",
181-
"data": null
182-
}
183-
```
184-
185-
</details>
186-
187-
<details>
188-
<summary>示例三:@NotNull 校验不通过</summary>
189-
190-
- 请求体
191-
```json
192-
{
193-
"switchAudio": null,
194-
"audioContent": null
195-
}
196-
```
197-
198-
- 响应体
199-
```json
200-
{
201-
"code": 400,
202-
"message": "switchAudio 不能为null",
203-
"data": null
204-
}
205-
```
206-
</details>
73+
在线文档:https://spel-validator.sticki.cn/guide/getting-started.html
20774

20875
## 📦 示例项目
20976

0 commit comments

Comments
 (0)