@Component
@Aspect
public class DefaultConvertAOP {
private Listexclude = 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 {
}
@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;
}



