1. 程式人生 > >java工具類-接受請求引數,並利用反射呼叫方法

java工具類-接受請求引數,並利用反射呼叫方法

public String a(HttpServletRequest request,HttpServletResponse response) throws JSONException, IOException, ParseException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        response.addHeader( "Access-Control-Allow-Origin", "*" );
        response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
        response.addHeader(
"Access-Control-Allow-Headers", "Content-Type, Authorization, Accept,X-Requested-With"); String reqStr=request.getParameter("sendJson"); long startTime = System.currentTimeMillis(); System.out.println("請求引數---" + reqStr); logger.info("請求引數---" + reqStr); String result
= ""; String transCode = ""; JSONObject jsObj = net.sf.json.JSONObject.fromObject(reqStr); net.sf.json.JSONObject head = net.sf.json.JSONObject.fromObject(jsObj.getJSONObject("head")); net.sf.json.JSONObject body = net.sf.json.JSONObject.fromObject(jsObj.getJSONObject("body"));
if(head.containsKey("transcode")){ transCode = head.getString("transcode"); }else{ logger.info("交易碼不存在!"); JSONObject resp = new JSONObject(); resp.put("code", "9999"); resp.put("message", "交易碼不存在"); result = resp.toString(); return result; } net.sf.json.JSONObject obj = GetResultByTransCode.getResult(transCode); try { ApplicationContext appContext = ContextLoader.getCurrentWebApplicationContext(); Object serviceObject = appContext.getBean(obj.getString("serviceName")); Method methodName = serviceObject.getClass().getMethod(obj.getString("methodName"), JSONObject.class); result = (String) methodName.invoke(serviceObject, body); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } return result; }