java checkfornull_自定义@CheckNull 请求参数非空检验

项目请求时,在controller里做非空判断繁琐低效。本文介绍通过自定义注解@CheckNull解决该问题,包括注解定义、解析,自定义异常类ParamsException,还给出使用示例,可一次性对多个参数进行非空判断。
部署运行你感兴趣的模型镜像

项目请求时,在controller里面做一大堆非空判断,真是繁琐低效.加个注解一次性解决问题

1. 自定义注解@CheckNull

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

public @interface CheckNull {

String[] value() default {};

}

2. 对@CheckNull进行解析

package com.lyf.aop;

import com.lyf.annotation.CheckNull;

import com.lyf.base.Constant;

import com.lyf.exception.ParamsException;

import io.micrometer.core.instrument.util.StringUtils;

import lombok.extern.slf4j.Slf4j;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.reflect.MethodSignature;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.core.annotation.Order;

import org.springframework.stereotype.Component;

import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;

import java.util.Map;

@Aspect

@Component

@Slf4j

public class CheckNullAspect {

@Autowired

HttpServletRequest request;

@Before("@annotation(com.lyf.annotation.CheckNull) && @annotation(checkNull)")

public void check(JoinPoint jp, CheckNull checkNull) throws Throwable {

String[] checkNames = checkNull.value();

Map paramMap = getParamMap(jp);

for (int i = 0; i < checkNames.length; i++) {

String checkName = checkNames[i];

if(StringUtils.isBlank(paramMap.get(checkName))

&& StringUtils.isBlank(request.getParameter(checkName))){// 兼容通过对象获取参数形式

throw new ParamsException(Constant.ERROR_CODE, checkName+"不能为空");

}

}

}

/**

* 获取请求参数列表

* @return

*/

private Map getParamMap(JoinPoint jp) {

Object[] argArr = jp.getArgs();

MethodSignature signature = (MethodSignature) jp.getSignature();

String[] paramNameArr = signature.getParameterNames();

Map paramMap = new HashMap<>();

for (int i = 0; i < paramNameArr.length; i++) {

paramMap.put(paramNameArr[i], ObjectUtils.nullSafeToString(argArr[i]));

}

return paramMap;

}

}

3. 自定义异常类ParamsException

package com.lyf.exception;

public class ParamsException extends RuntimeException {

private Integer code= 300;

private String msg= "操作失败!";

public ParamsException( Integer code, String msg) {

super(msg);

this.code = code;

this.msg = msg;

}

public ParamsException(Integer code) {

super("操作失败");

this.code = code;

}

public ParamsException( String msg) {

super(msg);

this.msg = msg;

}

public Integer getCode() {

return code;

}

public void setCode(Integer code) {

this.code = code;

}

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

}

4. 使用示例

@RequestMapping("update")

@CheckNull({"id","name"}) // 当然支持多个参数

public ResultInfo update (User user){

userService.update(user);

return Result.success();

}

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值