moco框架的简单搭建

一、moco搭建的注意事项

  • 了解moco相关的请求(https://blog.csdn.net/gx0904/article/details/88218283 )
  • moco的jar包版本:moco-runner-0.12.0-standalone.jar
  • 用到的了跨域的相关知识(https://blog.csdn.net/gx0904/article/details/88221553 )

二、开始搭建

1、首先设置全局的拦截:

注:①file_root:json文件的路径 ②include:json文件名

[ {
    "response":{
      "headers":{
        "Access-Control-Allow-Origin":"*",
        "Access-Control-Allow-Methods":"*",
        "Access-Control-Max-Age":"3600",
        "Access-Control-Allow-Headers":"*",
        "Access-Control-Allow-Credentials":"true",
        "Content-Type":"text/html;charset=utf-8"
      }
    }
  ,"file_root": "default/verify"
  ,"include" : "default_verify.json"
  }]

以下json文件过滤的是get请求,请求只要是get请求,直接访问 default_verify_4010.json文件

[ {
    "description": "过滤get提交,code【4010】"
    ,"request" : {
      "method" : "get"
    }
    ,"response" : {"file":"default_verify_4010.json"}
  }]

default_verify_4010.json文件

{
  "code":4010
  ,"msg":"提交格式错误【必须为post提交】"
}

2、用户登录

注:latency设置登录响应延迟2秒

[{
  "response":{
    "headers":{
      "Access-Control-Allow-Origin":"*",
      "Access-Control-Allow-Methods":"*",
      "Access-Control-Max-Age":"3600",
      "Access-Control-Allow-Headers":"*",
      "Access-Control-Allow-Credentials":"true",
      "Content-Type":"text/html;charset=utf-8"
    },
    "latency":{"duration":1,"unit":"second"}},
    "file_root": "api/login",
    "include" : "login.json"
}]

访问同一接口时,不同用户登录获取信息不同,获取不同的token

[{
    "description":"登录成功",
    "request":{
      "uri":"/user/login_ok",
      "forms":{"userName":"zhang", "userPwd":"123123"}
    },
    "response":{"file":"response_file/login_ok_w_file.json"}
  },  
  {
  	"description":"登录成功",
    "request":{
     	"uri":"/user/login_ok",
      	"forms":{"userName":"admin",  "userPwd":"123123"}
    },
    "response":{"file":"response_file/login_ok_file.json"}
  }]

response_file/login_ok_w_file.json

{
  "code": 2000,
  "msg": "ok",
  "data": {
    "id": 2,
    "userName": "zhang",
    "sex": 2,
    "token": "6666545389012348888856789012"
  }
}

response_file/login_ok_file.json

{
  "code": 2000,
  "msg": "ok",
  "data": {
    "id": 1,
    "userName": "admin",
    "sex": 1,
    "token": "123456789012348888856789012"
  }
}

3、查询

request:设置全局请求

{
  "response":{
    "headers":{
      "Access-Control-Allow-Origin":"*",
      "Access-Control-Allow-Methods":"*",
      "Access-Control-Max-Age":"3600",
      "Access-Control-Allow-Headers":"*",
      "Access-Control-Allow-Credentials":"true",
      "Content-Type":"text/html;charset=utf-8"},
      "latency":{"duration":1,"unit":"second"}
  },
  "request":{"forms":{"ucode":"admin","token":"123456789012348888856789012"}},
  "file_root": "api/surface/user",
  "include" : "user.json"
}

查询时必须带有全局的request请求

[{
    "description": "查询用户的所有菜单",
    "request": {
      "uri": "/get/list/ok",
      "method": "post"
    },
    "response": {
      "file": "response_file/user_list_ok_file.json"
    }
  }]

user_list_ok_file.json

[
	{"code": 0,"msg": "查询成功", "data":{"id":1,"menuName": "菜单一" }},
	{"code": 0,"msg": "查询成功", "data":{"id":2, "menuName": "菜单二"}},
	{"code": 0,"msg": "查询成功", "data":{"id":2, "menuName": "菜单三"}},
	{"code": 0,"msg": "查询成功", "data":{"id":2, "menuName": "菜单四"}}
]

4、全局异常

当未访问到任何借口是,直接访问异常接口

 {
  "response":{"headers":
  {"Access-Control-Allow-Origin":"*",
    "Access-Control-Allow-Methods":"*",
    "Access-Control-Max-Age":"3600",
    "Access-Control-Allow-Headers":"*",
    "Access-Control-Allow-Credentials":"true",
    "Content-Type":"text/html;charset=utf-8"}
  }
,"file_root": "default/error"
,"include" : "default_error.json"
}

异常json文件只有响应

[{
    "description": "为所有未匹配的请求设置默认错误响应【2010】"
    ,"response" : {"file":"default_error_2010.json"}
}]

"default_error_2010.json

{
  "code":2010
  ,"msg":"其他异常【default_error】"
}
    

5.整体框架

整个json文件的读取顺序是自上到下的,全局拦截写在json文件的最顶端,当访问json文件时,最先访问的接口是拦截的接口,只要能访问到,就不再往下访问。全局异常放到json文件的最底部,因为异常接口不管什么接口都可以访问,当所有的接口都没被访问时,说明有异常,进入异常接口,访问收数据,用户先登录,用户登录时,获得相应的token,再根据用户的token和用户名,查询和他对应的数据。不同用户tonken不同,拥有的权限不同

  • 项目结构
    moco框架的简单搭建_第1张图片

setting.json

[
  {
    "response":{
     	"headers":{"Access-Control-Allow-Origin":"*", "Access-Control-Allow-Methods":"*", "Access-Control-MaxAge":"3600","Access-Control-Allow-Headers":"*","Access-Control-Allow-Credentials":"true","Content-Type":"text/html;charset=utf-8" }
     } ,
   	"file_root": "default/verify",
    "include" : "default_verify.json"
  },
  {
  	"response":{
	 	"headers":{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"*","Access-Control-Max-Age":"3600","Access-Control-Allow-Headers":"*","Access-Control-Allow-Credentials":"true","Content-Type":"text/html;charset=utf-8"},"latency":{"duration":1,"unit":"second"}
   	},
   	"file_root": "api/login",
   	"include" : "login.json"
  },
  {
 	"response":{
   		"headers":{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"*","Access-Control-Max-Age":"3600","Access-Control-Allow-Headers":"*","Access-Control-Allow-Credentials":"true","Content-Type":"text/html;charset=utf-8"},"latency":{"duration":1,"unit":"second"}
	},
  	"request":{"forms":{"ucode":"admin","token":"123456789012348888856789012"}},
  	"file_root": "api/surface/user",
  	"include" : "user.json"
	},
  {
 	"response":{
  		"headers":{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"*","Access-Control-Max-Age":"3600","Access-Control-Allow-Headers":"*","Access-Control-Allow-Credentials":"true","Content-Type":"text/html;charset=utf-8"}
	},
	"file_root": "default/error",
	"include" : "default_error.json"
 }
]
  • 运行
    java -jar moco-runner-0.12.0-standalone.jar start -p 8866 -g setting.json

你可能感兴趣的:(moco框架的简单搭建)