forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Nov 18, 2019
1 parent
ecc7a43
commit 08fb49e
Showing
9 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
.../src/main/java/cn/iocoder/springboot/lab22/validation/config/ValidationConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cn.iocoder.springboot.lab22.validation.config; | ||
|
||
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration; | ||
import org.springframework.context.MessageSource; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; | ||
|
||
import javax.validation.Validator; | ||
|
||
@Configuration | ||
public class ValidationConfiguration { | ||
|
||
/** | ||
* 参考 {@link ValidationAutoConfiguration#defaultValidator()} 方法,构建 Validator Bean | ||
* | ||
* @return Validator 对象 | ||
*/ | ||
@Bean | ||
public Validator validator(MessageSource messageSource) { | ||
// 创建 LocalValidatorFactoryBean 对象 | ||
LocalValidatorFactoryBean validator = ValidationAutoConfiguration.defaultValidator(); | ||
// 设置 messageSource 属性,实现 i18 国际化 | ||
validator.setValidationMessageSource(messageSource); | ||
// 返回 | ||
return validator; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/dto/UserUpdateDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cn.iocoder.springboot.lab22.validation.dto; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
/** | ||
* 用户更新 DTO | ||
*/ | ||
public class UserUpdateDTO { | ||
|
||
/** | ||
* 用户编号 | ||
*/ | ||
@NotNull(message = "{UserUpdateDTO.id.NotNull}") | ||
private Integer id; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public UserUpdateDTO setId(Integer id) { | ||
this.id = id; | ||
return this; | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...tion-01/src/main/java/cn/iocoder/springboot/lab22/validation/dto/UserUpdateStatusDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cn.iocoder.springboot.lab22.validation.dto; | ||
|
||
import javax.validation.constraints.AssertFalse; | ||
import javax.validation.constraints.AssertTrue; | ||
|
||
/** | ||
* 用户更新状态 DTO | ||
*/ | ||
public class UserUpdateStatusDTO { | ||
|
||
/** | ||
* 分组 01 ,要求状态必须为 true | ||
*/ | ||
public interface Group01 {} | ||
|
||
/** | ||
* 状态 02 ,要求状态必须为 false | ||
*/ | ||
public interface Group02 {} | ||
|
||
/** | ||
* 状态 | ||
*/ | ||
@AssertTrue(message = "状态必须为 true", groups = Group01.class) | ||
@AssertFalse(message = "状态必须为 false", groups = Group02.class) | ||
private Boolean status; | ||
|
||
public Boolean getStatus() { | ||
return status; | ||
} | ||
|
||
public UserUpdateStatusDTO setStatus(Boolean status) { | ||
this.status = status; | ||
return this; | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
lab-22/lab-22-validation-01/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
spring: | ||
# i18 message 配置,对应 MessageSourceProperties 配置类 | ||
messages: | ||
basename: i18n/messages # 文件路径基础名 | ||
encoding: UTF-8 # 使用 UTF-8 编码 |
1 change: 1 addition & 0 deletions
1
lab-22/lab-22-validation-01/src/main/resources/i18n/messages.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
UserUpdateDTO.id.NotNull=用户编号不能为空 |
1 change: 1 addition & 0 deletions
1
lab-22/lab-22-validation-01/src/main/resources/i18n/messages_en.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
UserUpdateDTO.id.NotNull=userId cannot be empty |
1 change: 1 addition & 0 deletions
1
lab-22/lab-22-validation-01/src/main/resources/i18n/messages_ja.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
UserUpdateDTO.id.NotNull=ユーザー番号は空にできません |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters