Jersey JAX-RS Provider Integration Bundle for Bootique

Provides Jersey JAX-RS integration with Bootique.

License

License

Categories

Categories

Jersey Program Interface REST Frameworks Bootique User Interface Web Frameworks
GroupId

GroupId

com.nhl.bootique.jersey
ArtifactId

ArtifactId

bootique-jersey
Last Version

Last Version

0.17
Release Date

Release Date

Type

Type

jar
Description

Description

Jersey JAX-RS Provider Integration Bundle for Bootique
Provides Jersey JAX-RS integration with Bootique.
Project Organization

Project Organization

National Hockey League
Source Code Management

Source Code Management

https://github.com/nhl/bootique-jersey

Download bootique-jersey

How to add to project

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

Dependencies

compile (5)

Group / Artifact Type Version
com.nhl.bootique : bootique jar 0.18
com.nhl.bootique.jetty : bootique-jetty jar 0.16
org.glassfish.jersey.containers : jersey-container-servlet jar 2.21
org.glassfish.hk2 : hk2-api jar 2.4.0-b31
org.glassfish.hk2 : hk2-locator jar 2.4.0-b31

test (8)

Group / Artifact Type Version
junit : junit jar 4.12
org.mockito : mockito-core jar 2.0.31-beta
org.glassfish.jersey.test-framework : jersey-test-framework-core jar 2.21
org.glassfish.jersey.test-framework.providers : jersey-test-framework-provider-inmemory jar 2.21
org.glassfish.jersey.media : jersey-media-multipart jar 2.21
org.slf4j : slf4j-simple jar 1.7.13
com.nhl.bootique : bootique-test jar 0.18
com.nhl.bootique.jetty : bootique-jetty-test jar 0.16

Project Modules

There are no modules declared in this project.

Build Status Maven Central

bootique-jersey

Provides Jersey JAX-RS server and client integration with Bootique. This provides the necessary API to create REST services and consume someone else's REST services.

Jersey Server

Integrates JAX-RS server as a servlet in Bootique. See usage example bootique-rest-demo. A few quick tips:

Add Jersey server capabilities to your Bootique app:

<dependency>
	<groupId>io.bootique.jersey</groupId>
	<artifactId>bootique-jersey</artifactId>
</dependency>

Publish a package with API endpoints:

public void configure(Binder binder) {
    // use any API class from the package to include the entire package
    JerseyModule.extend(binder).addPackage(MyApi.class);
}

Additionally automated annotation-driven object-to-JSON serialization capabilities:

<dependency>
	<groupId>io.bootique.jersey</groupId>
	<artifactId>bootique-jersey-jackson</artifactId>
</dependency>

Exclude null properties from JSON responses:

public void configure(Binder binder) {
    JerseyJacksonModule.extend(binder).skipNullProperties();
}

Jersey Client

Integrates JAX-RS-based HTTP client in Bootique with support for various types of server authentication (BASIC, OAuth2, etc.). Allows to configure multiple client runtime parameters, as well as define server URL endpoints. Implementation is built on top of Jersey and Grizzly connector.

Quick Start

Add the module to your Bootique app:

<dependency>
	<groupId>io.bootique.jersey</groupId>
	<artifactId>bootique-jersey-client</artifactId>
</dependency>

Or if you want HTTPS clients with health checks and metrics:

<dependency>
	<groupId>io.bootique.jersey</groupId>
	<artifactId>bootique-jersey-client-instrumented</artifactId>
</dependency>

Inject HttpClientFactory and create client instances:

@Inject
private HttpClientFactory clientFactory;

public void doSomething() {

    Client client = clientFactory.newClient();
    Response response = client
        .target("https://example.org")
        .request()
        .get();
}

Configuring Connection Parameters

You can specify a number of runtime parameters for your HTTP clients via the app .yml (or any other Bootique configuration mechanism):

jerseyclient:
  followRedirects: true
  readTimeoutMs: 2000
  connectTimeoutMs: 2000
  asyncThreadPoolSize: 10

Mapping URL Targets

In the example above we injected HttpClientFactory (that produced instances of JAX RS Client), and hardcoded the endpoint URL in Java. Instead you can map multiple URLs in the .yml, assigning each URL a symbolic name and optionally providing URL-specific runtime parameters:

jerseyclient:
  targets:
    google:
      url: "https://google.com"
    bootique:
      url: "https://bootique.io"
      followRedirects: false

Now you can inject HttpTargets and acquire instances of WebTarget by name:

@Inject
private HttpTargets targets;

public void doSomething() {

    Response response = targets.newTarget("bootique").request().get();
}

This not only reduces the amount of code, but more importantly allows to manage your URLs (and their runtime parameters) via configuration. E.g. you might use a different URL between test and production environments without changing the code.

Using BASIC Authentication

If your server endpoint requires BASIC authentication, you can associate your Clients and WebTargets with a named auth configuration. One or more named configurations are setup like this:

jerseyclient:
  auth:
    myauth:
      type: basic
      username: myuser
      password: mypassword

When creating a client in the Java code you can reference auth name ("myauth"):

@Inject
private HttpClientFactory clientFactory;

public void doSomething() {

    Client client = clientFactory.newBuilder().auth("myauth").build();
    Response response = client
        .target("https://example.org")
        .request()
        .get();
}

Or you can associate a target with it:

jerseyclient:
  ...
  targets:
    secret:
      url: "https://example.org"
      auth: myauth

Using OAuth2 Authentication

OAuth2 authentication is very similar to BASIC. In fact they are no different on the Java end. In YAML the type should be "oauth2", and an extra "tokenUrl" property is required. Here is an example auth for a Twitter client:

jerseyclient:
  auth:
    twitter:
      type: oauth2
      tokenUrl: https://api.twitter.com/oauth2/token
      username: sdfjkdferefxfkdsf
      password: Efcdsfdsflkurecdsfj
com.nhl.bootique.jersey

Versions

Version
0.17
0.16
0.15
0.14
0.13
0.12
0.11
0.10
0.9
0.8