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中如何集成参数 ...
随机推荐
- QMusic
https://github.com/dragondjf/QMusic QMusic是基于python + PyQT5 + QML的一款本地音乐播放器,旨在提升Linux下更好的音乐播放 ...
- zabbix_agentd客户端安装与配置(Linux操作系统)
标注:官网下载zabbix安装包(zabbix安装包里包含了zabbix_agentd客户端安装包,我们只选择zabbix_agentd客户端安装) zbbix官网下载地址: http://www. ...
- image 和 barplot 的组合
代码示例: par(mfrow = c(1, 2)) par(mar = c(1, 10, 1, 0), las = 1, ann = F) image(1:10, 1:10, matrix(samp ...
- [lua, mysql] 将多条记录数据组合成一条sql插入语句(for mysql)
-- 演示将多条记录数据组合成一条sql插入语句(for mysql) function getTpl0(tname) -- 获取表各个字段 local t = { tpl_pack = {" ...
- iOS: 自动增高的 textView
如 iPhone 内应用“信息”的输入框一样,输入文字过多或者输入换行,输入框可以随着内容自动变化.主要是计算内容的尺寸并相应更改输入框的frame.具体表现在: 更改输入框的 frame.origi ...
- Android 编程下的代码混淆之(android-support-v4.jar)
项 目在代码混淆过程中如果引用了第三方 Jar 包,需要在混淆的脚本文件中加入第三方 Jar 包的声明.部分第三方 Jar 包虽然在混淆脚本中进行了声明,但是在混淆过程中经常会发现内部类或者引用文件找 ...
- UGUI之控件以及按钮的监听事件系统
using UnityEngine; using System.Collections; using UnityEngine.EventSystems; public class EventTrigg ...
- VS常用快捷鍵
折疊所有方法 Ctrl +M +M 折疊單個方法 Ctrl +M +O 折疊單個方法
- 用Eclipse编写Android程序的代码提示功能
用Eclipse编写Android程序的代码提示功能主要是在java和xml文件中,有时候会失效,默认的提示功能有限. 1)java文件自动提示 Window->Preferences- ...
- 笔记--Wcf全面解析(上)---(1)
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using ...