mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 spring validation 示例
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 class UserController {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
spring:
|
||||
# i18 message 配置,对应 MessageSourceProperties 配置类
|
||||
messages:
|
||||
basename: i18n/messages # 文件路径基础名
|
||||
encoding: UTF-8 # 使用 UTF-8 编码
|
||||
@@ -0,0 +1 @@
|
||||
UserUpdateDTO.id.NotNull=用户编号不能为空
|
||||
@@ -0,0 +1 @@
|
||||
UserUpdateDTO.id.NotNull=userId cannot be empty
|
||||
@@ -0,0 +1 @@
|
||||
UserUpdateDTO.id.NotNull=ユーザー番号は空にできません
|
||||
@@ -47,6 +47,9 @@ public class UserServiceTest {
|
||||
|
||||
@Test
|
||||
public void testValidator() {
|
||||
// 打印,查看 validator 的类型
|
||||
System.out.println(validator);
|
||||
|
||||
// 创建 UserAddDTO 对象
|
||||
UserAddDTO addDTO = new UserAddDTO();
|
||||
// 校验
|
||||
|
||||
Reference in New Issue
Block a user