Put it to REST!

Spring Boot REST Client Auto Configuration

License

License

GroupId

GroupId

org.zalando
ArtifactId

ArtifactId

put-it-to-rest
Last Version

Last Version

0.13.0
Release Date

Release Date

Type

Type

jar
Description

Description

Put it to REST!
Spring Boot REST Client Auto Configuration
Project URL

Project URL

https://github.com/zalando-incubator/put-it-to-rest
Project Organization

Project Organization

Zalando SE
Source Code Management

Source Code Management

https://github.com/zalando-incubator/put-it-to-rest

Download put-it-to-rest

How to add to project

<!-- https://jarcasting.com/artifacts/org.zalando/put-it-to-rest/ -->
<dependency>
    <groupId>org.zalando</groupId>
    <artifactId>put-it-to-rest</artifactId>
    <version>0.13.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.zalando/put-it-to-rest/
implementation 'org.zalando:put-it-to-rest:0.13.0'
// https://jarcasting.com/artifacts/org.zalando/put-it-to-rest/
implementation ("org.zalando:put-it-to-rest:0.13.0")
'org.zalando:put-it-to-rest:jar:0.13.0'
<dependency org="org.zalando" name="put-it-to-rest" rev="0.13.0">
  <artifact name="put-it-to-rest" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.zalando', module='put-it-to-rest', version='0.13.0')
)
libraryDependencies += "org.zalando" % "put-it-to-rest" % "0.13.0"
[org.zalando/put-it-to-rest "0.13.0"]

Dependencies

compile (13)

Group / Artifact Type Version
com.google.code.findbugs : jsr305 jar 3.0.2
com.google.gag : gag jar 1.0.1
com.google.guava : guava jar 22.0
org.springframework.boot : spring-boot-autoconfigure jar
org.springframework.boot : spring-boot-configuration-processor Optional jar
org.zalando : riptide-core jar 2.4.0
org.zalando : riptide-httpclient jar 2.4.0
org.zalando : riptide-stream jar 2.4.0
org.zalando : riptide-exceptions Optional jar 2.4.0
org.zalando : riptide-hystrix Optional jar 2.4.0
org.zalando : tracer-spring-boot-starter jar 0.11.2
org.zalando : logbook-spring-boot-starter jar 1.4.0
org.zalando.zmon : zmon-actuator Optional jar 0.9.7

provided (3)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.16
org.zalando.stups : tokens jar 0.11.0-beta-2
org.zalando.stups : stups-http-components-oauth2 jar 1.0.19

test (9)

Group / Artifact Type Version
junit : junit jar 4.12
org.hamcrest : java-hamcrest jar 2.0.0.0
org.hobsoft.hamcrest : hamcrest-compose jar 0.3.0
org.mockito : mockito-core jar 2.0.93-beta
org.springframework.boot : spring-boot-starter jar 1.5.4.RELEASE
org.springframework : spring-test jar
com.github.stefanbirkner : system-rules jar 1.16.1
org.springframework.boot : spring-boot-starter-web jar
org.springframework.boot : spring-boot-starter-test jar

Project Modules

There are no modules declared in this project.

Deprecated

Please head over to zalando/riptide and use the riptide-spring-boot-starter instead.

Migration

Please follow these steps to migrate from Put it to REST! to the Riptide Spring Boot Starter:

  • Change rest key to riptide
  • Change connection-timeout to connect-timeout
  • Remove plugins and replace
    • original-stack-trace with preserve-stack-trace
    • temporary-exception with detect-transient-faults
  • No hystrix support, use failsafe, if applicable
  • Different defaults
    • Maximum connections per route was increased from 2 to 20
    • Thread pool has a bounded queue by default and a size of 0

Put it to REST!

Relaxing frog

Build Status Coverage Status Javadoc Release Maven Central License

put to rest verb: to finish dealing with something and forget about it

Put it to REST! is a library that seamlessly integrates various REST- and HTTP-related client-side tools in the easiest and convenient way possible. It solves a recurring problem of bootstrapping and wiring different libraries together whenever interaction with a remote service is required. Spinning up new clients couldn't get any easier!

  • Technology stack: Spring Boot
  • Status: Beta

Example

rest.clients:
  example:
    base-url: http://example.com
    oauth.scopes:
      - example.read
@RestClient("example")
private Rest example;

Features

Dependencies

  • Java 8
  • Any build tool using Maven Central, or direct download
  • Spring Boot
  • Riptide
  • Logbook
  • Tracer
  • Tokens
  • Apache HTTP Client
  • Hystrix

Installation

Add the following dependency to your project:

<dependency>
    <groupId>org.zalando</groupId>
    <artifactId>put-it-to-rest</artifactId>
    <version>${put-it-to-rest.version}</version>
</dependency>

If you want OAuth support you need to additionally add stups-spring-oauth2-support to your project. It comes with Tokens equipped.

