生成模拟填参工具

      前后端分离的工作中经常会遇到前端催着要接口,然后又要能有回参.索性就写个通过反射模拟填参工具的模型,写接口的时候自动根据你要返回的对象填充参数,返回前端(因未实际应用,所以没有进一步优化).代码如下:

import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;

/**
 * 模拟回参工具
 */
public class MockUtil {
    public static void main(String[] args) throws Exception {
        Demo demo = new Demo();
        Demo mock = mock(demo);
        System.out.println(new Gson().toJson(mock));
    }

    public static List<String> mockList(int count, String values) {
        List<String> list = new LinkedList<>();
        if (StringUtils.isNotBlank(values)) {
            String str[] = values.split(",");
            list = Arrays.asList(str);
        } else {
            for (int i = 0; i < count; i++) {
                list.add(i + "");
            }
        }
        return list;
    }

    //模拟回参
    public static <T> T mock(T demo) throws Exception {
//        Demo demo = new Demo();
        Field[] fields = demo.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (field.getType() == int.class || field.getType() == Integer.class) {
                field.set(demo, 1);
            } else if (field.getType() == long.class || field.getType() == Long.class) {
                field.set(demo, 1L);
            } else if (field.getType() == short.class) {
                short shortValue = 1;
                field.set(demo, shortValue);
            } else if (field.getType() == float.class) {
                field.set(demo, 0.01f);
            } else if (field.getType() == double.class || field.getType() == Double.class) {
                field.set(demo, 0.01);
            } else if (field.getType() == boolean.class || field.getType() == Boolean.class) {
                field.set(demo, true);
            } else if (field.getType() == BigDecimal.class) {
                field.set(demo, new BigDecimal("1.00"));
            } else if (field.getType() == String.class) {
                field.set(demo, "测试数据");
            } else if (field.getType() == Date.class) {
                field.set(demo, new Date());
            } else if (field.getType() == List.class) {
                List<String> list = Arrays.asList("测试数据1", "测试数据2");
                field.set(demo, list);
            } else if (field.getType() == Set.class) {
                List<String> list = Arrays.asList("测试数据1", "测试数据2");
                field.set(demo, new HashSet<>(list));
            } else if (field.getType() == Map.class) {
                Map<Object, Object> map = new HashMap<Object, Object>() {{
                    put("1", "测试数据1");
                    put("2", "测试数据2");
                }};
                field.set(demo, map);
            } else {
                Object newInstance = field.getType().newInstance();
                field.set(demo, newInstance);
            }
        }
        return demo;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值