1>先安装java8的库,jdk8u181-x64.dmg
官网或者 https://www.cr173.com/mac/122803.html
惹是java库高于java8,就会出现类似错误
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.ibatis.reflection.Reflector(file:/C:/Users/jiangcy/.m2/repository/org/mybatis/mybatis/3.4.5/mybatis-3.4.5.jar)to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.apache.ibatis.reflection.Reflector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2>
下载 moco-runner-0.10.2-standalone.jar
参考
https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/
我使用的是0.10.2,直接下载地址是
3>
在任意位置创建文件夹,我命名FilServiceApi,然后将下载的moco-runner-0.10.2-standalone.jar 放入FilServiceApi文件夹中
同时在当前文件夹新建json文件
使用终端新建单个命令: touch global.json
全部接口用单个文件配置的json
[
{
"request": {
"method" : "get",
"uri": "/getMethod"
},
"response": {
"text": "This is a Get Method!"
}
},
{
"request": {
"method" : "get",
"uri": "/moveToRight"
},
"response": {
"text": "This is a moveToRight Method!"
}
},
{
"request": {
"uri": "/getMethodWithParams",
"queries": {
"param1": "1",
"param2": "2"
}
},
"response": {
"text": "This is a moveToRight Method!"
}
},
{
"request": {
"method" : "post",
"uri": "/postMethod",
"headers" : {
"content-type" : "application/json",
"sessionid": "e566288ba77de98d"
},
"forms" :{
"name" : "admin",
"password" : "123456"
}
},
"response": {
"text": "This is a POST Method!"
}
},
{
"response" : {
"headers" : {
"foo" : "bar"
}
},
"include": "blah.json"
}
]
单个文件配置(就是所有接口和返回数据全部在一个json文件里面)
java -jar moco-runner-0.10.2-standalone.jar start -p 1234 -c global.json(端口自己自定义,不要有冲突)
这个时候就已经启动了接口服务器
重点介绍多个单json文件配置
4>
同样可以在FilServiceApi文件下 终端新建josn文件:
touch config.json
[
{
"include": "login/login_request.json"
},
{
"include": "register/register_request.json"
}
]
同时新建文件夹
login文件夹(登陆接口)
login_request.json
[ { "request": {
"uri": "/test/login",
"method": "get",
"queries": {
"username": "admin",
"password": "123456"
} },
"response": {
"file": "login/login_response.json"
} } ]
login_response.json
{
"HttpStatus": 200,
"HttpData": {
"code": 200,
"message": null,
"data":[
{
"sta_n": 1,
"equip_no": 5640,
"set_no": 4,
"set_nm": "向右运动",
"set_type": "X",
"main_instruction": "4",
"minor_instruction": "-",
"record": true,
"action": "登陆设置",
"value": null,
"canexecution": true,
"VoiceKeys": null,
"EnableVoice": false,
"Reserve1": null,
"Reserve2": null,
"Reserve3": null,
"qr_equip_no": 0
}
]
},
"HttpMessage": null
}
register文件夹(注册接口)
register_request.json
[
{
"request": {
"uri": "/test/register",
"method": "get",
"queries": {
"username": "admin",
"password": "123456"
}
},
"response": {
"file": "register/register_response.json"
}
}
]
register_response.json
{
"HttpStatus": 200,
"HttpData": {
"code": 200,
"message": null,
"data":[
{
"sta_n": 1,
"equip_no": 5640,
"set_no": 4,
"set_nm": "我是注册接口返回数据",
"set_type": "X",
"main_instruction": "4",
"minor_instruction": "-",
"record": true,
"action": "注册",
"value": null,
"canexecution": true,
"VoiceKeys": null,
"EnableVoice": false,
"Reserve1": null,
"Reserve2": null,
"Reserve3": null,
"qr_equip_no": 0
}
]
},
"HttpMessage": null
}
全局文件配置(就是接口和返回数据分散在不同的json文件里面)
java -jar moco-runner-0.10.2-standalone.jar start -p 1235 -g config.json
这个时候查看接口是否正常
http://localhost:1235/test/login?username=admin&password=123456
http://localhost:1235/test/register?username=admin&password=123456
最后参考网址
https://www.jianshu.com/p/0aa0ba7ded5d
https://www.jianshu.com/p/638da64422a0
https://blog.csdn.net/shensky711/article/details/52770686#如何在配置文件添加注释
使用npm全局安装json-server:
npm install -g json-server
可以通过查看版本号,来测试是否安装成功:
json-server -v
json-server --watch db.json