现在趋向于前台和后台分层开发
前端调用后台接口获取数据,然后在页面展示.
但是有时候前台(静态资源)和后台联调时很麻烦:
(1)后台依赖协作方,里面的内容很难遍历各种情况;
(2)后台的数据修改起来很麻烦,因为我们没法直接修改数据库,因为数据库在协作方那里.
所以我想到了一个解决方法:stub
stub,说白了,就是造假数据
当然假数据符合协作方的API文档规范
因为通常情况下,需求出来后,协作方接口正在开发,
但是我们应用需要调用协作方接口进行开发
但是总不能等着他们开发完成吧
所以让他们先出API文档,然后我们根据API文档搞假数据来开发
先看下我是如何操作的.
我使用JMeter进行接口测试
执行结果如下:
但是,你们知道返回的数据来自哪里吗?
来自于如下文件:
orderContent.json的内容就是返回结果:
所以你想要返回什么内容,就可以直接修改对应的.json 文件.
规范:
比如你访问路径:/tv_mobile/stub/appstore/orderContent
那么stub目录下面一定有一个文件夹叫appstore,appstore 下面一定有一个文件orderContent.json
再比如你访问路径:/tv_mobile/stub/orgList
那么stub目录下面一定有一个文件叫orgList.json
项目使用spring MVC 框架
核心控制器:
- package com.web.controller.comm;
- import java.io.FileInputStream;
- import java.io.IOException;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.log4j.Logger;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import com.common.util.SystemHWUtil;
- import com.common.util.WebServletUtil;
- import com.io.hw.file.util.FileUtils;
- import com.util.JSONPUtil;
- /***
- * 用于stub
- *
- * @author huangweii 2015年5月29日
- */
- @Controller
- @RequestMapping("/stub")
- public class StubController {
- protected Logger logger = Logger.getLogger(this.getClass());
- public static final String stub_folder = "stub/";
- public static final String stub_file_Suffix = ".json";
- /***
- *
- * @param request
- * @param action
- * @param callback
- * @return
- */
- @ResponseBody
- @RequestMapping(value = "/{action}", produces = SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF)
- public String corsJsonSimple(HttpServletRequest request,
- @PathVariable String action, String callback) {
- String content = null;
- content = stub(request, stub_folder + action + stub_file_Suffix);
- return JSONPUtil.getJsonP(content, callback);
- }
- /***
- *
- * @param request
- * @param namespace
- * @param action
- * @param callback
- * @return
- */
- @ResponseBody
- @RequestMapping(value = "/{namespace}/{action}", produces = SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF)
- public String corsJsonSimple(HttpServletRequest request,
- @PathVariable String namespace, @PathVariable String action,
- String callback) {
- String content = null;
- content = stub(request, stub_folder + namespace + "/" + action
- + stub_file_Suffix);
- return JSONPUtil.getJsonP(content, callback);
- }
- private String stub(HttpServletRequest request, String path) {
- return stub(request, path, SystemHWUtil.CURR_ENCODING);
- }
- /***
- * 读取文件
- * @param request
- * @param path
- * @param charset
- * @return
- */
- private String stub(HttpServletRequest request, String path,String charset) {
- String content = null;
- try {
- String realPath2 = WebServletUtil.getRealPath(request, path);
- java.io.InputStream input = new FileInputStream(realPath2);
- content = FileUtils.getFullContent2(input,charset);
- } catch (java.io.FileNotFoundException ex) {
- ex.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return content;
- }
- }
核心方法:
- /**
- * 获取相对地址的绝对地址
- *
- * @param request
- * @param relativePath
- * @return
- */
- public static String getRealPath(HttpServletRequest request, String relativePath) {
- return request.getSession().getServletContext().getRealPath(relativePath);
- }
--------控制器更新了 2016-01-24--------
- package oa.web.controller.common;
- import com.common.util.SystemHWUtil;
- import com.common.util.WebServletUtil;
- import com.io.hw.file.util.FileUtils;
- import com.io.hw.json.HWJacksonUtils;
- import com.string.widget.util.ValueWidget;
- import org.apache.log4j.Logger;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import javax.servlet.http.HttpServletRequest;
- import java.io.FileInputStream;
- import java.io.IOException;
- /***
- * 用于stub
- *
- * @author huangweii 2015年5月29日
- */
- @Controller
- @RequestMapping("/stub")
- public class StubController {
- public static final String stub_folder = "stub/";
- public static final String stub_file_Suffix = ".json";
- protected Logger logger = Logger.getLogger(this.getClass());
- /***
- * @param request
- * @param action
- * @param callback
- * @return
- */
- @ResponseBody
- @RequestMapping(value = "/{action}", produces = SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF)
- public String corsJsonSimple(HttpServletRequest request,
- @PathVariable String action, String callback, String charset) {
- return stubAction(request, stub_folder + action + stub_file_Suffix, callback, charset);
- }
- /***
- * @param request
- * @param namespace
- * @param action
- * @param callback
- * @return
- */
- @ResponseBody
- @RequestMapping(value = "/{namespace}/{action}", produces = SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF)
- public String corsJsonSimple(HttpServletRequest request,
- @PathVariable String namespace, @PathVariable String action,
- String callback
- , String charset) {
- return stubAction(request, stub_folder + namespace + "/" + action
- + stub_file_Suffix, callback, charset);
- }
- private String stubAction(HttpServletRequest request, String actionPath, String callback, String charset) {
- if (ValueWidget.isNullOrEmpty(charset)) {
- charset = SystemHWUtil.CURR_ENCODING;
- }
- String content = stub(request, actionPath, charset);
- logger.info(SystemHWUtil.CRLF + content);
- return HWJacksonUtils.getJsonP(content, callback);
- }
- @ResponseBody
- @RequestMapping(value = "/{group}/{namespace}/{action}", produces = SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF)
- public String stubAction(HttpServletRequest request,
- @PathVariable String group,
- @PathVariable String namespace, @PathVariable String action,
- String callback
- , String charset) {
- return stubAction(request, stub_folder + group + "/" + namespace + "/" + action
- + stub_file_Suffix, callback, charset);
- }
- @ResponseBody
- @RequestMapping(value = "/{version}/{group}/{namespace}/{action}", produces = SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF)
- public String stubAction(HttpServletRequest request,
- @PathVariable String version,
- @PathVariable String group,
- @PathVariable String namespace, @PathVariable String action,
- String callback
- , String charset) {
- return stubAction(request, stub_folder + version + "/" + group + "/" + namespace + "/" + action
- + stub_file_Suffix, callback, charset);
- }
- private String stub(HttpServletRequest request, String path) {
- return stub(request, path, SystemHWUtil.CURR_ENCODING);
- }
- /***
- * 读取文件
- *
- * @param request
- * @param path
- * @param charset
- * @return
- */
- private String stub(HttpServletRequest request, String path, String charset) {
- String content = null;
- try {
- String realPath2 = WebServletUtil.getRealPath(request, path);
- java.io.InputStream input = new FileInputStream(realPath2);
- content = FileUtils.getFullContent2(input, charset, true);
- } catch (java.io.FileNotFoundException ex) {
- ex.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return content;
- }
- }