java 入参校验_Java优雅的校验入参工具类

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.util.StringUtils;

import java.lang.reflect.Field;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

/**

* @Author: SunK

* @Date: 2019/9/18 17:23

* @Description:

* @Version 1.0

*/

public class CheckObjectUtils {

private static final Logger logger = LoggerFactory.getLogger(CheckObjectUtils.class);

/**

* 校验对象属性均不能为空!

*

* @param obj

* @return

* @author Sunk

*/

public static BaseResult checkObject(Object obj) {

BaseResult result = new BaseResult();

if (obj == null){

result.setErrorCode("1");

result.setMessage("参数不能为空!");

return result;

}

for (Field field : obj.getClass().getDeclaredFields()) {

field.setAccessible(true);

try {

if (StringUtils.isEmpty(field.get(obj))) {

logger.error("==> " + field.getName() + "不能为空!");

result.setErrorCode("1");

result.setMessage(field.getName() + "不能为空!");

return result;

}

} catch (IllegalAccessException e) {

result.setErrorCode("1");

result.setMessage(field.getName() + "不能为空!");

return result;

}

}

return result;

}

/**

* 指定字段名字,校验对象属性不能为空!

*

* @param obj

* @return

* @author Sunk

*/

public static BaseResult checkObjectByFileldName(Object obj, String... checkColumnName) {

BaseResult result = new BaseResult();

if (obj == null){

result.setErrorCode("1");

result.setMessage("参数不能为空!");

return result;

}

MapcheckColumnMap = new HashMap<>();

for (String filter : checkColumnName) {

checkColumnMap.put(filter,1);

}

for (Field field : obj.getClass().getDeclaredFields()) {

field.setAccessible(true);

String fieldName = field.getName();

if (!checkColumnMap.containsKey(fieldName)){

continue;

}

try {

if (StringUtils.isEmpty(field.get(obj))) {

logger.info("==> " + field.getName() + "不能为空!");

result.setErrorCode("1");

result.setMessage(field.getName() + "不能为空!");

return result;

}

} catch (IllegalAccessException e) {

result.setErrorCode("1");

result.setMessage(field.getName() + "不能为空!");

return result;

}

}

return result;

}

/**

* 过滤掉字段后,校验剩余对象属性不能为空!

*

* @param obj

* @return

* @author Sunk

*/

public static BaseResult checkObjectWithFilter(Object obj, String... filterColumnName) {

BaseResult result = new BaseResult();

if (obj == null){

result.setErrorCode("1");

result.setMessage("参数不能为空!");

return result;

}

MapfilterColumnMap = new HashMap<>();

for (String filter : filterColumnName) {

filterColumnMap.put(filter,1);

}

for (Field field : obj.getClass().getDeclaredFields()) {

field.setAccessible(true);

String fieldName = field.getName();

if (filterColumnMap.containsKey(fieldName)){

continue;

}

try {

if (StringUtils.isEmpty(field.get(obj))) {

logger.info("==> " + field.getName() + "不能为空!");

result.setErrorCode("1");

result.setMessage(field.getName() + "不能为空!");

return result;

}

} catch (IllegalAccessException e) {

result.setErrorCode("1");

result.setMessage(field.getName() + "不能为空!");

return result;

}

}

return result;

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值