1 postMan通用类:
TestController.java
package com.ddwei.testcontroller; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.commons.lang.StringUtils; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.web.bind.annotation.*; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @RestController @RequestMapping("test") public class TestController { @Autowired private ApplicationContext applicationContext; @RequestMapping(value = "/testApi",method = RequestMethod.POST) @ResponseBody public Object postManExcute(@RequestBody ApiParam apiParam) throws Exception{ Object resultObject = null; try { //获取接口对象 interfaceName Class<?> classz = this.getClass().getClassLoader().loadClass(apiParam.getInterfaceName()); Object oo = applicationContext.getBean(classz); //获取接口对象的所有方法 Method[] methods = classz.getMethods(); Method method = null; //匹配interface取到的methods 和参数送过来的method 进行匹配,匹配上,说明有这个方法 for(Method met :methods){ if(apiParam.getMethodName().equals(met.getName())){ method = met; break; //结束循环体 } } List<Params> paramsList = apiParam.getParamsList(); //参数根据索引排序 paramsList.stream().sorted(Comparator.comparing(Params::getIndex)).collect(Collectors.toList()); List<Object> objects = new ArrayList<>(); for(Params params :paramsList){ if(StringUtils.isBlank(params.getType())){ continue; } Class<?> typeClass = this.getClass().getClassLoader().loadClass(params.getType()); Object object = null; if(typeClass.getName().equals("java.util.List")){ Class<?> fanxingClass = this.getClass().getClassLoader().loadClass(params.getFanxingClass()); object = JSONArray.parseArray(JSONObject.toJSONString(params.getData()),fanxingClass); }else if(typeClass.getName().contains("java.lang")||"java.util.Map".equals(typeClass.getName())){ object = params.getData(); }else{ object = JSONObject.parseObject(JSONObject.toJSONString(params.getData()),typeClass); } if(!typeClass.getName().equals("java.util.List")&&StringUtils.isNotBlank(params.getFanxingClass())){ Class<?> fanxingClass = this.getClass().getClassLoader().loadClass(params.getFanxingClass()); String fanxingField = params.getFanxingFieldName(); String fanxingField1 = "set"+fanxingField.substring(0,1).toUpperCase()+fanxingField.substring(1); String fanxingField2 = "get"+fanxingField.substring(0,1).toUpperCase()+fanxingField.substring(1); Method m2 = object.getClass().getMethod(fanxingField2); Object gg = JSONObject.parseObject(JSONObject.toJSONString(m2.invoke(object)),fanxingClass); Method[] m1s = object.getClass().getMethods(); for(Method m :m1s){ if(m.getName().equals(fanxingField1)){ m.invoke(object,gg); break; } } } objects.add(object); } if(method==null){ return null; } resultObject = method.invoke(oo,objects.toArray()); } catch (ClassNotFoundException e) { throw e; } catch (BeansException e) { throw e; } catch (SecurityException e) { throw e; } catch (NoSuchMethodException e) { throw e; } catch (IllegalAccessException e) { throw e; } catch (IllegalArgumentException e) { throw e; } catch (InvocationTargetException e) { throw e; } return resultObject; } }
ApiParam.java
package com.ddwei.testcontroller; import lombok.Getter; import lombok.Setter; import java.io.Serializable; import java.util.List; @Getter @Setter public class ApiParam implements Serializable { private String interfaceName; private String methodName; private List<Params> paramsList; }
Params.java
package com.ddwei.testcontroller; import lombok.Getter; import lombok.Setter; import java.io.Serializable; @Getter @Setter public class Params implements Serializable { private int index; private String type; private String fanxingClass; private String fanxingFieldName; private Object data; }
诸葛
浙公网安备 33010602011771号