springboot---->springboot中的校验器(一)
这里面我们简单的学习一下springboot中关于数据格式化的使用。冬天花败,春暖花开,有人离去,有人归来。
springboot中的校验器
我们的测试环境是springboot,对请求的person数据先做转换再校验。
一、我们定义的实体Bean类Person
package com.linux.huhx.learn.converter; import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable; /**
* @Author: huhx
* @Date: 2017-12-15 下午 3:30
* @Desc: 实体类
*/
public class Person implements Serializable {
@NotNull(message = "用户名不能为空")
@Size(min = 3, message = "用户名的长度小于3位")
private String username;
private String password; @Min(value = 18, message = "用户的年龄不能小于18")
private int age; public Person(String username, String password, int age) {
this.username = username;
this.password = password;
this.age = age;
} // ..get set
}
二、我们的检验控制器类
package com.linux.huhx.learn.converter; import com.linux.huhx.exception.MaxRunTimeException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid; /**
* @Author: huhx
* @Date: 2017-12-15 下午 3:46
* @Desc: 测试springboot中自定义类型转换器
*/ @RestController
@RequestMapping("/converter")
public class PersonConverterAction { @PostMapping("/person")
public Person convertrStringToPerson(@Valid Person person, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
for (ObjectError error : bindingResult.getAllErrors()) {
throw new MaxRunTimeException(error.getDefaultMessage());
}
}
return person;
}
}
通过postman发送post的请求:http://localhost:9998/converter/person。

返回的数据:
{
"timestamp": ,
"status": ,
"error": "Internal Server Error",
"exception": "com.linux.huhx.exception.MaxRunTimeException",
"message": "用户的年龄不能小于18",
"path": "/converter/person"
}
友情链接
springboot---->springboot中的校验器(一)的更多相关文章
- springboot(五).如何在springboot项目中使用拦截器
在每个项目中,拦截器都是我们经常会去使用的东西,基本上任一一个项目都缺不了拦截器的使用. 如日志记录.登录验证,session验证等,都需要拦截器来拦截URL请求,那springboot中的拦截器是如 ...
- 如何在SpringBoot项目中使用拦截器
相比springmvc,springboot中拦截器不需要在xml中配置,只需定义拦截器类 implements HandlerInterceptor 和拦截器拦截路径的配置类extends WebM ...
- 在SpringBoot框架中使用拦截器
1.继承WebMvcConfigureAdapter类,覆盖其addInterceptors接口,注册我们自定义的拦截器 package com.eth.wallet.config; import c ...
- 【spring】-- jsr303参数校验器
一.为什么要进行参数校验? 当我们在服务端控制器接受前台数据时,肯定首先要对数据进行参数验证,判断参数是否为空?是否为电话号码?是否为邮箱格式?等等. 这里有个问题要注意: 前端代码一般上会对这些数据 ...
- Springboot中SpringMvc拦截器配置与应用(实战)
一.什么是拦截器,及其作用 拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在方法执行之前或之后加入某些操作,其实就是AOP的一种实现策略.它通过动态拦截Action调用的对 ...
- springboot项目中接口入参的简单校验
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- 在springboot中使用拦截器
在springMVC中可以实现拦截器,是通过实现HandlerInterceptor接口,然后在springmvc-web.xml中配置就可以使用拦截器了.在springboot中拦截器也是一样的思想 ...
- springmvc以及springboot中的拦截器配置
拦截器两种实现 如果不同的controller中都需要拦截器,不能使用相同的拦截器,因为拦截器不能跨controller,这个时候只能为不同的controller配置不同的拦截器,每一个拦截器只能 ...
- SpringBoot 如何进行参数校验,老鸟们都这么玩的!
大家好,我是飘渺. 前几天写了一篇 SpringBoot如何统一后端返回格式?老鸟们都是这样玩的! 阅读效果还不错,而且被很多号主都转载过,今天我们继续第二篇,来聊聊在SprinBoot中如何集成参数 ...
随机推荐
- The required Server component failed to start so Tomcat is unable to start问题解决
问题出现: Server Tomcat v8.5 Server at localhost failed to start. 或者The required Server component faile ...
- C# IEnumerator的详解
迭代器模式是设计模式中行为模式(behavioral pattern)的一个例子,他是一种简化对象间通讯的模式,也是一种非常容易理解和使用的模式.简单来说,迭代器模式使得你能够获取到序列中的所有元素而 ...
- Linux系统教程:设置GRUB菜单密码
1.认识启动配置选项 [root@server5 ~]# cat /boot/grub/grub.conf # grub.conf generated by anaconda # # Note ...
- 运行 Shell 脚本有两种方法:
1.作为可执行程序 将上面的代码保存为 test.sh,并 cd 到相应目录: chmod +x ./test.sh #使脚本具有执行权限 ./test.sh #执行脚本 注意,一定要写成 ./tes ...
- git基于历史commit创建分支
基于以前的commit创建一个分支 步骤: 1.确定需要取出版本的commit值 git log 2.基于该commit创建分支 git branch <branch name> < ...
- mysql 5.1超过默认8小时空闲时间解决办法(错误:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure)
报错: MySQL第二天早上第一次连接超时报错, com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications lin ...
- Springboot学习笔记(七)-集成Redis
redis下载地址 添加依赖 <dependencies> <dependency> <groupId>org.springframework.boot</g ...
- 移动端H5地图离线瓦片方案(1)(2)
2在作者另一篇 移动端H5地图离线瓦片方案 文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 移动端的网速和 ...
- IE中自定义标签使用自封闭格式引发错误!
最近学习IONIC,其中用到了ion-menu-nav-button,由于标签开始和结尾之间没有内容,所以图省事儿使用自封闭标签的写法: <ion-menu-nav-button class=& ...
- iview框架select默认选择一个option的值
给select加v-model,绑定的值为默认要显示的option的value值,展示的则为option的标签之间的内容,并且如果option的value是双引号,这里绑定的值也要双引号,否则不能正常 ...