com.englishtown:vertx-mod-jersey

Allows creating JAX-RS jersey resources that will handle incoming http requests to vert.x

License

License

Categories

Categories

Jersey Program Interface REST Frameworks
GroupId

GroupId

com.englishtown
ArtifactId

ArtifactId

vertx-mod-jersey
Last Version

Last Version

3.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Allows creating JAX-RS jersey resources that will handle incoming http requests to vert.x
Project URL

Project URL

https://github.com/englishtown/vertx-mod-jersey
Project Organization

Project Organization

Englishtown

Download vertx-mod-jersey

How to add to project

<!-- https://jarcasting.com/artifacts/com.englishtown/vertx-mod-jersey/ -->
<dependency>
    <groupId>com.englishtown</groupId>
    <artifactId>vertx-mod-jersey</artifactId>
    <version>3.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.englishtown/vertx-mod-jersey/
implementation 'com.englishtown:vertx-mod-jersey:3.0.1'
// https://jarcasting.com/artifacts/com.englishtown/vertx-mod-jersey/
implementation ("com.englishtown:vertx-mod-jersey:3.0.1")
'com.englishtown:vertx-mod-jersey:jar:3.0.1'
<dependency org="com.englishtown" name="vertx-mod-jersey" rev="3.0.1">
  <artifact name="vertx-mod-jersey" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.englishtown', module='vertx-mod-jersey', version='3.0.1')
)
libraryDependencies += "com.englishtown" % "vertx-mod-jersey" % "3.0.1"
[com.englishtown/vertx-mod-jersey "3.0.1"]

Dependencies

compile (2)

Group / Artifact Type Version
org.glassfish.jersey.core : jersey-server jar 2.11
asm : asm jar 3.3.1

provided (7)

Group / Artifact Type Version
com.englishtown : vertx-mod-hk2 jar 1.7.0
com.englishtown : vertx-mod-guice jar 1.3.0-final
org.glassfish.hk2 : guice-bridge jar 2.3.0-b05
com.google.inject.extensions : guice-multibindings jar 3.0
com.englishtown : when.java jar 3.0.0
io.vertx : vertx-core jar 2.1.1
io.vertx : vertx-platform jar 2.1.1

test (5)

Group / Artifact Type Version
org.glassfish.jersey.media : jersey-media-json-jackson jar 2.11
org.mockito : mockito-all jar 1.9.5
com.englishtown : when.java-testtools jar 3.0.0
junit : junit jar 4.11
io.vertx : testtools jar 2.0.3-final

Project Modules

There are no modules declared in this project.

vertx-jersey

Allows creating JAX-RS Jersey resources in vert.x.

Build Status Maven Central

Getting started

Add the vertx-jersey dependency to your project

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

See maven-simplest for getting started. There are additional example modules covering: injection, filters, serialization, swagger etc.

Version Matrix

vertx-jersey jersey vert.x
4.7.0 2.25.1 3.5.0
4.6.0 2.25.1 3.4.2
4.5.0 2.23.1 3.3.0
4.4.2 2.23 3.2.1
4.3.0 2.22.1 3.2.0
4.2.0 2.22 3.1.0
4.0.0 2.18 3.0.0
3.0.1 (vertx-mod-jersey) 2.11 2.x

There are multiple ways to start the Jersey Server:

1. Run vertx-jersey as a service

Running as a service is probably the easiest way to get started.

From the command line:

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

Programmatically:

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

See the maven-service example.

NOTE: When running as a service, vertx-hk2 must be on the class path.

2. Run the verticle

Rather than running as a service, you can run the JerseyVerticle from the command line:

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

Or programmatically

vertx.deployVerticle("java-hk2:com.englishtown.vertx.jersey.JerseyVerticle", config);
vertx.deployVerticle("java-guice:com.englishtown.vertx.jersey.JerseyVerticle", config);

This assumes you have vertx-hk2 or vertx-guice on the class path as well as vertx-jersey and all its dependencies.

3. Create JerseyServer yourself

You can also skip the JerseyVerticle and instantiate the JerseyServer yourself. It is easiest to use DI for this, but it can also be done manually.

Configuration

The vertx-jersey configuration is as follows:

{
    "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 - The host or ip address to listen at for connections. 0.0.0.0 means listen at all available addresses. Default is 0.0.0.0
  • port - The port to listen at for connections. Default is 80.
  • ssl - Should the server use https as a protocol? Default is false.
  • base_path - The base path jersey responds to. Default is /.
  • packages - An array of package names to inspect for resources. Can also use json field resources.
  • components - An array of components class names to inject (features, etc.). For example: "org.glassfish.jersey.jackson.JacksonFeature". Can also use json field features.
  • instances - An array of singleton instances to be created and registered (HK2 binders etc.). Can also use json field binders.
  • properties - An object with additional properties to be set on the ResourceConfig. Can also use json field resource_config.
  • compression_supported - A boolean whether the server supports compression. Default is false.
  • jks_options - A JSON object to create the io.vertx.core.net.JksOptions. Only used if ssl is true.
  • receive_buffer_size - The int receive buffer size. The value is optional.
  • max_body_size - The int max body size allowed. The default value is 1MB.
  • backlog_size - An int that sets the http server backlog size. The default value is 10,000

You must configure at least one package or component.

You can also group the jersey configuration under a jersey json field:

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

Examples

Simple
{
    "resources": ["com.englishtown.vertx.jersey.resources"]
}
All settings
{
    "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 Resource Injection

The javax.ws.rs.core.Context annotation can be used to inject vert.x objects into a resource constructor, field, or method parameter. Supported vert.x objects include

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

To inject custom objects, you must provide one or more binders in the configuration. See the injection example projects.

Dependency Injection

The JerseyVerticle requires dependency injection. Guice and HK2 binders are provided:

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

See the examples directory for runnable hk2 and guice samples.

vertx-guice

If using vertx-guice, ensure the vertx-guice jar is on the class path so vert.x registers the GuiceVerticleFactory.

Note: The Guice Multibindings extension is required.

vertx-mod-hk2

If using vertx-hk2, ensure the vertx-hk2 jar is on the class path so vert.x registers the HK2VerticleFactory.

Note: if you are using vertx-mod-hk2, ensure you are using 1.7.0 or higher.

Example Resource Method

@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

The promises package provides when.java wrappers to create a JerseyServer. You must provide the when.java dependency.

Example

The following example assumes a com.englishtown.vertx.jersey.promises.WhenJerseyServer instance has been injected using the com.englishtown.vertx.hk2.WhenHK2JerseyBinder with vertx-hk2 module.

    @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;
                });

    }
com.englishtown

Labs @ EF Education First

Repositories moving to https://github.com/ef-labs

Versions

Version
3.0.1
3.0.0
3.0.0-RC1
2.7.1
2.7.0
2.6.1
2.6.0
2.5.0
2.4.0-final
2.3.0-final
2.2.0-final
2.1.0-final
2.0.0-final
1.0.0-final
1.0.0-CR3
1.0.0-CR2
1.0.0-beta5
1.0.0-beta4
1.0.0-beta3
1.0.0-beta1