import com.alibaba.fastjson.JSON;
import com.topevery.sums.exchange.business.share.vo.*;
import io.swagger.annotations.ApiModelProperty;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* @Author
* @Date 2021/12/24 13:39
* @Version 1.0
* @Descrption
*/
public class shengchengwendang {
public static void main(String[] args) throws Exception {
doc(new PBQ10001VO());
// method(VO);
}
// 生成请求参数文档
public static void doc(Object ob) {
Class aClass = ob.getClass();
Field[] declaredFields = aClass.getDeclaredFields();
for (Field f :
declaredFields) {
String value = " ";
if (f.isAnnotationPresent(ApiModelProperty.class)) {
ApiModelProperty annotation = f.getAnnotation(ApiModelProperty.class);
value = annotation.value();
}
String name = f.getName();
String type = f.getGenericType().toString();
int i = type.lastIndexOf(".");
String substring = type.substring(i + 1);
System.out.println(name + "\t" + value + "\t" + substring);
}
}
/**
* 通过反射获取字段较多的JavaBean数据填充类继而进行测试
* @param model 传入JavaBean的实体
* @return
* @throws IllegalAccessException
* @throws NoSuchMethodException
* @throws InvocationTargetException
*/
public static Object method(Object model) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Class aClass = model.getClass();
List<Field> classs = new ArrayList();
// Field[] declaredFields = aClass.getDeclaredFields();
while (aClass != null) {
classs.addAll(Arrays.asList(aClass.getDeclaredFields()));
aClass = aClass.getSuperclass();
}
for (int i = 1; i < classs.size(); i++) {
Field f = classs.get(i);
String name = f.getName();
name = name.substring(0, 1).toUpperCase() + name.substring(1); // 将属性的首字符大写,方便构造get,set方法
String type = f.getGenericType().toString(); //
String typeName = f.getGenericType().getTypeName();
// System.out.println(typeName);
if (type.equals("class java.lang.String")) { // 如果type是类类型,则前面包含"class ",后面跟类名
Method m = model.getClass().getMethod("get" + name);
String value = (String) m.invoke(model); // 调用getter方法获取属性值
if (value == null) {
m = model.getClass().getMethod("set" + name, String.class);
m.invoke(model, name);
}
}
if (type.equals("class java.lang.Integer")) {
Method m = model.getClass().getMethod("get" + name);
Integer value = (Integer) m.invoke(model);
if (value == null) {
m = model.getClass().getMethod("set" + name, Integer.class);
m.invoke(model, 24657553);
}
}
if (type.equals("class java.util.Date")) {
Method m = model.getClass().getMethod("get" + name);
Date value = (Date) m.invoke(model);
if (value == null) {
m = model.getClass().getMethod("set" + name, Date.class);
m.invoke(model, new Date());
}
}
if (type.equals("class java.lang.Double")) {
Method m = model.getClass().getMethod("get" + name);
Double value = (Double) m.invoke(model);
if (value == null) {
m = model.getClass().getMethod("set" + name, Double.class);
m.invoke(model, 100.12);
}
}
if (type.equals("class java.lang.Long")) {
Method m = model.getClass().getMethod("get" + name);
Long value = (Long) m.invoke(model);
if (value == null) {
m = model.getClass().getMethod("set" + name, Long.class);
m.invoke(model, 600L);
}
}
if (type.equals("class java.math.BigDecimal")) {
Method m = model.getClass().getMethod("get" + name);
BigDecimal value = (BigDecimal) m.invoke(model);
if (value == null) {
m = model.getClass().getMethod("set" + name, BigDecimal.class);
m.invoke(model, new BigDecimal("3.14"));
}
}
}
System.out.println(JSON.toJSONString(model));
return model;
}
生成请求参数文档及测试数据生成
最新推荐文章于 2024-07-10 21:37:49 发布