import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yunva.vertx.test.vertproject.util.JsonUtil;

import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;

public class TestServer {

private static final Logger logger = LoggerFactory.getLogger(Server.class);

public static void main(String[] args){
HttpServerOptions options = new HttpServerOptions().setMaxWebsocketFrameSize(1000000);//Server配置

Vertx vertx = Vertx.vertx();
HttpServer server = vertx.createHttpServer(options);//创建Server

server.requestHandler(request -> {
request.handler(buffer -> {//从body中读取数据
System.out.println("I have received a chunk of the body of length " + buffer.length());
Map<String, Object> map = JsonUtil.jsonToMap(buffer.toString());
System.out.println("name:" +map.get("name") + " school" + map.get("school"));
});

System.out.println("you have recieved a request from " + request.uri());
System.out.println("you have recieved a request from " + request.absoluteURI());//绝对路径
Map<String, Object> map = new HashMap<>();
map.put("name", "hello");
map.put("sdfa", "dsfakd");
//设置返回头
HttpServerResponse response = request.response();
MultiMap headers = response.headers();
headers.set("content-type", "text/html");
headers.set("other-header", "wibble");
request.response().setChunked(true).write(map.toString()).end("you get a reply from the server");
});
server.listen(8080, "127.0.0.1");
}
}
/*
* vertx创建服务接收请求并返回:
* 创建HttpServer-->server.requestHandler---》request.handler---》request.response()
*
* 处理表单请求
* 表单请求分为两种方式:
* 1 普通请求 application/x-www-form-urlencoded
* 2 多文件请求 multipart/form-data
* 对于普通请求,起参数均可以从URL中获取,可像处理一般请求一样处理
* 对于multipart/form-data请求
* server.requestHandler(request -> {
request.setExpectMultipart(true);
request.endHandler(v -> {
// The body has now been fully read, so retrieve the form attributes
MultiMap formAttributes = request.formAttributes();
});
});
处理文件上传
server.requestHandler(request -> {
request.setExpectMultipart(true);
request.uploadHandler(upload -> {
System.out.println("Got a file upload " + upload.name());
});
});

File uploads can be large we don’t provide the entire upload in a single buffer as that might result in
memory exhaustion, instead, the upload data is received in chunks:

request.uploadHandler(upload -> {
upload.handler(chunk -> {
System.out.println("Received a chunk of the upload of length " + chunk.length());
});
});
*
*
*
* */

vertx简单服务创建的更多相关文章

  1. vertx简单客户端创建

    import java.util.HashMap;import java.util.Map; import com.yunva.vertx.test.vertproject.util.JsonUtil ...

  2. SOAPUI使用教程-从现有的服务创建REST模拟服务

    从现有的服务创建REST模拟服务 在您的项目创建一个模拟服务从现有的服务是非常简单的. 右键单击 服务 . 选择 生成模拟服务 . 打开一个对话框窗口. 输入一个描述性的名称. 点击 好吧 . 创建模 ...

  3. WCF服务创建与使用(请求应答模式)

    不说废话,直接上代码.以下服务创建是在独立的WCF类库中,若采用WCF应程程序,定义及创建服务代码均相同,但文件名不同,是CalculatorService.svc 第一步,定义服务契约(Servic ...

  4. 使用普通Windows服务创建Quartz.Net服务项目

    Quartz.NET 项目地址 http://quartznet.sourceforge.net/ 源码下载地址:Quartz.Net.2.0 首先创建Quartz.Net.2.0解决方案,添加 Wi ...

  5. Android应用程序请求SurfaceFlinger服务创建Surface的过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/7884628 前面我们已经学习过Android应 ...

  6. Supervisor 为服务创建守护进程

    今天需要再服务上部署一个.net 方面的项目:当时开启服务的命令只能在前台执行:使用nohub CMD &等放在后台开启服务都会宕机:所以搜寻了Supervisor 这个解决办法,为服务创建守 ...

  7. 前端使用node.js+express+mockjs+mysql实现简单服务端,2种方式模拟数据返回

    今天,我教大家来搭建一个简单服务端 参考文章: https://www.jianshu.com/p/cb89d9ac635e https://www.cnblogs.com/jj-notes/p/66 ...

  8. C#-Windows服务创建和运行

    Windows服务创建和运行    适用场景: ASP.Net通常是一个无状态的提供程序,不支持持续运行代码或者定时执行某段代码,所以我们需要构建自己的Windows服务来运行那些定时任务. 项目中需 ...

  9. MVC CodeFirst简单的创建数据库(非常详细的步骤)

       最近在学习MVC的开发,相信有过开发经验的人初学一个新的框架时候的想法跟我一样最关心的就是这个框架如何架构,每个架构如何分工,以及最最关键的就是如何与数据库通信,再下来才是学习基础的页面设计啊等 ...

随机推荐

  1. rabbitMQ学习(三)

    订阅/广播模式 发送端: import java.io.IOException; import com.rabbitmq.client.ConnectionFactory; import com.ra ...

  2. asp.net core获取HttpContext相关操作

    建立类: using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;us ...

  3. android 简单打jar包

    先建议一个moduel,先写一个下载图片代码: public class LoadTest extends AsyncTask<Void,Void,byte[]>{ public stat ...

  4. memcached tomcat maven 学习记录

    2016.12.11 maven 快速搭建项目,只要有pom.xml文件配置好依赖 可以把项目切割(具体切割出来的块怎么用?) nginx 负载均衡  文件服务器 主要配置nginx.conf 文件 ...

  5. mysql在Linux下

    Linux下查看mysql.apache是否安装,并卸载. 指令 ps -ef|grep mysql 得出结果 root               ?        :: /bin/sh /usr/ ...

  6. 超高性能的json序列化之MVC中使用Json.Net

    先不废话,直接上代码 Asp.net MVC自带Json序列化 /// <summary> /// 加载组件列表 /// </summary> /// <param na ...

  7. 并发下常见的加锁及锁的PHP具体实现-转载

    并发下常见的加锁及锁的PHP具体实现 http://www.cnblogs.com/scotoma/archive/2010/09/26/1836312.html 在最近的项目中有这样的场景 1.生成 ...

  8. eclipse的快捷键(写给eclipse新手)

    Eclipse常用的快捷键 1.常用的快捷键 Alt+/:常用于补全单词.方法,以及内容辅助,可以省很多事情 Ctrl+1:用于全局,快速修正 Ctrl+D  删除整行 Ctrl+F   用于全局 , ...

  9. SRM 146 DIV1 800

    Problem Statement      The purpose of a roundabout is to control the flow of traffic at a busy inter ...

  10. WAP端 经验记录2

    1. LightboxV2 插件 点击A 应该关闭弹层的效果,但是 SAMSUNG 手机上原生浏览器上,看上去不会关闭却跳转了,但当点击回退按钮的时候就会看见弹层已经消失(其实之前的关闭效果已经记录了 ...