java学习笔记11 - 使用forward实现API接口转发
2013-03-05 14:01
671 查看
在做一个API项目的时候,跟同事讨论使用哪种形式的接口名称更合适,备选的方案有两种:
1. https://xxx.com/openapi/v1/demo/hello?a=aaa&b=bbb
2. https://xxx.com/openapi?api=demo.hello&v=1&a=aaa&b=bbb
最后我们选择第2种,但底层还是第1种,所以两咱方式都是可以正常访问的,提供给调用方的是第2咱形式,这样就只要使用forward做下转发就行了,具体代码如下:
这样就ok了,原来的具体controller不用做任何改动。
1. https://xxx.com/openapi/v1/demo/hello?a=aaa&b=bbb
2. https://xxx.com/openapi?api=demo.hello&v=1&a=aaa&b=bbb
最后我们选择第2种,但底层还是第1种,所以两咱方式都是可以正常访问的,提供给调用方的是第2咱形式,这样就只要使用forward做下转发就行了,具体代码如下:
@ResponseBody @RequestMapping(value="/openapi") public String index(HttpServletRequest request, HttpServletResponse response) { //获取参数 String apiName = request.getParameter("api"); String version = request.getParameter("v"); //设置默认API名称 if (StringUtils.isEmpty(apiName)) { apiName = "demo.hello"; } //替换API名称中的.号为/ apiName = StringUtils.replace(apiName, ".", "/"); //设置默认版本号 if (StringUtils.isEmpty(version)) { version = "1"; } //转发接口 try { request.getRequestDispatcher("/openapi/v"+version+"/"+apiName).forward(request, response); } catch (Exception e) { //TODO log //TODO return error message } return null; }
这样就ok了,原来的具体controller不用做任何改动。
相关文章推荐
- 【Java学习笔记】Comparable接口的实现和使用
- 【Java学习笔记】Comparable接口的实现和使用
- 【使用forward实现API接口转发】
- 【Java学习笔记】Comparable接口的实现和使用
- Java学习笔记11:在公共类中使用访问方法而非公有域
- 【Java学习笔记】Comparable接口的实现和使用
- Java学习笔记-------使用DataOutputStream 、DataInputStream 组合实现C/S通信 :实例
- 【Java学习笔记】Comparable接口的实现和使用
- java学习笔记—使用HttpSession实现QQ的访问记录(31)
- Java多线程学习笔记1——线程的实现方式以及定时器的使用
- Android UI学习笔记 之 使用ViewPager实现导航
- 【Java学习笔记】实现简单的宠物商店管理
- Java学习笔记-解析Java对象的equals()和hashCode()的使用
- Java学习笔记之使用MyEclipse将java项目打包成jar文件,并生成Javadoc帮助文档
- Java学习笔记 实现阿拉伯数字(一亿以内)转换为中文繁体数字
- Java 学习笔记 (3) -抽象类 abstract 接口 interface 内部类 inner class 继承 extends 实现 implements 包 package
- 20160322 javaweb 学习笔记--response验证码实现
- ZooKeeper学习笔记:使用zookeeper的API实现增删查改以及客户端的观察者模式
- 大话设计模式-简单工厂模式(学习笔记与java实现)
- React学习笔记:使用jquery实现动画效果淡入淡出