1.入口controller如下:
interfaceClassName:代表类名,即需要调用哪个类 methodName:方法名,即需要调用的interfaceClassName类中的方法 clientId、password业务参数,根据自己需求来确定要不要 reqData:业务参数,所有业务数据封装在里面,根据自己具体需求来写
package com.example.invoketest.controller;
import com.alibaba.fastjson.JSONObject;
import com.example.invoketest.pojo.ReqData;
import org.apache.catalina.core.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@Controller
public class InvokeDemo {
//统一入口
@ResponseBody
@PostMapping("/postdata")
public String postdata(@RequestParam(name = "interfaceClassName") String interfaceClassName,
@RequestParam(name = "methodName") String methodName,
@RequestParam(name = "clientId") String clientId,
@RequestParam(name = "password") String password,
@RequestBody ReqData reqData) throws Exception {
JSONObject jsonObject = reqData.getData();
//通过Class.forName获得类
Class testClass= Class.forName(interfaceClassName);
Object obj=testClass.newInstance();
//调用一个参数的jump
Method m=testClass.getDeclaredMethod(methodName,String.class); //通过类来获得类方法
String result = (String)m.invoke(obj,clientId+'|'+password);//通过invoke调用类方法
//调用2个参数jump
Method m2=testClass.getDeclaredMethod(methodName,String.class,String.class);
String result2 = (String)m2.invoke(obj,clientId,password);
//一个JSONObject参数 传body中的对象
Method m3=testClass.getDeclaredMethod(methodName,JSONObject.class);
String result3 = (String)m3.invoke(obj,jsonObject);
System.out.println("結果:"+result);
return result;
}
}
2.两个服务类如下
1)DogService和DogServiceImpl
package com.example.invoketest.service;
import com.alibaba.fastjson.JSONObject;
public interface DogService {
//jump方法重载
public String jump(String exchangeId);//一个String参数
public String jump(String client,String password);//二个String参数
public String jump(JSONObject jsonObject);//一个JSONObject参数
//eat方法
public String eat(String exchangeId);
public String eat(String client,String password);
public String eat(JSONObject jsonObject);
//其他参数类型自行组合
}
package com.example.invoketest.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.example.invoketest.service.DogService;
import org.springframework.stereotype.Service;
@Service
public class DogServiceImpl implements DogService {
public String jump(String exchangeId){
System.out.println("Dog jump一个String参数:"+exchangeId);
return exchangeId;
}
public String jump(String client,String password){
System.out.println("Dog jump两个个String参数:"+client+password);
return client;
}
public String jump(JSONObject jsonObject){
System.out.println("Dog jump一个JSONObject参数:"+jsonObject.toString());
return jsonObject.toString();
}
public String eat(String exchangeId){
System.out.println("Dog eat,一个String参数:"+exchangeId);
return exchangeId;
}
public String eat(String client,String password){
System.out.println("Dog eat,二个String参数:"+client+password);
return client+password;
}
public String eat(JSONObject jsonObject){
System.out.println("Dog eat一个JSONObject参数:"+jsonObject.toString());
return jsonObject.toString();
}
}
2)PersonService和PersonServiceImpl
package com.example.invoketest.service;
import com.alibaba.fastjson.JSONObject;
public interface PersonService {
public String jump(String exchangeId);//一个Stirng参数
public String jump(String client,String password);//二个Stirng参数
public String jump(JSONObject jsonObject);//一个JSONObject参数
//eat方法
public String eat(String exchangeId);
public String eat(String client,String password);
public String eat(JSONObject jsonObject);
//其他参数类似 就这样组合即可
}
package com.example.invoketest.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.example.invoketest.service.PersonService;
import org.springframework.stereotype.Service;
@Service
public class PersonServiceImpl implements PersonService {
public String jump(String exchangeId){
System.out.println("Person jump,一个String参数:"+exchangeId);
return exchangeId;
}
public String jump(String client,String password){
System.out.println("Person jump,两个个String参数:"+client+password);
return client+password;
}
public String jump(JSONObject jsonObject){
System.out.println("Person jump,一个JSONObject参数:"+jsonObject.toString());
return jsonObject.toString();
}
public String eat(String exchangeId){
System.out.println("Person eat,一个String参数:"+exchangeId);
return exchangeId;
}
public String eat(String client,String password){
System.out.println("Person eat,两个String参数:"+client+password);
return client+password;
}
public String eat(JSONObject jsonObject){
System.out.println("Person eat,两个String参数:"+jsonObject.toString());
return jsonObject.toString();
}
}
3.调用测试
1.调用DogServiceImpl的jump方法
2.调用PersonServiceImpl的jump方法
如上,就通过了传递不同的参数调用不同类的不同方法。