標籤:
/** * @author tandg * */@Controllerpublic class MainController extends BaseController { Logger log = Logger.getLogger(MainController.class); @Autowired private UserService userService; @RequestMapping(value="/login.do") public String Login(@RequestParam("v") String version) { Method m = null; try { String currMethodName = Thread.currentThread().getStackTrace()[1].getMethodName();//擷取當前請求的方法名 m = getVersionMethod(currMethodName, version); return (String) m.invoke(this); } catch (Exception ex) { log.error("Abnormal request interface :"+ex.getMessage(), ex); JSONObject result = new JSONObject(); result.put("Fail", "Abnormal request interface :"+ex.getMessage()); return result.toString(); } } /** * 最新版本登入方法 */ public String Login() { /** * 處理登入商務邏輯代碼... */ return null; } /** * 舊版本登入方法 */ public String Login1_0() { /** * 處理登入商務邏輯代碼... */ return null; } /** * 處理請求參數 */ public Map<String, Object> getParamMap(HttpServletRequest req) { Map paramMap = new HashMap(); paramMap.putAll(req.getParameterMap()); return paramMap; } /** * controller 版本選擇 * * @param currMethodName * @param version * @return * @throws SecurityException * @throws NoSuchMethodException */ public Method getVersionMethod(String currMethodName, String version) throws SecurityException, NoSuchMethodException { Class c = this.getClass(); Method m = null; //比對版本,如v=1.0 if (version.equalsIgnoreCase(ServerConfig.VERSION)) { m = c.getMethod(currMethodName); } else { m = c.getMethod(currMethodName + version); } return m; }}
springmvc Controller對app用戶端進行請求版本控制