zoukankan      html  css  js  c++  java
  • 方法入参检测工具类 spring自带org.springframework.util.Assert 通用类

    断言方法

    1. notNull(Object object)   非空判断
    当 object 不为 null 时抛出异常,notNull(Object object, String message) 方法允许您通过 message 定制异常信息。和 notNull() 方法断言规则相反的方法是 isNull(Object object)/isNull(Object object, String message),它要求入参一定是 null; 

    2. isTrue(boolean expression) / isTrue(boolean expression, String message)  
    当 expression 不为 true 抛出异常; 

    3. notEmpty(Collection collection) / notEmpty(Collection collection, String message)  
    当集合未包含元素时抛出异常。 
    notEmpty(Map map) / notEmpty(Map map, String message) 和 notEmpty(Object[] array, String message) / notEmpty(Object[] array, String message) 分别对 Map 和 Object[] 类型的入参进行判断; 

    4. hasLength(String text) / hasLength(String text, String message)  当 text 为 null 或长度为 0 时抛出异常; 

    5. hasText(String text) / hasText(String text, String message)  text 不能为 null 且必须至少包含一个非空格的字符,否则抛出异常; 

    6. isInstanceOf(Class clazz, Object obj) / isInstanceOf(Class type, Object obj, String message)  如果 obj 不能被正确造型为 clazz 指定的类将抛出异常; 
    7. isAssignable(Class superType, Class subType) / isAssignable(Class superType, Class subType, String message)  subType 必须可以按类型匹配于 superType,否则将抛出异常; 

    使用 Assert 断言类可以简化方法入参检测的代码,如 InputStream getData(String file) 在应用 Assert 断言类后,其代码可以简化为以下的形式: 

    public InputStream getData(String file){ 
        Assert.hasText(file,"file入参不是有效的文件地址"); 
        ① 使用 Spring 断言类进行方法入参检测 
    … 

  • 相关阅读:
    ng-深度学习-课程笔记-1: 介绍深度学习(Week1)
    java发送http请求和多线程
    Spring Cloud Eureka注册中心(快速搭建)
    Spring boot集成Swagger2,并配置多个扫描路径,添加swagger-ui-layer
    springboot在idea的RunDashboard如何显示出来
    Oracle 中select XX_id_seq.nextval from dual 什么意思呢?
    mysql类似to_char()to_date()函数mysql日期和字符相互转换方法date_f
    MySQL的Limit详解
    HikariCP 个人实例
    NBA-2018骑士季后赛
  • 原文地址:https://www.cnblogs.com/ysg520/p/10998948.html
Copyright © 2011-2022 走看看