从浏览器输入参数,到后台处理的vertx程序

vertx由于性能较高,逐渐变得流行。下面将一个vertx的入门案例。

添加依赖

        <!-- vertx -->
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-core</artifactId>
            <version>3.5.0</version>
        </dependency>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-web</artifactId>
            <version>3.5.0</version>
        </dependency>

 

1:创建一个vertical,能够对url进行拦截

   

package payItem.main;

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

import com.yiji.openapi.tool.fastjson.JSON;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;


public class AutoAccountVerticle extends AbstractVerticle{
    
    public void start(){
        Router router=Router.router(vertx);//创建路由
        router.route().handler(BodyHandler.create());  //处理请求体body
        router.route("/autoAccount").handler(          //拦截url
                ctx -> {  //上下文
                    String username=ctx.request().getParam("username");  //获得请求中的参数
                    String password=ctx.request().getParam("password");
                    JsonObject jo=new JsonObject();
                    jo.put("username", username).put("password", password);  //将参数转化为json数据,添加到JsonObject
                    
                    vertx.eventBus().<JsonObject> send(   //事件总线,交由处理程序处理,这次请求
                            AutoAccountService.AUTO_ACCOUNT_SERVICE_URL,   //处理程序的url
                            jo,             //传递给处理程序的消息体,只能是基本数据类型或者JsonObject类型
                            result -> {     //返回结果
                                if(result.succeeded()){   
                                    System.out.println(""+result.result().replyAddress());
                                     ctx.response()  //对前台的响应。
                                    .putHeader("content-type", "application/json")
                                    .end("JSON_CB(Json.encodePrettily(result.result().body()))");  //返回jsonP数据。
//                                     .end(Json.encodePrettily(result.result().body()));  //返回json数据。
                                }else{
                                    ctx.response().setStatusCode(400)
                                    .end(result.cause().toString());
                                }
                                }
                            );
                            
                });
        vertx.createHttpServer().requestHandler(router::accept).listen(8080);
    }


}

2:创建处理事件

   

package payItem.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;


/**
 * 处理程序
 * @author admin
 *
 */
public class AutoAccountService extends AbstractVerticle {

  private SQLClient sqlClient;
  public static String AUTO_ACCOUNT_SERVICE_URL="AUTO_ACCOUNT_SERVICE_URL";

  public void start(){
      
        vertx.eventBus().consumer(AUTO_ACCOUNT_SERVICE_URL,  //处理的URL
                msg -> {//接收的消息
                    JsonObject jo=(JsonObject) msg.body();
                    System.out.println(jo); //处理请求消息
                    msg.reply(jo);  //返回的消息,给result。
                  }
                );
}
}

3:在主函数中发布verticle,每个verticle,都能成为一个服务,只需添加vertx.createHttpServer().requestHandler(router::accept).listen(8080)即可,这样里面就可以设置路由功能。

     但是带有主函数的verticle只能有一个

    

package payItem.main;

import io.vertx.core.Vertx;

public class AutoAccountMain {

    public static void main(String[] args) {
        Vertx vertx=Vertx.vertx(); 
        vertx.deployVerticle(new AutoAccountService());
        vertx.deployVerticle(new AutoAccountVerticle());
    }
}

4:访问:http://localhost:8080/autoAccount?username=1&passward=2

   浏览器显示:

JSON_CB(Json.encodePrettily(result.result().body()))
posted @ 2018-01-30 15:32  1367356  阅读(1616)  评论(0编辑  收藏  举报