Java 反射如何实现接口参数校验

文摘   2023-05-29 17:40   江苏  

0. 

JavareflectionJava  


1.

于其它TMD就是


2.

@Valid@Validated


3.

mock


3.1 

/** * @author siri */@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface ParamValid {}

3.2

@Aspect@Component@Order(5)public class ParamValidAspect {
private static final Logger logger = LoggerFactory.getLogger(ParamValidAspect.class);
@Before(<span data-raw-text="" "="" data-textnode-index-1685352259513="47" data-index-1685352259513="752" data-textnode-notemoji-index-1685352259513="752" class="character">"@annotation(com.lanshan.pay.annotation.ParamValid)<span data-raw-text="" "="" data-textnode-index-1685352259513="47" data-index-1685352259513="803" data-textnode-notemoji-index-1685352259513="803" class="character">") public void ValidParam(JoinPoint joinPoint) throws IllegalAccessException { String method = joinPoint.getSignature().getDeclaringTypeName() + <span data-raw-text="" "="" data-textnode-index-1685352259513="53" data-index-1685352259513="958" data-textnode-notemoji-index-1685352259513="958" class="character">".<span data-raw-text="" "="" data-textnode-index-1685352259513="53" data-index-1685352259513="960" data-textnode-notemoji-index-1685352259513="960" class="character">" + joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); if(null != args && args.length>0){ for (Object arg : args) { Field[] fields = arg.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(Boolean.TRUE); final Object val = field.get(arg); // // final NotBlank notBlank = field.getAnnotation(NotBlank.class); if(null != notBlank && Objects.isNull(val)){ String msg = field.getName()+<span data-raw-text="" "="" data-textnode-index-1685352259513="97" data-index-1685352259513="1610" data-textnode-notemoji-index-1685352259513="1610" class="character">":<span data-raw-text="" "="" data-textnode-index-1685352259513="97" data-index-1685352259513="1612" data-textnode-notemoji-index-1685352259513="1612" class="character">"+notBlank.message(); logger.error(<span data-raw-text="" "="" data-textnode-index-1685352259513="100" data-index-1685352259513="1670" data-textnode-notemoji-index-1685352259513="1670" class="character">"{}:{}<span data-raw-text="" "="" data-textnode-index-1685352259513="100" data-index-1685352259513="1690" data-textnode-notemoji-index-1685352259513="1690" class="character">",method,msg); throw new BizException(HttpStatus.BAD_REQUEST.value(), msg); } // final NotNull notNull = field.getAnnotation(NotNull.class); if(null != notNull && Objects.isNull(val)){ String msg = field.getName()+<span data-raw-text="" "="" data-textnode-index-1685352259513="121" data-index-1685352259513="2035" data-textnode-notemoji-index-1685352259513="2035" class="character">":<span data-raw-text="" "="" data-textnode-index-1685352259513="121" data-index-1685352259513="2037" data-textnode-notemoji-index-1685352259513="2037" class="character">"+notNull.message(); logger.error(<span data-raw-text="" "="" data-textnode-index-1685352259513="124" data-index-1685352259513="2094" data-textnode-notemoji-index-1685352259513="2094" class="character">"{}:{}<span data-raw-text="" "="" data-textnode-index-1685352259513="124" data-index-1685352259513="2114" data-textnode-notemoji-index-1685352259513="2114" class="character">",method,msg); throw new BizException(HttpStatus.BAD_REQUEST.value(), msg); } // final NotEmpty notEmpty = field.getAnnotation(NotEmpty.class); if(null != notEmpty && Objects.isNull(val)){ String msg = field.getName()+<span data-raw-text="" "="" data-textnode-index-1685352259513="145" data-index-1685352259513="2461" data-textnode-notemoji-index-1685352259513="2461" class="character">":<span data-raw-text="" "="" data-textnode-index-1685352259513="145" data-index-1685352259513="2463" data-textnode-notemoji-index-1685352259513="2463" class="character">"+notEmpty.message(); logger.error(<span data-raw-text="" "="" data-textnode-index-1685352259513="148" data-index-1685352259513="2521" data-textnode-notemoji-index-1685352259513="2521" class="character">"{}:{}<span data-raw-text="" "="" data-textnode-index-1685352259513="148" data-index-1685352259513="2541" data-textnode-notemoji-index-1685352259513="2541" class="character">",method,msg); throw new BizException(HttpStatus.BAD_REQUEST.value(), msg); } } } }
}}

3.3 @ParamValidBindingResult @Validated  @Valid

@ParamValid@PostMapping(<span data-raw-text="" "="" data-textnode-index-1685352259513="162" data-index-1685352259513="2801" data-textnode-notemoji-index-1685352259513="2801" class="character">"/save<span data-raw-text="" "="" data-textnode-index-1685352259513="162" data-index-1685352259513="2807" data-textnode-notemoji-index-1685352259513="2807" class="character">")public Result<OrderSaveVo> save(@Validated OrderSaveDto orderSaveDto, BindingResult bindingResult) {    OrderSaveVo orderSaveVo = orderService.saveOrder(orderSaveDto);    return Result.success(orderSaveVo);}

3.4

public class OrderSaveDto implements Serializable {

/** * id */ @NotNull(message = <span data-raw-text="" "="" data-textnode-index-1685352259513="187" data-index-1685352259513="3127" data-textnode-notemoji-index-1685352259513="3127" class="character">"id<span data-raw-text="" "="" data-textnode-index-1685352259513="187" data-index-1685352259513="3136" data-textnode-notemoji-index-1685352259513="3136" class="character">") private Long itemId;
/** * */ @NotNull(message = <span data-raw-text="" "="" data-textnode-index-1685352259513="199" data-index-1685352259513="3211" data-textnode-notemoji-index-1685352259513="3211" class="character">"<span data-raw-text="" "="" data-textnode-index-1685352259513="199" data-index-1685352259513="3221" data-textnode-notemoji-index-1685352259513="3221" class="character">") private Integer itemTotalCount;
/** * */ private String notifyUrl;}

3.5  


3.2


P.SDemo


API

java.lang.Class

java.lang.reflect.Method

java.lang.reflect.Field

java.lang.reflect.Constructor


如有任何问题,欢迎指正

晚霞程序员
一位需要不断学习的30+程序员……