Skip to content

Commit

Permalink
增加 spring validation 示例
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Nov 18, 2019
1 parent ecc7a43 commit 08fb49e
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 0 deletions.
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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

}
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;
}

}
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;
}

}
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 编码
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UserUpdateDTO.id.NotNull=用户编号不能为空
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UserUpdateDTO.id.NotNull=userId cannot be empty
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UserUpdateDTO.id.NotNull=ユーザー番号は空にできません
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public void testAdd02() {

@Test
public void testValidator() {
// 打印,查看 validator 的类型
System.out.println(validator);

// 创建 UserAddDTO 对象
UserAddDTO addDTO = new UserAddDTO();
// 校验
Expand Down

0 comments on commit 08fb49e

Please sign in to comment.