springboot 验证 默认使用的是hibernate validator ,不用额外增加引用包,springboot已经内置包含。

设置pom相关依赖

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.itstudy</groupId>
<artifactId>demo</artifactId>
<version>1.0.0-SNAPSHOT</version> <name>demo</name> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.30</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

1)定义接收参数实体

package com.study.valid_demo.vo;

import javax.validation.constraints.AssertFalse;
import javax.validation.constraints.Pattern; import org.hibernate.validator.constraints.NotBlank; /**
* 接收客户端信息载体
*
* @author Administrator
*
*/
public class RegisterVO { @NotBlank(message = "用户名不能为空")
private String userName; @NotBlank(message = "年龄不能为空")
@Pattern(regexp = "^[0-9]{1,2}$", message = "年龄不正确")
private String age; @AssertFalse(message = "必须为false")
private Boolean isFalse;
/**
* 如果是空,则不校验,如果不为空,则校验
*/
@Pattern(regexp = "^[0-9]{4}-[0-9]{2}-[0-9]{2}$", message = "出生日期格式不正确")
private String birthday; public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public Boolean getIsFalse() {
return isFalse;
} public void setIsFalse(Boolean isFalse) {
this.isFalse = isFalse;
} public String getBirthday() {
return birthday;
} public void setBirthday(String birthday) {
this.birthday = birthday;
}
}

2.定义controller

package com.study.valid_demo;

import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import com.study.valid_demo.vo.RegisterVO; @RestController
@RequestMapping("/index")
public class IndexController { @RequestMapping("/register")
@ResponseBody
public String register(@RequestBody @Valid RegisterVO register, BindingResult result) { // 使用BindingResult 时,验证错误需要自己处理。应用程序程序正常返回
if (result.hasErrors()) {
for (ObjectError error : result.getAllErrors()) {
System.out.println(error.getDefaultMessage());
} } return register.getUserName();
} @RequestMapping("/register2")
@ResponseBody
public String register(@RequestBody @Valid RegisterVO register) { // 不使用ResultBind时,在spring验证绑定时报400 Badrequest错误,并提示错误详细信息
return register.getUserName();
}
}

3)项目启动

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class App { public static void main(String[] args) { SpringApplication app = new SpringApplication(App.class);
//关闭banner
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
} }

SpringBoot 参数检查 Controller中检查参数是否合法的更多相关文章

  1. IntelliJ IDEA中如何设置忽略@param注释中的参数与方法中的参数列表不一致的检查

  2. spring mvc controller中的参数验证机制(二)

    这里我们介绍以下自定义的校验器的简单的使用示例 一.包结构和主要文件 二.代码 1.自定义注解文件MyConstraint package com.knyel.validator; import ja ...

  3. spring mvc controller中的参数验证机制(一)

    一.验证用到的注解 @Valid 对传到后台的参数的验证 @BindingResult 配合@Valid使用,验证失败后的返回 二.示例 1.传统方式 @PostMapping public User ...

  4. springboot如何读取配置文件中的参数(例如:application-consts.properties) 又结合maven读取配置文件的顺序

    1.启动项目后,会读取pom.xml中的配置文件,例如现在读取的是本地配置 2.找到对应的配置文件  会读取uri地址下的配置.注:如果为springboot启动无需加config项目的名称,应该本身 ...

  5. SpringBoot接口 - 如何优雅的对参数进行校验?

    在以SpringBoot开发Restful接口时, 对于接口的查询参数后台也是要进行校验的,同时还需要给出校验的返回信息放到上文我们统一封装的结构中.那么如何优雅的进行参数的统一校验呢? @pdai ...

  6. Swift语言中为外部参数设置默认值可变参数常量参数变量参数输入输出参数

    Swift语言中为外部参数设置默认值可变参数常量参数变量参数输入输出参数 7.4.4  为外部参数设置默认值 开发者也可以对外部参数设置默认值.这时,调用的时候,也可以省略参数传递本文选自Swift1 ...

  7. Confluence 6 在你用户宏中使用参数

    你可以为你的用户宏指定参数.这样的话,用户可以使用参数来决定 Confluence 页面的显示情况. 如何在 Confluence 页面中使用你的宏参数 当添加一个宏到 Confluence 页面中的 ...

  8. Python 函数中,参数是传值,还是传引用?

    在 C/C++ 中,传值和传引用是函数参数传递的两种方式,在Python中参数是如何传递的?回答这个问题前,不如先来看两段代码. 代码段1: def foo(arg): arg = 2 print(a ...

  9. Flask中路由参数

    Flask中路由参数.请求方式设置 一.参数设置 1.参数类型 Flask中参数的使用 @app.route('/parames/<username>/') def hello_world ...

随机推荐

  1. 1129. Recommendation System (25)

    Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...

  2. Spring AOP注解配置demo

    https://blog.csdn.net/yhl_jxy/article/details/78815636#commentBox

  3. C\C++下获取系统进程或线程ID(转)

    在程序开发时有时需要获取线程和进程ID以分析程序运行 ()windows下获取进程或线程ID 通过调用系统提供的GetCurProcessId或GetNowThreadID来获取当前程序代码运行时的进 ...

  4. 1.使用kubeadm安装kubernetes

    一.环境准备 所有规划主机(一台master,两台node)均需操作 1.关闭防火墙,selinux [root@node1 ~]# systemctl stop firewalld [root@no ...

  5. 【机器人M号】题解

    题目 题目描述 3030年,Macsy正在火星部署一批机器人. 第1秒,他把机器人1号运到了火星,机器人1号可以制造其他的机器人. 第2秒,机器人1号造出了第一个机器人--机器人2号. 第3秒,机器人 ...

  6. 【leetcode】1131. Maximum of Absolute Value Expression

    题目如下: Given two arrays of integers with equal lengths, return the maximum value of: |arr1[i] - arr1[ ...

  7. 在mac上安装rabbitmq

    在 OS X 上安装 RabbitMQ¶ 在 Snow Leopard 上安装 RabbitMQ 最简单的方式就是 Homebrew ——OS X 上的一款新颖别致,光彩动人的包管理系统. 在本例中, ...

  8. maven打包的时候you are running on a JRE rather than a JDK?

    解决方案.删除掉,然后重新添加. 然后remove掉 然后Add  Library

  9. 30 年前的圣诞节,Python 序章被谱写

    1989 年圣诞节期间,已经从阿姆斯特丹大学(University of Amsterdam)获得数学和计算机硕士学位的 Guido van Rossum,为了打发圣诞节的无趣,决心开发一个新语言解释 ...

  10. 在 mac 系统上安装 python 的 MySQLdb 模块

    在 mac 系统上安装 python 的 MySQLdb 模块 特别说明:本文主要参考了Mac系统怎么安装MySQLdb(MySQL-Python) 第 1 步:下载 MySQL-python-1.2 ...