SpringBoot 官方文档示例(82):JSR303校验

一、添加依赖:
spring boot 2.67

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

或者

       <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.2.3.Final</version>
        </dependency>

前者包含后者

二、定义实体类,使用JSR 303注解对属性进行限制

package cn.edu.tju.domain;


import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.*;

public class Student {
    @NotBlank(message = "名字不能为空")
    private String name;

    @Min(value = 18, message = "年龄不能小于18岁")
    @Range(min=20,max=25,message = "年龄要在20和25之间")
    private Integer age;

    @NotNull(message = "手机号不能为空")
    @Pattern(regexp = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$", message = "手机号错误")
    private String phoneNumber;

    @Email(message = "邮箱格式错误")
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

三、在controller中使用实体类的地方加上@Validated(spring的注解)或者@Valid注解(JSR303实现类提供)

package cn.edu.tju.controller;

import cn.edu.tju.domain.Student;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/test")
public class TestController {
    @PostMapping("/student")
    public String testStudent(@Validated @RequestBody Student student){
        return "ok";
    }
}

或者

package cn.edu.tju.controller;

import cn.edu.tju.domain.Student;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/test")
public class TestController {
    @PostMapping("/student")
    public String testStudent(@Valid @RequestBody Student student){
        return "ok";
    }
}

使用Postman向接口post数据,不符合要求时,控制台会有提示。

##########################################################################
其它写法:

package cn.edu.tju.service;

import javax.validation.constraints.Size;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@Service
@Validated
public class DemoService {
    public String checkInfo(@Size(min = 8, max = 10, message = "8到10") String info) {
        return "ok";
    }
}


package cn.edu.tju.controller;

import cn.edu.tju.domain.Student;
import cn.edu.tju.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private DemoService demoService;
    
    @RequestMapping("/check/{info}")
    public String checkInfo(@PathVariable("info")String info){
        return demoService.checkInfo(info);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值