SpringBoot(二)

一、JSR303数据校验

1、简介

JSR是Java Specification Requests的缩写,意思是Java 规范提案。JSR-303 是JAVA EE 6 中的一项子规范,叫做Bean Validation,Hibernate Validator 是 Bean Validation 的参考实现 . Hibernate Validator 提供了 JSR 303 规范中所有内置 constraint 的实现,除此之外还有一些附加constraint。

Springboot中可以用@validated来校验数据,如果数据异常则会统一抛出异常,方便异常中心统一处理。

2、规范中内置 constraint 的实现

(1)、空检查

  • @Null 验证对象是否为null

  • @NotNull 验证对象是否不为null, 无法查检长度为0的字符串

  • @NotBlank 检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格.

  • @NotEmpty 检查约束元素是否为NULL或者是EMPTY.

(2)、Booelan检查 

  • @AssertTrue 验证 Boolean 对象是否为 true 

  • @AssertFalse 验证 Boolean 对象是否为 false

(3)、长度检查

  • @Size(min=, max=) 验证对象(Array,Collection,Map,String)长度是否在给定的范围之内

  • @Length(min=, max=) Validates that the annotated string is between min and max included.

(4)、日期检查

  • @Past 验证 Date 和 Calendar 对象是否在当前时间之前,验证成立的话被注释的元素一定是一个过去的日期

  • @Future 验证 Date 和 Calendar 对象是否在当前时间之后 ,验证成立的话被注释的元素一定是一个将来的日期

  • @Pattern 验证 String 对象是否符合正则表达式的规则,被注释的元素符合制定的正则表达式,

  • regexp:正则表达式 flags: 指定 Pattern.Flag 的数组,表示正则表达式的相关选项。

(5)、数值检查

建议使用在Stirng,Integer类型,不建议使用在int类型上,因为表单值为“”时无法转换为int,但可以转换为Stirng为”“,Integer为null

  • @Min 验证 Number 和 String 对象是否大等于指定的值

  • @Max 验证 Number 和 String 对象是否小等于指定的值

  • @DecimalMax 被标注的值必须不大于约束中指定的最大值. 这个约束的参数是一个通过BigDecimal定义的最大值的字符串表示.小数存在精度

  • @DecimalMin 被标注的值必须不小于约束中指定的最小值. 这个约束的参数是一个通过BigDecimal定义的最小值的字符串表示.小数存在精度

  • @Digits 验证 Number 和 String 的构成是否合法

  • @Digits(integer=,fraction=) 验证字符串是否是符合指定格式的数字,interger指定整数精度,fraction指定小数精度。

  • @Range(min=, max=) 被指定的元素必须在合适的范围内

  • @Range(min=10000,max=50000,message=”range.bean.wage”)

  • @Valid 递归的对关联对象进行校验, 如果关联对象是个集合或者数组,那么对其中的元素进行递归校验,如果是一个map,则对其中的值部分进行校验.(是否进行递归验证)

  • @CreditCardNumber信用卡验证

  • @Email 验证是否是邮件地址,如果为null,不进行验证,算通过验证。

  • @ScriptAssert(lang= ,script=, alias=)

  • @URL(protocol=,host=, port=,regexp=, flags=)

(6)、其它的校验检查

  • @NotBlank 检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格.

  • @NotEmpty 检查约束元素是否为NULL或者是EMPTY.

  • @Length 被检查的字符串长度是否在指定的范围内

  • @CreditCardNumber信用卡验证 @Email 验证是否是邮件地址,如果为null,不进行验证,算通过验证。

  • @URL 验证是否是一个url地址

3、基本使用

添加maven依赖

 <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
 <dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
 </dependency>
  1. 在参数上加上校验注解,如果参数是自定义类型,则在类的属性上加校验注解。

  2. 使校验注解生效

    2.1 直接在参数上加校验注解,需要在类上加@Validated

    2.1 自定义类型,变量前面加@Validated或者@Valid

@Data
public class Demo {
    //不能为空且不能为空串
    @Null(message = "姓名不能为空")
    private String username;
}

@PostMapping("/demo/01")
public Result demo1(@Valid Dem demo,@NotBlank String email){
    return Result.success(200,"成功");
}

@Validated和@Valid的区别:

@Validated:

    Spring提供的

    支持分组校验

    可以用在类型、方法和方法参数上。但是不能用在成员属性(字段)上

    由于无法加在成员属性(字段)上,所以无法单独完成级联校验,需要配合@Valid

@Valid:

    JDK提供的(标准JSR-303规范)

    不支持分组校验

    可以用在方法、构造函数、方法参数和成员属性(字段)上

    可以加在成员属性(字段)上,能够独自完成级联校验

