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中如何集成参数 ...
随机推荐
- unity-------------------打包BuildAssetBundles的使用
unity5打包机制下,一种资源打ab和资源管理的方案.1.打ab: 1.设置平台 2.清楚所有资源的assetbundlename: string[] abNameArr = AssetDataba ...
- JS 在火狐浏览器下关闭弹窗
1.首先,要确定火狐设置是否允许通过JS代码window.close()方法关闭标签. 确定方式如下: 在Firefox地址栏里输入 about:config 在配置列表中找到dom. ...
- windows上開啟多個apache服務器
1.安裝apache(這裡我用的是集成環境) 比較php版本 5.6 與 7.2 比較mysql版本 拓展: 注意:對個不同的版本的mysql,命令行進入,需要指明端口號,如:mysql -uroo ...
- java提取url里的域名
使用java标准类库java.net.URL java.net.URL url = new java.net.URL("http://blog.csdn.net/xxx.png") ...
- app已损坏,打不开。你应该将它移到废纸篓。
app已损坏,打不开.你应该将它移到废纸篓.(macOS Sierra 10.12) ,打开终端,执行 sudo spctl --master-disable 即可.
- Java写 插入 选择 冒泡 快排
/** * Created by wushuang on 2014/11/19. */ public class SortTest { @Test public void mainTest() { i ...
- ubuntu 12.04 右上角的网络连接图标突然消失不见
某天Ubuntu右上角的网络连接图标突然消失不见了,右击panel -> add to panel -> Notification Area 也不管用,最关键的是上不了网了.可以在Netw ...
- windows 下查看端口占用情况
windows下面查看端口占用情况: netstat -ano|findstr "8888" TCP 127.0.0.1:8888 0.0.0.0:0 LISTENING 6876 ...
- LINE 不被封锁的技巧
什么是封锁? 谈LINE 被封锁之前,我们先来了解一下什么是封锁.LINE 的封锁分为「好友封锁你」与「官方封锁你」二种,有些人将官方封锁讲成「停权」,其实LINE 的停权并不是你的帐号全被封锁,被封 ...
- PyCharm搭建pyqt5开发环境
PyCharm搭建PyQt5开发环境 1.安装PyQt5 2.PyCharm环境配置 2.1 添加QtDesigner 2.2 添加PyUIC 2.3 添加Pyrcc 2.4 添加assistant ...