您的位置:首页技术文章
文章详情页

SpringBoot参数校验与国际化使用教程

【字号: 日期:2023-04-06 15:15:21浏览:3作者:猪猪
一、参数校验

springboot 使用校验框架validation校验方法的入参

SpringBoot的Web组件内部集成了hibernate-validator,所以我们这里并不需要额外的为验证再导入其他的包。

1、bean 中添加标签

标签需要加在属性上,@NotEmpty标签String的参数不能为空

@Datapublic class DemoDto { @NotEmpty(message = '名称不能为空') private String name; @Length(min = 5, max = 25, message = 'key的长度为5-25') private String key; @Pattern(regexp = '[012]', message = '无效的状态标志') private String state;}2、Controller中开启验证

在Controller 中 请求参数上添加@Validated 标签开启验证

@RequestMapping('test') public String test(@Valid @RequestBody DemoDto dto){ System.out.println('test....................'); return 'test.........................'; }

测试返回结果

{ 'timestamp': '2020-01-14 13:30:03', 'status': 400, 'error': 'Bad Request', 'errors': [{ 'codes': ['Length.demoDto.key','Length.key','Length.java.lang.String','Length' ], 'arguments': [{ 'codes': ['demoDto.key','key' ], 'arguments': null, 'defaultMessage': 'key', 'code': 'key'},25,5 ], 'defaultMessage': 'key的长度为5-25', 'objectName': 'demoDto', 'field': 'key', 'rejectedValue': '11', 'bindingFailure': false, 'code': 'Length'},{...},{...} ], 'message': 'Validation failed for object=’demoDto’. Error count: 3', 'path': '/test'}

返回的错误信息比较乱,需要统一整理,这个时候可以使用全局异常处理的方法

3、异常处理,捕获错误信息

当验证不通过时会抛异常出来。在异常处理器中捕获异常信息(因为验证不通过的项可能是多个所以统一捕获处理),并抛给前端。(此处是前后端分离开发)

@RequestMapping('test') public ResultBean test(@Valid @RequestBody DemoDto dto){ System.out.println('test....................'); return new ResultBean('test.........................'); }

这里统一返回一个自定义的ResultBean类型

@Slf4j@RestControllerAdvicepublic class GlobalExceptionHandler { @ExceptionHandler(value = MethodArgumentNotValidException.class) public ResultBean methodArgumentNotValid(HttpServletRequest req, MethodArgumentNotValidException ex) { ResultBean result = ResultBean.FAIL; List<ObjectError> errors =ex.getBindingResult().getAllErrors(); StringBuffer errorMsg=new StringBuffer(); errors.stream().forEach(x -> errorMsg.append(x.getDefaultMessage()).append(';')); log.error('---MethodArgumentNotValidException Handler--- ERROR: {}', errorMsg.toString()); result.setMsg(errorMsg.toString()); return result; }}

此时的返回结果为:

{ 'code': 500, 'msg': '无效的状态标志;key的长度为5-25;名称不能为空;', 'content': null}

二、分组校验

有时候需要在不同的方法中对同一个bean中的参数进行校验

1、在dto中添加groups

@Datapublic class DemoDto { public interface Default { } public interface Update { } @NotEmpty(message = '名称不能为空') private String name; @Length(min = 5, max = 25, message = 'key的长度为5-25' ,groups = Default.class ) private String key; @Pattern(regexp = '[012]', message = '无效的状态标志',groups = {Default.class,Update.class} ) private String state;}

2、在controller中需要用到@Validated来校验

@RequestMapping('test2') public String test2(@Validated(value = DemoDto.Default.class) @RequestBody DemoDto dto){ System.out.println('test....................'); return 'test.........................'; } @RequestMapping('test4') public String test4(@Validated(value = {DemoDto.Default.class,DemoDto.Update.class}) @RequestBody DemoDto dto){ System.out.println('test....................'); return 'test.........................'; }三、国际化返回配置文件的信息1. 在Resource下添加properties文件

SpringBoot参数校验与国际化使用教程

文件中添加需要打印的消息,如:

demo.key.null=demo的key不能为空start.ge.end = 开始日期{0}必须小于结束日期{1}!demo.key.length=demo的key长度不正确2. 在application.yml中添加配置

spring: messages: encoding: UTF-8 basename: message/messages_zh3. 使用方法

在类中直接注入,即可使用

@Autowired private MessageSource messageSource; @RequestMapping('getMessageByKey') public ResultBean getMessageByKey(@Valid @RequestBody DemoDto dto){ String key = dto.getKey(); String [] param = {'2019-8-8', '2019-9-9'}; return new ResultBean(messageSource.getMessage(key, param, Locale.CHINA)); }

测试调用和返回结果,返回的数据和预期相符合

SpringBoot参数校验与国际化使用教程

三、国际化参数校验

根据上面的修改

1、bean 中添加标签

标签需要加在属性上,@NotEmpty标签String的参数不能为空

@Datapublic class DemoDto { @NotEmpty(message = '{demo.key.null}') @Length(min = 5, max = 25, message = '{demo.key.length}') private String key;}

2、添加上ValidationMessages文件

国际化配置文件必须放在classpath的根目录下,即src/java/resources的根目录下。

国际化配置文件必须以ValidationMessages开头,比如ValidationMessages.properties 或者 ValidationMessages_en.properties。

在/resources的根目录下添加上ValidationMessages.properties文件

demo.key.null=demo的key不能为空,这里是validationMessagedemo.key.length=demo的key长度不正确

3、返回结果

{ 'code': 500, 'msg': 'demo的key不能为空,这里是validationMessage;', 'content': null}

自定义properties文件

SpringBoot 国际化验证 @Validated 的 message 国际化资源文件默认必须放在 resources/ValidationMessages.properties 中。

现在我想把资源文件放到 resources/message/messages_zh.properties 中

若要自定义文件位置或名称则需要重写WebMvcConfigurerAdapter 的 getValidator 方法,但WebMvcConfigurerAdapter在springboot2中已经废弃了,可以改为使用WebMvcConfigurationSupport

在一的基础上修改:

@Configurationpublic class ValidatorConfiguration extends WebMvcConfigurationSupport { @Autowired private MessageSource messageSource; @Override public Validator getValidator() { return validator(); } @Bean public Validator validator() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.setValidationMessageSource(messageSource); return validator; }}

最后得到结果为:

{ 'code': 500, 'msg': 'demo的key不能为空ID:{0};', 'content': null}

参考文章:

spring boot国际化——MessageSource的使用

总结

到此这篇关于SpringBoot参数校验与国际化使用教程的文章就介绍到这了,更多相关SpringBoot参数校验与国际化使用内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!

标签: Spring
相关文章: