栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

关于使用AOP切面处理框架对于IntegerBigdecimal可能返回-1的实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于使用AOP切面处理框架对于IntegerBigdecimal可能返回-1的实例

直接上代码 AOP切面

@Component
@Aspect
public class DefaultConvertAOP {

private List exclude = new ArrayList<>(Arrays.asList("void"));

@Pointcut("execution(* org.muyuan.slaughter.workshop.convertApi.service..*(..))")
public void convert(){

}

@Around("convert()")
public Object convert(ProceedingJoinPoint point) throws Throwable {
    MethodSignature methodSignature = (MethodSignature) point.getSignature();
    Method method = methodSignature.getMethod();
    Class returnType = method.getReturnType();
    Object result = point.proceed();
    String returnTypeName = returnType.getName();
    if(exclude.contains(returnTypeName)) return result;

    if(returnTypeName.contains("org.muyuan.slaughter.workshop.convertApi.vo")){
        Class clz = result.getClass();
        Field[] fields = clz.getDeclaredFields();
        if (fields.length > 0) {
            for (Field field : fields) {
                field.setAccessible(true);
                Object o = field.get(result);
                String name = field.getType().getName();
                if(null == o && name.equals("java.lang.Integer")){
                    field.set(result, 0);
                }else if(null == o && name.equals("java.math.BigDecimal")){
                    field.set(result, BigDecimal.ZERO);
                }else if(null == o && name.equals("java.util.List")){
                    field.set(result, new ArrayList<>());
                }
                if(null != o && name.equals("java.util.List") && field.isAnnotationPresent(ConvertProperty.class)){
                    if(!((List) o).isEmpty()){
                        for (Object item : (List) o) {
                            Class cls = item.getClass();
                            if (cls.isAnnotationPresent(ConvertDefault.class)) {
                                evaluation(item, cls.getDeclaredFields());
                            }
                        }
                    }
                }
            }
        }
    }

    if(result instanceof List){
      if(!((List) result).isEmpty()){
          for (Object item : ((List) result)) {
              Class cls = item.getClass();
              if (cls.isAnnotationPresent(ConvertDefault.class)) {
                  evaluation(item, cls.getDeclaredFields());
              }

          }
      }
    }
    return result;
}


private void evaluation(Object obj, Field[] fields) throws IllegalAccessException {
    if (fields.length > 0) {
        for (Field field : fields) {
            field.setAccessible(true);
            Object o = field.get(obj);
            String name = field.getType().getName();
            if(null == o && name.equals("java.lang.Integer")){
                field.set(obj, 0);
            }else if(null == o && name.equals("java.math.BigDecimal")){
                field.set(obj, BigDecimal.ZERO);
            }
        }
    }
}

}

VO实体加注解

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.TYPE})
@Documented
public @interface ConvertDefault {

}

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.FIELD})
@Documented
public @interface ConvertProperty {
}

VO类


@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ConvertDefault
@ApiModel(value = “MySpByProductOutStockExeVO对象”, description = “MySpByProductOutStockExe”)
public class MySpByProductOutStockExeVO implements Serializable {

private static final long serialVersionUID = 1L;

@JsonProperty(“TargetWarehouse”)
private String targetwarehouse;

@JsonProperty(“plan_no”)
private String planNo;

@JsonProperty(“main_product_code”)
private String mainProductCode;

@JsonProperty(“main_product_name”)
private String mainProductName;

@JsonProperty(“car_num”)
private Integer carNum;

@JsonProperty(“actual_weight”)
private BigDecimal actualWeight;

@JsonProperty(“FStartTime”)
private Date fstarttime;

@JsonProperty(“FEndTime”)
private Date fendtime;

@JsonProperty(“RealWeight”)
private BigDecimal realweight;

@JsonProperty(“FOrderBackStatus”)
private String forderbackstatus;

@JsonProperty(“PTypeName”)
private String ptypename;

@JsonProperty(“plan_pre_goods_time”)
private Date planPreGoodsTime;

@JsonProperty(“crm_area”)
private String crmArea;

@JsonProperty(“FProductType”)
private String fproducttype;

@JsonProperty(“FDeskName”)
private String fdeskname;

@JsonProperty(“FDeskCode”)
private String fdeskcode;

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/921348.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号