原文链接:http://www.jianshu.com/p/638da64422a0
我是服务器小白。不了解真正的服务器是怎么搭建的。但是小编在本文将会讲述如何在十分钟内,在自己的电脑上搭建出服务器。并带领大家使用搭建好的服务器。总之,干货满满。
本文源码点此下载:源码
注:本文客户端使用AFNetworking框架来请求测试服务器数据。
本文目录
1. 搭建测试服务器
2. 测试POST、GET网络数据请求
3. 在实际项目中的运用
1. 搭建测试服务器
小编选择moco基于 Java 开发的开源项目来搭建测试服务器。
moco使用的场景是什么呢?
我是一个企业级软件开发人员,每次面对集成就是我头疼开始的时候,漫长集成拉锯战拖延了我们的进度。幸好有了moco,几行配置就可以模拟一个服务,我再也不需要看集成服务团队的脸色了。
我是一个移动开发人员,老板催得紧,可服务器端开发进度慢,我空有一个漂亮的iPhone应用,发挥不出作用。幸好有了moco,很快就可以搭建出一个模拟服务,我再也不用把生命浪费在无效的等待上了。
我是一个前端开发人员,做Inception的时候,客户总想看到一个完整的应用演示,可哪有时间开发后端服务啊!幸好有了moco,几下就可以弄出一个模拟服务,我做的页面一下就有了生命力。
moco搭建出模拟服务器是不是很麻烦啊?
moco有两种使用模式:
- API 模式
- 独立运行模式
如果你编写的是个Java应用(或是以其它JVM上的语言编写的应用),你可以采用直接调用API。
本文采用的是独立运行模式,点击此处下载moco服务端(其实就是一个jar包),当然你可以从官网下载源代码按照官网的说明编译生成jar包。小编不想搞的过于复杂,我们需要的快速搭建测试服务器。小编选择直接下载jar包。
测试打印Hello world!
注意:为了易于表述,小编在桌面上创建moco-server文件夹,我把刚才下载的moco-runner-0.10.2-standalone.jar。其中0.10.2代表的是当前的版本号。
moco-server文件夹里,创建一个test.json文件(.json是文件后缀)。
![](http://upload-images.jianshu.io/upload_images/1255171-664725ce8e7bc071.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在文件里输入以下内容后保存。
[
{
"response": {
"text": "Hello world!"
}
}
]
注:一定要是英文 " 引号 ,否则会报错。使用文件编辑经常会莫名其妙的改变引号,所以小编建议使用
右键-打开方式-选择Xcode
编辑内容。
打开终端,输入以下内容:
//指向moco-server当前目录
cd /Users/macOne/Desktop/moco-server
回车后再输入以下内容:
java -jar moco-runner-0.10.2-standalone.jar start -p 12306 -c test.json
出现下图表示运行成功:
![](http://upload-images.jianshu.io/upload_images/1255171-cc1a681ebda104d4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在浏览器流输入:http://localhost:12306
可以看到"Hello World!"字样。
说明模拟服务器是可以正常使用的。到此服务器就搭建完毕了。是不是很简单!
2. 测试POST、GET网络数据请求
小编将一步步带你使用配置文件配置网络请求和响应数据。
小编在本篇文章介绍客户端常用Http请求方法 Get
和 Post
。moco同样也支持PUT
、 DELETE
方法。
- Get 方法 不带参数
在test.json里输入一下内容
在浏览器里输入[ { "response": { "text": "Hello world!" } }, { "request": { "method" : "get", "uri": "/getMethod" }, "response": { "text": "This is a Get Method!" } } ]
http://localhost:12306/getMethod
回车后结果如下:
This is a Get Method!
-
request 请求
有14个固定的属性:method
,headers
,json
,factory
,uri
,text
,cookies
,xpaths
,json_paths
,version
,file
,queries
,path_resource
,forms
。一定要遵循这些方法。 常用的method(请求方式)
,headers(heads参数)
,uri(url地址)
,file(指定调用的请求文件)
,queries(请求带参)
,forms(表单内容)
。 -
response 响应
有12个固定属性:status
,attachment
,headers
,version
,factory
,file
,text
,proxy
,cookies
,json
,latency
,path_resource
。
在本篇文章中用到file
,text
。
通过命令返回的数据,可以看到当前的数据请求和返回的信息:
![](http://upload-images.jianshu.io/upload_images/1255171-636e949f40a90b3d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
Get方法 带参数
默认的请求方式是Get,你可以不用写method
。
{
"request": {
"uri": "/getMethodWithParams",
"queries": {
"param1": "1",
"param2": "2"
}
},
"response": {
"text": "This is a method with params!"
}
}
注意同一级的属性后要有 " , ",支持运行过程中编辑文件内容。编辑的内容是否合法在终端上会有提示。
在浏览器里输入http://localhost:12306/getMethodWithParams?param1=1¶m2=2
回车后结果如下:
This is a method with params!
- Post 方法
写一个复杂的带有headers,body的Post 请求方法:
{
"request": {
"method" : "post",
"uri": "/postMethod",
"headers" : {
"content-type" : "application/json",
"sessionid": "e566288ba77de98d"
},
"forms" :{
"name" : "Airfei",
"password" : "123456"
}
},
"response": {
"text": "This is a POST Method!"
}
}
小编是通过Paw http客户端请求工具进行验证。
![](http://upload-images.jianshu.io/upload_images/1255171-8c81f1dc6a2659ce.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
以上就是常用的使用配置数据方法。此test.json文件从此处查看。
3. 在实际项目中的运用
我要创建一个项目,结合此项目使用moco服务器。此项目中包括登陆界面和资产列表界面。
在实际项目中,请求接口数据比较多。因此把所有的配置放在同一个文件里,是非常不合适的。需要进行全局配置,参见官网的Global Settings。
前期准备,写配置文件
在moco-server文件夹创建settings.json
,同时创建login和asset两个文件夹(为了方便查找)。
- 在login文件夹内创建
login.json
和login_response.json
两个文件。 - 在asset文件夹内创建
assetList.json
和assetList_response.json
两个文件。
- 在
settings.json
文件输入以下内容://是不是像导入文件头文件 [ { "include" : "login/login.json" }, { "include" : "asset/assetList.json" } ]
login.json
输入以下内容:[ { "request" : { "uri" : "/assetApp/login", "method" : "get", "queries" : { "username" : "Airfei", "password" : "123456" } }, "response" : { "file" : "./login/login_response.json" } } ]
login_response.json
输入以下内容://返回结果OK [ { "status" : "OK" } ]
-
assetList.json
内容//请求资产列表接口 [ { "request" : { "uri" : "/assetApp/assetList", "method" : "post", "headers" : { "content-type" : "application/json", "sessionid": "e566288ba77de98d" } }, "response" : { "file" : "./asset/assetList_response.json" } } ]
assetList_response.json
内容//资产列表清单 [ { "assettype": "TD固定资产", "assetname": "空调", "assetcode": "4435-22333111", "barcode": "4435-22333111", "manufacturer": "天津市xxxxx", "unit": "台", "amount": "2", "addrname": "河北省xxxxxxx", "cost": "3500" }, { "assettype": "TD固定资产", "assetname": "电脑", "assetcode": "4435-2234566", "barcode": "4435-2234566", "manufacturer": "北京市xxxxx", "unit": "台", "amount": "7", "addrname": "河北省xxxxxxx", "cost": "4500" }, { "assettype": "TD固定资产", "assetname": "饮水机", "assetcode": "4735-22333123", "barcode": "4735-22333123", "manufacturer": "上海市xxxxx", "unit": "台", "amount": "5", "addrname": "河北省xxxxxxx", "cost": "1250" }, { "assettype": "TD固定资产", "assetname": "打印机", "assetcode": "4435-2334567", "barcode": "4435-2334567", "manufacturer": "天津市xxxxx", "unit": "台", "amount": "5", "addrname": "河北省xxxxxxx", "cost": "2760" }, { "assettype": "TD固定资产", "assetname": "电脑桌", "assetcode": "4435-2267998", "barcode": "4435-2267998", "manufacturer": "天津市xxxxx", "unit": "台", "amount": "4", "addrname": "河北省xxxxxxx", "cost": "650" }, { "assettype": "TD固定资产", "assetname": "手机", "assetcode": "4435-22345881", "barcode": "4435-22345881", "manufacturer": "天津市xxxxx", "unit": "台", "amount": "2", "addrname": "河北省xxxxxxx", "cost": "5500" }, { "assettype": "TD固定资产", "assetname": "柜式空调", "assetcode": "4435-6666444", "barcode": "4435-6666444", "manufacturer": "天津市xxxxx", "unit": "台", "amount": "1", "addrname": "河北省xxxxxxx", "cost": "8800" }, { "assettype": "TD固定资产", "assetname": "立式衣架", "assetcode": "4435-8944444", "barcode": "4435-8944444", "manufacturer": "天津市xxxxx", "unit": "个", "amount": "2", "addrname": "河北省xxxxxxx", "cost": "300" }, { "assettype": "TD固定资产", "assetname": "加湿器", "assetcode": "4435-3277778", "barcode": "4435-3277778", "manufacturer": "天津市xxxxx", "unit": "台", "amount": "5", "addrname": "河北省xxxxxxx", "cost": "180" }, { "assettype": "TD固定资产", "assetname": "转椅", "assetcode": "4435-7844442", "barcode": "4435-7844442", "manufacturer": "天津市xxxxx", "unit": "个", "amount": "14", "addrname": "河北省xxxxxxx", "cost": "400" } ]
配置完以上内容后在终端里输入以下方法
cd /Users/macOne/Desktop/moco-server
//注意 -g 不是 -c
java -jar moco-runner-0.10.2-standalone.jar start -p 12306 -g settings.json
得到以下内容说明你的配置文件没有错误。Server is started!
INFO Server is started at 12306
INFO Shutdown port is 51783
创建客户端并使用AFNetwoking网络请求
登录界面其实输入用户名和密码请求服务器验证是否通过。其实也可以返回一些认证信息。用于校验以后的所有请求的合法性。
客户端上的登录数据请求代码:
-(void)requestLogin
{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
NSString *url=@"http://localhost:12306/assetApp/login";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//请求所带参数
NSDictionary *dic = @{@"username":_name,
@"password":_pwd
};
[manager GET:url parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSString *receiveStr = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
NSData * data = [receiveStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"登录成功";
hud.removeFromSuperViewOnHide = YES;
NSLog(@"result:%@",jsonDict);
//做一个处理,1秒后刷新。也可以不用加
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//隐藏hub
[hud hide:YES];
//转到下一界面
[[AppDelegate shareDelegate] setupMainViewController];
});
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
hud.mode = MBProgressHUDModeText;
hud.labelText = @"登录失败";
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:1.0];
NSLog(@"登录失败");
}];
}
登录成功后,进入资产列表界面。POST请求服务器获取资产列表数据,在请求数据需要带上headers参数用于验证请求的合法性。请求代码如下:
-(void)requestAssetList
{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
NSString *url=@"http://localhost:12306/assetApp/assetList";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//带有headers参数
[manager.requestSerializer setValue:@"e566288ba77de98d" forHTTPHeaderField:@"sessionid"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager POST:url parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSString *receiveStr = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
NSData * data = [receiveStr dataUsingEncoding:NSUTF8StringEncoding];
_assetArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
//做一个处理,1秒后刷新。模拟请求数据
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//刷新界面
[_tableView reloadData];
//隐藏hub
[hud hide:YES];
});
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
hud.mode = MBProgressHUDModeText;
hud.labelText = @"请求资产列表失败";
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:1.0];
}];
}
源码点击这里获得。
如果觉得文章还不错,请点一波关注吧!干货系列会继续给你带来更多实用的东西。