根据服务类全路径,方法名,参数全路径,参数json串调用服务类的方法

import com.musi.common.utils.JsonUtils;
import com.musi.common.utils.StringUtils;

import java.lang.reflect.Method;

/**
 * @Author:musi
 * @Date:2019/3/25
 * @Description:
 */
public class ReflectCode {
    public static void reflect(String serviceClassStr,String serviceMethodStr, String param, String requestClassStr) throws Exception {
        if(StringUtils.isNotEmpty(serviceClassStr)&&StringUtils.isNotEmpty(serviceMethodStr)){
            Class serviceClass = Class.forName(serviceClassStr);
            Object service = serviceClass.newInstance();
            Method method = null;
            if(StringUtils.isNotEmpty(requestClassStr)){
                Class requestClass = Class.forName(requestClassStr);
                method = serviceClass.getMethod(serviceMethodStr,requestClass);
                Object request = requestClass.newInstance();

                if(StringUtils.isNotEmpty(param)){
                    request = JsonUtils.parseObject(param,requestClass);
                }
                Object response = method.invoke(service,request);
            }else{
                method = serviceClass.getMethod(serviceMethodStr);
                Object response = method.invoke(serviceClass);
            }
        }else{
            throw new RuntimeException("请求的service类或者方法名为空");
        }
    }
    public static void main(String[] args) throws Exception {
        reflect("com.musi.common.test.reflect.TestService","test","{'param':'1111111'}","com.musi.common.test.reflect.RequestParam");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值