<!-- if you need OAuth support additionally add: -->
<dependency>
    <groupId>org.zalando.stups</groupId>
    <artifactId>stups-http-components-oauth2</artifactId>
    <version>$stups-http-components-oauth2.version}{</version>
</dependency>

Configuration

You can now define new REST clients and override default configuration in your application.yml:

rest:
  defaults:
    connection-timeout: 1 seconds
    socket-timeout: 2 seconds
    connection-time-to-live: 30 seconds
    max-connections-per-route: 16
    max-connections-total: 16
    plugins:
      - original-stack-trace
  oauth:
    access-token-url: https://auth.example.com
    credentials-directory: /secrets
    scheduling-period: 10 seconds
    connection-timeout: 1 second
    socket-timeout: 1500 milliseconds
  clients:
    example:
      base-url: https://example.com
      connection-timeout: 2 seconds
      socket-timeout: 3 seconds
      oauth.scopes:
        - uid
        - example.read
      plugins:
        - original-stack-trace
        - temporary-exception
        - hystrix
      compress-request: true
    trusted:
      base-url: https://my.trusted.com
      keystore:
        path: trusted.keystore
        password: passphrase

Clients are identified by a Client ID, for instance example in the sample above. You can have as many clients as you want.

For a complete overview of available properties, they type and default value please refer to the following table:

Configuration Type Required Default
rest.defaults.connection-timeout TimeSpan no 5 seconds
rest.defaults.socket-timeout TimeSpan no 5 seconds
rest.defaults.connection-time-to-live TimeSpan no 30 seconds
rest.defaults.max-connections-per-route int no 2
rest.defaults.max-connections-total int no maximum of 20 and per route
rest.defaults.plugins List<String> no [original-stack-trace]
rest.oauth.access-token-url URI no env var ACCESS_TOKEN_URL
rest.oauth.credentials-directory Path no env var CREDENTIALS_DIR
rest.oauth.scheduling-period TimeSpan no 5 seconds
rest.oauth.connetion-timeout TimeSpan no 1 second
rest.oauth.socket-timeout TimeSpan no 2 seconds
rest.oauth.connection-time-to-live TimeSpan no see rest.defaults.connection-time-to-live
rest.clients.<id>.base-url URI no none
rest.clients.<id>.connection-timeout TimeSpan no see rest.defaults.connection-timeout
rest.clients.<id>.socket-timeout TimeSpan no see rest.defaults.socket-timeout
rest.clients.<id>.connection-time-to-live TimeSpan no see rest.defaults.connection-time-to-live
rest.clients.<id>.max-connections-per-route int no see rest.defaults.max-connections-per-route
rest.clients.<id>.max-connections-total int no see rest.defaults.max-connections-total
rest.clients.<id>.oauth no none, disables OAuth2 if omitted
rest.clients.<id>.oauth.scopes List<String> no none
rest.clients.<id>.plugins List<String> no [original-stack-trace]
rest.clients.<id>.compress-request boolean no false
rest.clients.<id>.keystore.path String no none
rest.clients.<id>.keystore.password String no none

Usage

After configuring your clients, as shown in the last section, you can now easily inject them:

@RestClient("example")
private Rest example;

All beans that are created for each client use the Client ID, in this case example, as their qualifier.

Besides Rest, you can also alternatively inject any of the following types per client directly:

  • RestTemplate
  • AsyncRestTemplate
  • ClientHttpRequestFactory
  • AsyncClientHttpRequestFactory
  • HttpClient
  • ClientHttpMessageConverters
  • AsyncListenableTaskExecutor

A global AccessTokens bean is also provided.

Trusted Keystore

A client can be configured to only connect to trusted hosts (see Certificate Pinning) by configuring the keystore key. Use keystore.path to refer to a JKS keystore on the classpath/filesystem and (optionally) specify the passphrase via keystore.password.

You can generate a keystore using the JDK's keytool:

./keytool -importcert -file some-cert.crt -keystore my.keystore -alias "<some-alias>"

Customization

For every client that is defined in your configuration the following graph of beans, indicated by the green color, will be created:

Client Dependency Graph

Regarding the other colors:

  • yellow: will be created once and then shared across different clients (if needed)
  • red: mandatory dependency
  • grey: optional dependency

Every single bean in the graph can optionally be replaced by your own, custom version of it. Beans can only be overridden by name, not by type. As an example, the following code would add XML support to the example client:

@Bean
@Qualifier("example")
public ClientHttpMessageConverters exampleHttpMessageConverters() {
    return new ClientHttpMessageConverters(new Jaxb2RootElementHttpMessageConverter());
}

The following table shows all beans with their respective name (for the example client) and type:

Bean Name Bean Type Configures by default
accessToken (no client prefix!) AccessTokens OAuth settings
exampleHttpMessageConverters ClientHttpMessageConverters Text, JSON and JSON Stream
exampleHttpClient HttpClient Interceptors and timeouts
exampleAsyncClientHttpRequestFactory AsyncClientHttpRequestFactory and ClientHttpRequestFactory
exampleRest Rest Base URL
exampleRestTemplate RestTemplate Base URL
exampleAsyncRestTemplate AsyncRestTemplate Base URL
exampleAsyncListenableTaskExecutor AsyncListenableTaskExecutor ConcurrentTaskExecutor

If you override a bean then all of its dependencies (see the graph), will not be registered, unless required by some other bean.

Getting Help

If you have questions, concerns, bug reports, etc., please file an issue in this repository's Issue Tracker.

Getting Involved/Contributing

To contribute, simply make a pull request and add a brief description (1-2 sentences) of your addition or change. For more details, check the contribution guidelines.

org.zalando

The Zalando Incubator

Projects in development at Zalando. We accept contributions and feedback—leave an issue at the relevant project's issues tracker.

Versions

Version
0.13.0
0.12.2
0.12.1
0.12.0
0.11.2
0.11.1
0.11.0
0.10.0
0.9.0
0.8.0
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.0