4、级联验证

@Data
public class Emp implements Serializable {

    //不能为空且不能为空串(调用trim()后)
    @NotBlank(message = "账号不能为空")
    private String username;
    
    @Valid //需要加上,否则不会验证Dept类中的校验注解
    @NotNull //并且需要触发该字段的验证才会进行嵌套验证。
    private Dept dept;
}


@Data
public class Dept implements Serializable {
    
    @NotBlank(message = "deptNameb不能为空")
    private String deptName;
}

5、分组验证

定义接口

public interface IGroup {
	
    interface Registry extends Default {}

    interface Update extends Default {}
}

指定校验的组

@Data
public class Emp implements Serializable {

    //当校验的组为update时才校验该字段
    @NotNull(message = "编号不能为空",groups = {IGroup.Update.class})
    @Min(value = 1,groups = {IGroup.Update.class})
    private Integer empNo;

    //不能为空且不能为空串(调用trim()后)
    @NotBlank(message = "账号不能为空")
    private String username;

    @Pattern(regexp = "^[0-9A-z]{10,18}$",message = "密码只能使用数字+字母",groups = IGroup.Registry.class)
    private String password;

    @Valid
    @NotNull
    private Dept dept;
}
@PostMapping("/emp/add")          //指定需要校验的组
public Result addEmp(@RequestBody @Validated(IGroup.Registry.class) Emp emp){
    return Result.success(200,"成功");
}

6、组序列

指定组与组之间的检验顺序,如果第一个组校验没过,就不会校验后面的组
 

@GroupSequence({Default.class,IGroup.Update.class, IGroup.Registry.class})
public interface IGroup {

    interface Registry extends Default {}

    interface Update extends Default {}
}
@PostMapping("/emp/add")
public Result addEmp(@RequestBody @Validated({IGroup.class}) Emp emp){
    return Result.success(200,"成功");
}

7、自定义校验注解

(1)、定义模型

public interface CaseMode{
   String UPPER="大写";
   String LOWER="小写";
}

(2)、创建自定义注解

@Target( FIELD, METHOD, PARAMETER, ANNOTATION_TYPE, TYPE_USE })
@Retention(RUNTIME)
@Constraint(validatedBy = CheckCaseValidator.class) //指定自定义验证器
@Documented
@Repeatable(CheckCase.List.class) //表示可以在同一位置重复多次
public @interface CheckCase {

    //默认的错误信息
    String message() default "{verification.default.Errormessage}";

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default { };

    String value();

    @Target({ FIELD, METHOD, PARAMETER, ANNOTATION_TYPE })
    @Retention(RUNTIME)
    @Documented
    @interface List {
        CheckCase[] value();
    }
}

(3)、创建自定义验证器第一个泛型是自定义注解、第二个是校验值的类型,也就是注解标注的字段的类型

  public class CheckCaseValidator implements ConstraintValidator<CheckCase, String> {

      private String caseMode;

      @Override
      public void initialize(CheckCase constraintAnnotation) {
          this.caseMode = constraintAnnotation.value();
      }
      /**
       * 判断是否通过校验
       * @param value   传入的值
       * @param context
       * @return
       */
      @Override
      public boolean isValid(String value, ConstraintValidatorContext context) {
          if ( value == null ) {
              return true;
          }

          if (CaseMode.UPPER.equals(caseMode) ) {
              return value.equals( value.toUpperCase() );
          }
          else {
              return value.equals( value.toLowerCase() );
          }
      }
  }

(4)、在 resources 目录下创建一个 ValidationMessages.properties 配置文件,key 是第二步 message 设置的默认值,value 是自定义错误信息。{value}为 @CheckCase的value属性的值

verification.default.Errormessage=字母必须为全为{value}

8、校验结果处理

(1)、全局异常处理

@RestControllerAdvice
public class GlobalExceptionHandler {
    
    @ExceptionHandler(BindException.class)
    public HashMap<String, String> bindExceptionHandler(BindException e){
        HashMap<String, String> map = new HashMap<>();
        e.getBindingResult().getFieldErrors().forEach(field -> {
            map.put(field.getField(), field.getDefaultMessage());
        });
        return map;
    }
    
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public HashMap<String, String> methodArgumentNotValidException(MethodArgumentNotValidException e){
        HashMap<String, String> map = new HashMap<>();
        e.getBindingResult().getFieldErrors().forEach(field -> {
            map.put(field.getField(), field.getDefaultMessage());
        });
        return map;
    }
    
