update [重磅更新] Validator 校验框架支持国际化
This commit is contained in:
parent
89d776acb5
commit
a94a0589dd
@ -5,16 +5,23 @@ import com.ruoyi.common.utils.MessageUtils;
|
|||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import io.swagger.annotations.ApiParam;
|
import io.swagger.annotations.ApiParam;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.hibernate.validator.constraints.Range;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试国际化
|
* 测试国际化
|
||||||
*
|
*
|
||||||
* @author Lion Li
|
* @author Lion Li
|
||||||
*/
|
*/
|
||||||
|
@Validated
|
||||||
@Api(value = "测试国际化控制器", tags = {"测试国际化管理"})
|
@Api(value = "测试国际化控制器", tags = {"测试国际化管理"})
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/demo/i18n")
|
@RequestMapping("/demo/i18n")
|
||||||
@ -31,4 +38,39 @@ public class TestI18nController {
|
|||||||
public AjaxResult<Void> get(@ApiParam("国际化code") String code) {
|
public AjaxResult<Void> get(@ApiParam("国际化code") String code) {
|
||||||
return AjaxResult.success(MessageUtils.message(code));
|
return AjaxResult.success(MessageUtils.message(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validator 校验国际化
|
||||||
|
* 不传值 分别查看异常返回
|
||||||
|
*
|
||||||
|
* 测试使用 not.null
|
||||||
|
*/
|
||||||
|
@ApiOperation("Validator 校验国际化")
|
||||||
|
@GetMapping("/test1")
|
||||||
|
public AjaxResult<Void> test1(@NotBlank(message = "{not.null}") String str) {
|
||||||
|
return AjaxResult.success(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bean 校验国际化
|
||||||
|
* 不传值 分别查看异常返回
|
||||||
|
*
|
||||||
|
* 测试使用 not.null
|
||||||
|
*/
|
||||||
|
@ApiOperation("Bean 校验国际化")
|
||||||
|
@GetMapping("/test2")
|
||||||
|
public AjaxResult<TestI18nBo> test2(@Validated TestI18nBo bo) {
|
||||||
|
return AjaxResult.success(bo);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class TestI18nBo {
|
||||||
|
|
||||||
|
@NotBlank(message = "{not.null}")
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@NotNull(message = "{not.null}")
|
||||||
|
@Range(min = 0, max = 100, message = "{length.not.valid}")
|
||||||
|
private Integer age;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
package com.ruoyi.framework.config;
|
package com.ruoyi.framework.config;
|
||||||
|
|
||||||
import org.hibernate.validator.HibernateValidator;
|
import org.hibernate.validator.HibernateValidator;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.MessageSource;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||||
|
|
||||||
import javax.validation.Validation;
|
|
||||||
import javax.validation.Validator;
|
import javax.validation.Validator;
|
||||||
import javax.validation.ValidatorFactory;
|
import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验框架配置类
|
* 校验框架配置类
|
||||||
@ -16,16 +18,26 @@ import javax.validation.ValidatorFactory;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class ValidatorConfig {
|
public class ValidatorConfig {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MessageSource messageSource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置校验框架 快速返回模式
|
* 配置校验框架 快速返回模式
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public Validator validator() {
|
public Validator validator() {
|
||||||
ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
|
LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
|
||||||
.configure()
|
// 国际化
|
||||||
.failFast(true)
|
factoryBean.setValidationMessageSource(messageSource);
|
||||||
.buildValidatorFactory();
|
// 设置使用 HibernateValidator 校验器
|
||||||
return validatorFactory.getValidator();
|
factoryBean.setProviderClass(HibernateValidator.class);
|
||||||
|
Properties properties = new Properties();
|
||||||
|
// 设置 快速异常返回
|
||||||
|
properties.setProperty("hibernate.validator.fail_fast", "true");
|
||||||
|
factoryBean.setValidationProperties(properties);
|
||||||
|
// 加载配置
|
||||||
|
factoryBean.afterPropertiesSet();
|
||||||
|
return factoryBean.getValidator();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user