From 08fb49e7a835b4c0d032d1ff3763dbcf69b0b87a Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Tue, 19 Nov 2019 01:20:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20spring=20validation=20?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/ValidationConfiguration.java | 29 +++++++++++++++ .../validation/controller/UserController.java | 17 +++++++++ .../lab22/validation/dto/UserUpdateDTO.java | 25 +++++++++++++ .../validation/dto/UserUpdateStatusDTO.java | 37 +++++++++++++++++++ .../src/main/resources/application.yaml | 5 +++ .../main/resources/i18n/messages.properties | 1 + .../resources/i18n/messages_en.properties | 1 + .../resources/i18n/messages_ja.properties | 1 + .../validation/service/UserServiceTest.java | 3 ++ 9 files changed, 119 insertions(+) create mode 100644 lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/config/ValidationConfiguration.java create mode 100644 lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/dto/UserUpdateDTO.java create mode 100644 lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/dto/UserUpdateStatusDTO.java create mode 100644 lab-22/lab-22-validation-01/src/main/resources/application.yaml create mode 100644 lab-22/lab-22-validation-01/src/main/resources/i18n/messages.properties create mode 100644 lab-22/lab-22-validation-01/src/main/resources/i18n/messages_en.properties create mode 100644 lab-22/lab-22-validation-01/src/main/resources/i18n/messages_ja.properties diff --git a/lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/config/ValidationConfiguration.java b/lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/config/ValidationConfiguration.java new file mode 100644 index 000000000..b7186c0e8 --- /dev/null +++ b/lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/config/ValidationConfiguration.java @@ -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; + } + +} diff --git a/lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/controller/UserController.java b/lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/controller/UserController.java index f7c972057..0af8be528 100644 --- a/lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/controller/UserController.java +++ b/lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/controller/UserController.java @@ -1,7 +1,9 @@ package cn.iocoder.springboot.lab22.validation.controller; import cn.iocoder.springboot.lab22.validation.dto.UserAddDTO; +import cn.iocoder.springboot.lab22.validation.dto.UserUpdateDTO; import cn.iocoder.springboot.lab22.validation.dto.UserUpdateGenderDTO; +import cn.iocoder.springboot.lab22.validation.dto.UserUpdateStatusDTO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.validation.annotation.Validated; @@ -32,4 +34,19 @@ public void updateGender(@Valid UserUpdateGenderDTO updateGenderDTO) { logger.info("[updateGender][updateGenderDTO: {}]", updateGenderDTO); } + @PostMapping("/update_status_true") + public void updateStatusTrue(@Validated(UserUpdateStatusDTO.Group01.class) UserUpdateStatusDTO updateStatusDTO) { + logger.info("[updateStatusTrue][updateStatusDTO: {}]", updateStatusDTO); + } + + @PostMapping("/update_status_false") + public void updateStatusFalse(@Validated(UserUpdateStatusDTO.Group02.class) UserUpdateStatusDTO updateStatusDTO) { + logger.info("[updateStatusFalse][updateStatusDTO: {}]", updateStatusDTO); + } + + @PostMapping("/update") + public void update(@Valid UserUpdateDTO updateDTO) { + logger.info("[update][updateDTO: {}]", updateDTO); + } + } diff --git a/lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/dto/UserUpdateDTO.java b/lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/dto/UserUpdateDTO.java new file mode 100644 index 000000000..159acb517 --- /dev/null +++ b/lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/dto/UserUpdateDTO.java @@ -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; + } + +} diff --git a/lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/dto/UserUpdateStatusDTO.java b/lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/dto/UserUpdateStatusDTO.java new file mode 100644 index 000000000..1a6232e7b --- /dev/null +++ b/lab-22/lab-22-validation-01/src/main/java/cn/iocoder/springboot/lab22/validation/dto/UserUpdateStatusDTO.java @@ -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; + } + +} diff --git a/lab-22/lab-22-validation-01/src/main/resources/application.yaml b/lab-22/lab-22-validation-01/src/main/resources/application.yaml new file mode 100644 index 000000000..310231bbf --- /dev/null +++ b/lab-22/lab-22-validation-01/src/main/resources/application.yaml @@ -0,0 +1,5 @@ +spring: + # i18 message 配置,对应 MessageSourceProperties 配置类 + messages: + basename: i18n/messages # 文件路径基础名 + encoding: UTF-8 # 使用 UTF-8 编码 diff --git a/lab-22/lab-22-validation-01/src/main/resources/i18n/messages.properties b/lab-22/lab-22-validation-01/src/main/resources/i18n/messages.properties new file mode 100644 index 000000000..f47c87f2e --- /dev/null +++ b/lab-22/lab-22-validation-01/src/main/resources/i18n/messages.properties @@ -0,0 +1 @@ +UserUpdateDTO.id.NotNull=用户编号不能为空 diff --git a/lab-22/lab-22-validation-01/src/main/resources/i18n/messages_en.properties b/lab-22/lab-22-validation-01/src/main/resources/i18n/messages_en.properties new file mode 100644 index 000000000..6bcd6c283 --- /dev/null +++ b/lab-22/lab-22-validation-01/src/main/resources/i18n/messages_en.properties @@ -0,0 +1 @@ +UserUpdateDTO.id.NotNull=userId cannot be empty diff --git a/lab-22/lab-22-validation-01/src/main/resources/i18n/messages_ja.properties b/lab-22/lab-22-validation-01/src/main/resources/i18n/messages_ja.properties new file mode 100644 index 000000000..ddc6c9fa4 --- /dev/null +++ b/lab-22/lab-22-validation-01/src/main/resources/i18n/messages_ja.properties @@ -0,0 +1 @@ +UserUpdateDTO.id.NotNull=ユーザー番号は空にできません diff --git a/lab-22/lab-22-validation-01/src/test/java/cn/iocoder/springboot/lab22/validation/service/UserServiceTest.java b/lab-22/lab-22-validation-01/src/test/java/cn/iocoder/springboot/lab22/validation/service/UserServiceTest.java index 3082da9ca..da2e7cf32 100644 --- a/lab-22/lab-22-validation-01/src/test/java/cn/iocoder/springboot/lab22/validation/service/UserServiceTest.java +++ b/lab-22/lab-22-validation-01/src/test/java/cn/iocoder/springboot/lab22/validation/service/UserServiceTest.java @@ -47,6 +47,9 @@ public void testAdd02() { @Test public void testValidator() { + // 打印,查看 validator 的类型 + System.out.println(validator); + // 创建 UserAddDTO 对象 UserAddDTO addDTO = new UserAddDTO(); // 校验