    @ExceptionHandler(ConstraintViolationException.class)
    public HashMap<String, String> handle(ConstraintViolationException e) {
        HashMap<String, String> map = new HashMap<>();
        e.getConstraintViolations().forEach(item->{
            map.put(item.getPropertyPath().toString(),item.getMessage());
        });
        return map;
    }
}

(2)、BindRequest

@PostMapping("/emp/test")
public Result test(@Validated Emp emp, BindingResult validResult){
    if (validResult.hasErrors()){
        HashMap<String, String> map = new HashMap<>();
        validResult.getFieldErrors().forEach(error->{
            map.put(error.getField(),error.getDefaultMessage());
        });
        return Result.error(HttpStatus.BAD_REQUEST.value(),map);
    }
    return Result.success(HttpStatus.OK.value(),"成功");
}

(3)、Validator

@SpringBootTest
public class ValidatorTest {

    private static Validator validator = Validation.byProvider(HibernateValidator.class)
            .configure()
            .failFast(false) // 是否开启快速失败模式
            .buildValidatorFactory()
            .getValidator();

    @Test
    public void test1(){
        Emp emp = new Emp();
        //单独校验某个属性
        //Set<ConstraintViolation<Emp>> validProperty = validator.validateProperty(emp, "username");
        //检验对象
        Set<ConstraintViolation<Emp>> validBean = validator.validate(emp);
        Iterator<ConstraintViolation<Emp>> iterator = validBean.iterator();
        while (iterator.hasNext()){
            ConstraintViolation<Emp> next = iterator.next();
            String property = next.getPropertyPath().toString();
            String message = next.getMessage();
            System.out.println(property+":"+message);
        }
    }
}

二、多环境切换

1、通过properties.yaml

在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml , 用来指定多个环境版本。例如:

  • application-test.properties 代表测试环境配置

  • application-dev.properties 代表开发环境配置

如果yml和properties同时都配置了端口,并且没有激活其他环境 , 默认会使用properties配置文件的!

配置文件可以存放在:

  • 项目根目录下的config目录下

  • 项目根目录

  • classpath(resources)下的config目录下

  • classpath下

当配置文件名称相同时,优先级为:

项目目录下的config > 项目的根目录 > classpath下的config目录 > classpath

properties.yaml配置:

(1)、分别创建对应的配置文件

  • application-环境名.properties|.yml

  • application-pro.properties |.yml正式

  • appication-dev.properties|.yml开发

  • application-test.properties|.yml 测试

(2)、选择某一具体环境,在主配置中添加:

properties: 
  spring.profies.active=环境名yml方式:
  	spring:
      profiles:
        active: pro

如果将application.properties文件注释掉,sprinboot仍然会读取其他的application-环境名.properties中的配置;且properties文件的优先级高于yml文件

2、通过单个yaml文件

#第一个环境(主环境)
server:
     port: 9999
spring:
     profiles:
          active: dev #指定使用哪个环境
 
--- #使用 --- 来作为分隔符
#第二个环境(开发环境)
server:
     port: 8001
spring:
     profiles: dev
 
---
#第三个环境(测试环境)
server:
     port: 8002
spring:
     profiles: test
---

3、利用maven方式实现多环境的切换

在maven中使用profiles标签;

  <!--配置信息-->
    <profiles>
        <!--开发环境-->
        <profile>
            <!--环境标识-->
            <id>dev</id>
            <activation>
                <!--默认环境-->
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
                <server.online.port>8082</server.online.port>
                <server.eureka.port>8085</server.eureka.port>
                <server.account.port>5583</server.account.port>
                <server.zuul.port>8888</server.zuul.port>
                <eureka.host>127.0.0.1</eureka.host>
                <eureka.backup.one.port>9908</eureka.backup.one.port>
                <eureka.backup.two.port>9909</eureka.backup.two.port>
                <eureka.backup.three.port>9910</eureka.backup.three.port>
            </properties>
        </profile>
        <!--测试环境-->
        <profile>
            <id>test</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>test</spring.profiles.active>
                <server.port>8083</server.port>
            </properties>
        </profile>
        <!--生产环境-->
        <profile>
            <id>pro</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>pro</spring.profiles.active>
                <server.port>8084</server.port>
            </properties>
        </profile>
    </profiles>

分别创建对应的配置文件

  • application-环境名.properties|.yml

  • application-pro.properties |.yml正式

  • application-dev.properties|.yml开发

  • application-test.properties|.yml 测试

在主配置文件中使用@spring.profiles.active@来引入;

spring.profiles.active=@spring.profiles.active@
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DF10F-0001A

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值