jfinal 判断方法参数不能为空的注解

1:注解

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.ElementType;

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface EmptyInterface {
    String[] value();
}
2.:使用拦截器(没有使用处理器)
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.xinxing.annotation.EmptyInterface;
import com.xinxing.utils.StampToDate;

import net.coobird.thumbnailator.util.exif.IfdStructure;

/**
 * 参数不能为空注解验证
 * @author Administrator
 *
 */
public class EmptyInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation inv) {
		EmptyInterface annotation = inv.getMethod().getAnnotation(EmptyInterface.class);
		if (annotation != null) {
			noEmpty(annotation, inv);
        }
	}
	
	public void noEmpty(EmptyInterface annotation,Invocation inv) {
		Controller con = inv.getController();
        String header = con.getHeader("Content-Type"); //取出head头
        if (header != null && header.indexOf("multipart/form-data") != -1) { //判断是否是form-data
        	String folderName = "loss/" + StampToDate.getMonthDay();
        	con.getFile("photo",folderName);
        }
        String[] value = annotation.value();
        boolean isTrue = true;
        String key = "";
        for (String v : value) {
            String parameter = con.getPara(v);
            if (parameter == null || parameter.trim().length() == 0) {
            	isTrue = false;
            	key = v;
            	break;
            }
        }
        if(isTrue){
        	inv.invoke();
        }else{
        	con.setAttr("msg", key+"不能为空");
        	con.setAttr("code", "509");
        	con.renderJson();
        }
        
    }

}
3.在路由配置文件中添加addInterceptor(new EmptyInterceptor()); 一定放在最上面,第一个使用。

4,控制器方法使用注解@EmptyInterface({"areaName","longitude","latitude","distance"}) 或 @EmptyInterface(“id”),没有参数验证的请clear掉拦截器 或者 修改

if (annotation != null) {
			noEmpty(annotation, inv);
        }
让注解参数不为空再去判断 为空就往下执行
inv.invoke();



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Spring中,可以使用`@RequestParam`注解来接收前端传来的参数,同时可以对参数进行验证。对于Double类型的参数,可以使用`@NotNull`注解判断其是否为。如果参数,则会抛出`MethodArgumentNotValidException`异常,可以通过`@ExceptionHandler`注解来处理该异常。 示例代码如下: ```java @RestController public class MyController { @GetMapping("/test") public String test(@RequestParam("param") @NotNull Double param) { // 处理业务逻辑 return "success"; } @ExceptionHandler(MethodArgumentNotValidException.class) public String handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { String message = e.getBindingResult().getFieldError().getDefaultMessage(); return "参数错误:" + message; } } ``` 上面的代码中,`test`方法使用`@RequestParam`注解来接收前端传来的参数,并使用`@NotNull`注解判断参数是否为。如果参数,则会抛出`MethodArgumentNotValidException`异常。我们使用`@ExceptionHandler`注解来处理该异常,将错误信息返回给前端。 需要注意的是,在使用`@NotNull`注解时,需要添加`validation-api`和`hibernate-validator`依赖。具体依赖如下: ```xml <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.1.Final</version> </dependency> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.10.Final</version> </dependency> ``` 通过上面的代码,我们可以捕获并处理Double类型参数的异常,并将错误信息返回给前端。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

梦里藍天

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

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

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

打赏作者

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

抵扣说明:

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

余额充值