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

springboot项目javax.validation使用

java Terry 1822浏览 157评论

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,您需要填写昵称和邮箱!

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

网友最新评论 (157)

  1. The time to read or visit the subject material or web pages we have linked to below.
    Google18分钟前回复
  2. Usually posts some very interesting stuff like this. If you are new to this site.
    Google2小时前回复
  3. Very few websites that transpire to be comprehensive below, from our point of view are undoubtedly nicely really worth checking out.
    Google4小时前回复
  4. Always a large fan of linking to bloggers that I adore but do not get quite a bit of link appreciate from.
    Google14小时前回复
  5. One of our guests not long ago proposed the following website.
    Google14小时前回复
  6. Sites of interest we have a link to.
    Google18小时前回复
  7. Usually posts some really interesting stuff like this. If you are new to this site.
    Google19小时前回复
  8. Usually posts some very interesting stuff like this. If you are new to this site.
    Google21小时前回复
  9. We came across a cool web page that you just may possibly appreciate. Take a look should you want.
    Google21小时前回复
  10. Below you will obtain the link to some sites that we believe you ought to visit.
  11. Very few sites that occur to be comprehensive beneath, from our point of view are undoubtedly properly worth checking out.
  12. Please take a look at the websites we comply with, like this one particular, because it represents our picks from the web.
  13. Here are a few of the web sites we advise for our visitors.
  14. Although websites we backlink to below are considerably not associated to ours, we really feel they're in fact really worth a go as a result of, so have a look.
  15. Below you will come across the link to some sites that we consider you ought to visit.
  16. Very couple of web sites that transpire to become detailed below, from our point of view are undoubtedly very well worth checking out.
  17. Just beneath, are many totally not connected websites to ours, however, they are certainly worth going over.