码迷,mamicode.com
首页 > 移动开发 > 详细

springmvc Controller对app客户端进行请求版本控制

时间:2015-09-02 12:05:39      阅读:528      评论:0      收藏:0      [点我收藏+]

标签:

/**
 * @author tandg
 *
 */
@Controller
public 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客户端进行请求版本控制

标签:

原文地址:http://my.oschina.net/evanNmin/blog/500545

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!