最新消息:走过路过不要错过,顺带点击下侧栏广告🙏

springboot项目javax.validation使用

java Terry 1332浏览 11评论

javax.validation 是基于JSR-303标准开发出来的,使用注解方式实现,及其方便,但是这只是一个接口,没有具体实现。Hibernate-Validator是一个hibernate独立的包,可以直接引用,他实现了javax.validation同时有做了扩展,比较强大。SpringBoot在内部集成了hibernate-validation,可以直接使用。

常用注解:

代码 说明
@Null 被注解的元素必须为null
@NotNull 被注解的元素必须不为null
@AssertTrue 被注解的元素必须为true
@AssertFalse 被注解的元素必须为false
@Min(value) 被注解的元素必须为数字,其值必须大于等于最小值
@Max(value) 被注解的元素必须为数字,其值必须小于等于最小值
@Size(max,min) 被注解的元素的大小必须在指定范围内
@Past 被注解的元素必须为过去的一个时间
@Future 被注解的元素必须为未来的一个时间
@Pattern 被注解的元素必须符合指定的正则表达式

springboot实例

bean

package me.yanand.pojo;

import javax.validation.constraints.Max;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

public class User {
    @NotBlank(message = "姓名不能为空")
    private String name;
    @NotNull(message = "年龄不能为空")
    @Max(value = 100,message = "年龄太大")
    private Integer age;

    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;
    }
}

controller

package me.yanand.controller;

import me.yanand.pojo.User;
import org.springframework.validation.BindingResult;
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
public class TestController {
    @RequestMapping("/")
    String hello(@RequestBody @Valid User user, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return bindingResult.getFieldError().getDefaultMessage();
        }
        return "ok";
    }
}

postman请求

postman

@Valid 和 BindingResult 是一一对应的,如果有多个@Valid,那么每个@Valid后面跟着的BindingResult就是这个@Valid的验证结果,顺序不能乱

转载请注明:Terry's blog » springboot项目javax.validation使用

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (11)

  1. Always a major fan of linking to bloggers that I like but really don't get lots of link enjoy from.
  2. Every after inside a though we pick out blogs that we read. Listed beneath are the most current internet sites that we choose.
  3. Wonderful story, reckoned we could combine a number of unrelated information, nevertheless truly really worth taking a appear, whoa did one particular learn about Mid East has got extra problerms too.
  4. The time to study or take a look at the material or web sites we have linked to beneath.
  5. Check beneath, are some absolutely unrelated web-sites to ours, nonetheless, they are most trustworthy sources that we use.
  6. Below youll obtain the link to some websites that we assume you ought to visit.
    Google2周前 (05-16)回复
  7. Here is a great Blog You may Uncover Interesting that we encourage you to visit.
    Google2周前 (05-15)回复
  8. Every when inside a whilst we pick out blogs that we study. Listed beneath would be the latest internet sites that we choose.
    Google2周前 (05-15)回复
  9. Although web-sites we backlink to below are considerably not connected to ours, we really feel they may be truly really worth a go via, so have a look.
    Google2周前 (05-14)回复
  10. Check below, are some absolutely unrelated web-sites to ours, however, they're most trustworthy sources that we use.
    Google2周前 (05-14)回复
  11. Every after in a although we select blogs that we study. Listed below would be the newest web-sites that we choose.
    Google2周前 (05-13)回复