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中如何集成参数 ...
随机推荐
- 模式识别之bayes---bayes 简单天气预测实现实例
Bayes Classifier 分类 在模式识别的实际应用中,贝叶斯方法绝非就是post正比于prior*likelihood这个公式这么简单,一般而言我们都会用正态分布拟合likelihood来实 ...
- (转)FFmpeg源代码简单分析:avformat_find_stream_info()
目录(?)[+] ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结 ...
- ubuntu -- 安装最新版的nodejs
1.安装最新的nodejs和npm # apt-get update # apt-get install -y python-software-properties software-properti ...
- 面向切面编程(AOP)简介
在软件中,有些行为对于大多数应用都是通用的.日志,安全和事务管理几乎是所有软件都需要的.他们是否可以主动的参与呢,如果让应用程序只关注与自己所针对的业务领域问题,而其他的问题有其他应用对象来处理.是否 ...
- yii2 页面渲染方法解析
render渲染.renderPartial渲染部分.renderContent.renderAjax.renderFile ① render显示view和layout ② renderPartial ...
- FXAA
无抗锯齿 SSAA 硬件抗锯齿,OpenGL自带,4x FXAA 从图中可以看出 FXAA抗锯齿,没有硬件MSAA抗锯齿效果好
- 分布式缓存系统 Memcached 整体架构
分布式缓存系统 Memcached整体架构 Memcached经验分享[架构方向] Memcached 及 Redis 架构分析和比较
- 解决Windows2003的IE安全级别不能修改的问题
默认装完win2k3IE安全级别是不能修改的,不管怎么样修改,系统总提示说 “此域的推荐安全级别是“安全级-高”.您选择的级别较低.还是返回最高级别的.刚才弄voip,在win2k3虚拟机测试,差点气 ...
- [转] 关于QT的系统总结
出处:http://www.cnblogs.com/wangqiguo/p/4625611.html 阅读目录 编译环境与开发流程 QT项目的构成及原理 QT中的布局 QT中的通用控件 QVarian ...
- 企业IT管理说:全自动就一定是最好的吗?
现如今,企业基础设施变得越来越网络化.虚拟化并且愈加复杂,人们更愿意寻求自动化以期优化信息管理过程.毕竟,有谁不想要一个更为有效且迅速的法子来管控数据的安全.存储.应用性能监控以及IT的经济性?这不用 ...