• vertx-jersey


    允许在vert.x中创建JAX-RS Jersey资源。

    建立状态 Maven中央

    入门

    将vertx-jersey依赖项添加到您的项目中

    <dependency>
        <groupId>com.englishtown.vertx</groupId>
        <artifactId>vertx-jersey</artifactId>
        <version>4.7.0</version>
    </dependency>

    有关入门,请参见maven-simplest还有其他示例模块,涵盖:注入,过滤器,序列化,摇动等。


    有多种方法启动Jersey服务器:

    1.运行vertx-jersey即服务

    作为服务运行可能是最简单的入门方法。

    在命令行中:

    vertx run service:com.englishtown.vertx:vertx-jersey:4.7.0 -conf config.json

    以编程方式:

    vertx.deployVerticle("service:com.englishtown.vertx:vertx-jersey:4.7.0", config);

    请参阅maven-service示例。

    注意:作为服务运行时,vertx-hk2 必须位于类路径上。

    2.运行垂直

    您可以从命令行运行JerseyVerticle,而不是作为服务运行:

    vertx run java-hk2:com.englishtown.vertx.jersey.JerseyVerticle -conf config.json
    vertx run java-guice:com.englishtown.vertx.jersey.JerseyVerticle -conf config.json
    

    或以编程方式

    vertx run java-hk2:com.englishtown.vertx.jersey.JerseyVerticle -conf config.json
    vertx run java-guice:com.englishtown.vertx.jersey.JerseyVerticle -conf config.json

    假设您在类路径上有vertx-hk2或vertx-guice以及vertx-jersey及其所有依赖项。

    3.自己创建JerseyServer

    您也可以跳过JerseyVerticle和实例化JerseyServer自己。为此,最容易使用DI,但是也可以手动完成。

    组态

    vertx-jersey配置如下:

    {
        "host": "<host>",
        "port": <port>,
        "ssl": <ssl>,
        "base_path": "<base_path>",
        "packages": ["<packages>"],
        "components": ["<components>"],
        "instances": ["<instances>"],
        "properties": {"<properties>"},
        "compression_supported": <compression_supported>,
        "jks_options": <jks_options>,
        "receive_buffer_size": <receive_buffer_size>,
        "max_body_size": <max_body_size>,
        "backlog_size": <backlog_sze>
    }
    • host-侦听连接的主机或IP地址。0.0.0.0表示监听所有可用地址。默认是0.0.0.0
    • port-用于侦听连接的端口。默认值为80
    • ssl-服务器应该https用作协议吗?默认值为false
    • base_path-基本路径球衣响应。默认值为/
    • packages-检查资源的一组软件包名称。也可以使用json字段resources
    • components-要注入的组件类名称的数组(功能等)。例如:"org.glassfish.jersey.jackson.JacksonFeature"也可以使用json字段features
    • instances-一组要创建和注册的单例实例(HK2活页夹等)。也可以使用json字段binders
    • properties-在ResourceConfig上设置具有附加属性的对象。也可以使用json字段resource_config
    • compression_supported-布尔值,表示服务器是否支持压缩。默认值为false
    • jks_options-创建io.vertx.core.net.JksOptions的JSON对象。只有使用,如果ssltrue
    • receive_buffer_size-int接收缓冲区的大小。该值是可选的。
    • max_body_size-允许的最大int主体大小。默认值为1MB。
    • backlog_size-设置http服务器待办事项大小的整数。默认值为10,000

    您必须配置至少一个软件包或组件。

    您还可以在jerseyjson字段下将jersey配置分组

    {
        "jersey": {
            "host": "<host>",
            "packages": "<packages>"
            ....
        }
    }

    例子

    简单
    {
        "resources": ["com.englishtown.vertx.jersey.resources"]
    }
    所有设定
    {
        "host": "localhost",
        "port": 8080,
        "base_path": "/rest",
        "resources": ["com.englishtown.vertx.jersey.resources", "com.englishtown.vertx.jersey.resources2"],
        "features": ["org.glassfish.jersey.jackson.JacksonFeature"],
        "binders": ["com.englishtown.vertx.jersey.AppBinder"]
    }

    Vertx资源注入

    javax.ws.rs.core.Context注释可用于物体注入vert.x到资源构造,场,或方法参数。支持的vert.x对象包括

      • io.vertx.core.http.HttpServerRequest
      • io.vertx.core.http.HttpServerResponse
      • io.vertx.core.streams.ReadStream<io.vertx.core.http.HttpServerRequest>
      • io.vertx.core.Vertx

    要注入自定义对象,必须在配置中提供一个或多个活页夹。请参阅注入示例项目。

    依赖注入

    JerseyVerticle需要依赖项注入。提供Guice和HK2粘合剂:

      • com.englishtown.vertx.guice.GuiceJerseyBinder
      • com.englishtown.vertx.hk2.HK2JerseyBinder

    请参阅示例目录以获取可运行的hk2和guice示例。

    顶点向导

    如果使用vertx-guice,请确保vertx-guice jar位于类路径上,以便vert.x注册GuiceVerticleFactory

    注意:需要Guice Multibindings扩展。

    vertx-mod-hk2

    如果使用vertx-hk2,请确保vertx-hk2 jar在类路径上,以便vert.x注册HK2VerticleFactory

    注意:如果您使用的是vertx-mod-hk2,请确保您使用的是1.7.0或更高版本。

    示例资源方法

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public void getQuery(
            @Suspended final AsyncResponse response,
            @Context ContainerRequest jerseyRequest,
            @Context HttpServerRequest vertxRequest,
            @Context Vertx vertx) {
    
        vertx.runOnLoop(new Handler<Void>() {
            @Override
            public void handle(Void aVoid) {
                response.resume("Hello World!");
            }
        });
    }

    承诺

    promises包提供when.java包装器来创建JerseyServer。您必须提供when.java依赖项。

    以下示例假定com.englishtown.vertx.jersey.promises.WhenJerseyServer已使用com.englishtown.vertx.hk2.WhenHK2JerseyBinderwith vertx-hk2模块注入实例

     @Override
        public void start(Future<Void> startedResult) throws Exception {
    
            JsonObject jerseyConfig = vertx.getOrCreateContext().config().getJsonObject("jersey");
    
            jerseyServer.createServer(jerseyConfig)
                    .then(server -> {
                        startedResult.complete();
                        return null;
                    })
                    .otherwise(t -> {
                        startedResult.fail(t);
                        return null;
                    });
    
        }
  • 相关阅读:
    angularjs学习笔记—事件指令
    JS编写点击页面弹出被点击的标签名
    对数据进行排序
    springBoot集成seata
    maven打包时根据不同的环境生成不同的jar包名称
    单列模式-双重锁校验解析
    hashmap原理简述
    Linux搭建disconf(二)
    Linux搭建dubbo-admin 分布式服务监控中心
    Linux安装zookeeper
  • 原文地址:https://www.cnblogs.com/endv/p/12031349.html
Copyright © 2020-2023  润新知