环境搭建

1.配置java开发环境

安装jdk,并配置环境变量

2.下载moco-runner-1.1.0-standalone.jar

此jar包提供了服务,下载地址:moco-runner-1.1.0-standalone.jar下载

3.新建Api.json,写入内容

[
  {
    "description": "登录接口",
    "request": {
      "uri": "/login",
      "method": "post",
      "json": {
        "username": "zhang",
        "password": "123456"
      }
    },
    "response": {
      "cookies": {
        "token": "86886aa352a959b0b06d8fc3b58cf626"
      },
      "json": {
        "msg": "login success",
        "status": "200"
      },
      "headers":{
        "Content-Type":"text/html;charset=gbk"
      }
    }
  },

  {
    "description": "提交订单接口",
    "request": {
      "uri": "/shop/submitOrder",
      "method": "post",
      "cookies": {
        "token": "86886aa352a959b0b06d8fc3b58cf626"
      },
      "json": {
        "goodsId": "11000",
        "addressId": "99",
        "cardId": 0,
        "buynum": 1
      }
    },
    "response": {
      "status": 200,
      "json": {
        "msg": "提交订单成功",
        "orderNumber": "1343359743",
        "tradestatus": 1
      },
      "headers":{
        "Content-Type":"text/html;charset=gbk"
      }
    }
  },
  {
    "description": "余额支付接口",
    "request": {
      "uri": "/shop/payOrder",
      "method": "post",
      "cookies": {
        "token": "86886aa352a959b0b06d8fc3b58cf626"
      },
      "json": {
        "addressId": "99",
        "cardId": 0,
        "orderId": "1343359743",
        "pay_password": "123456",
        "pay_type": 3,
        "weixin_type": "h5"
      }
    },
    "response": {
      "status": 200,
      "json": {
        "msg": "支付成功",
        "tradestatus": 2
      },
      "headers":{
        "Content-Type":"text/html;charset=gbk"
      }
    }
  },
  {
    "description": "取消订单接口",
    "request": {
      "uri": "/shop/cancelOrder",
      "method": "post",
      "cookies": {
        "token": "86886aa352a959b0b06d8fc3b58cf626"
      },
      "json": {
        "orderId": "1343359743",
        "reason": "Don't like"
      }
    },
    "response": {
      "status": 200,
      "json": {
        "msg": "取消订单成功",
        "tradestatus": 3
      },
      "headers":{
        "Content-Type":"text/html;charset=gbk"
      }
    }
  },

  {
    "description": "获取个人信息",
    "request": {
      "uri": "/user/getUserInfo",
      "method": "get",
      "cookies": {
        "token": "86886aa352a959b0b06d8fc3b58cf626"
      },
      "queries": {
        "orgid": "666"
      }
    },
    "response": {
      "status": 200,
      "json": {
        "uid": "71536528",
        "name": "zhangsan",
        "nickname": "zhangsanfen",
        "age": 88
      },
      "headers":{
        "Content-Type":"text/html;charset=gbk"
      }
    }
  },
  {
    "description": "修改昵称",
    "request": {
      "uri": "/user/modUserInfo",
      "method": "post",
      "cookies": {
        "token": "86886aa352a959b0b06d8fc3b58cf626"
      },
      "forms": {
        "uid": "71536528",
        "nickname": "zhangsifeng"
      }
    },
    "response": {
      "status": 200,
      "json": {
        "msg": "修改昵称成功"
      },
      "headers":{
        "Content-Type":"text/html;charset=gbk"
      }
    }
  }
]

使用jar运行json文件

将下载的jar包和Api.json放在同个目录,执行 java -jar ./moco-runner-0.11.0-standalone.jar http -p 8888 -c Api.json 运行,如果8888端口被占用可以换一个或者kill此端口,当出现下面的提示说明能正常请求接口了

C:\xxx\api_test>java -jar ./moco-runner-1.1.0-standalone.jar http -p 8888 -c Api.json
09 十月 2020 17:04:13 [main] INFO  Server is started at 8888
09 十月 2020 17:04:14 [main] INFO  Shutdown port is 65193

版权声明:如无特殊说明,文章均为本站原创,转载请注明出处

本文链接:http://example.com/article/java_interface/