de.dev-eth0.spring-boot.httpclient:httpclient-spring-boot-root

Configuration for the HttpClient for Spring Boot

License

License

Categories

Categories

Spring Boot Container Microservices CLI User Interface
GroupId

GroupId

de.dev-eth0.spring-boot.httpclient
ArtifactId

ArtifactId

httpclient-spring-boot-root
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

pom
Description

Description

Configuration for the HttpClient for Spring Boot
Project URL

Project URL

https://github.com/deveth0/httpclient-spring-boot-starter
Project Organization

Project Organization

dev-eth0
Source Code Management

Source Code Management

https://github.com/deveth0/httpclient-spring-boot-starter

Download httpclient-spring-boot-root

How to add to project

<!-- https://jarcasting.com/artifacts/de.dev-eth0.spring-boot.httpclient/httpclient-spring-boot-root/ -->
<dependency>
    <groupId>de.dev-eth0.spring-boot.httpclient</groupId>
    <artifactId>httpclient-spring-boot-root</artifactId>
    <version>1.1.0</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/de.dev-eth0.spring-boot.httpclient/httpclient-spring-boot-root/
implementation 'de.dev-eth0.spring-boot.httpclient:httpclient-spring-boot-root:1.1.0'
// https://jarcasting.com/artifacts/de.dev-eth0.spring-boot.httpclient/httpclient-spring-boot-root/
implementation ("de.dev-eth0.spring-boot.httpclient:httpclient-spring-boot-root:1.1.0")
'de.dev-eth0.spring-boot.httpclient:httpclient-spring-boot-root:pom:1.1.0'
<dependency org="de.dev-eth0.spring-boot.httpclient" name="httpclient-spring-boot-root" rev="1.1.0">
  <artifact name="httpclient-spring-boot-root" type="pom" />
</dependency>
@Grapes(
@Grab(group='de.dev-eth0.spring-boot.httpclient', module='httpclient-spring-boot-root', version='1.1.0')
)
libraryDependencies += "de.dev-eth0.spring-boot.httpclient" % "httpclient-spring-boot-root" % "1.1.0"
[de.dev-eth0.spring-boot.httpclient/httpclient-spring-boot-root "1.1.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

  • httpclient-spring-boot-autoconfigure
  • httpclient-spring-boot-starter
  • httpclient-spring-boot-sample

httpclient-spring-boot-starter

Build Status Code scanning - action Coverage Status Sonatype Nexus (Releases) Sonatype Nexus (Snapshots) License

This project provides a Spring-Boot Starter that enables the additional configuration of the used Httpclients.

It supports the configuration of OkHttp and Apache HttpClient which are also the supported Clients in Spring.

Usage

The httpclient-spring-boot-starter brings most of the required configuration with it, therefor you only need to add it as a maven dependency and enable the desired Httpclient.

<dependency>
  <groupId>de.dev-eth0.spring-boot.httpclient</groupId>
  <artifactId>httpclient-spring-boot-starter</artifactId>
  <version>LATEST-VERSION</version>
</dependency>

Make sure, that you have either org.apache.httpcomponents:httpclient or com.squareup.okhttp3:okhttp declared as a dependency and enable the configuration as described in spring-cloud-commons - http-clients.

This will make sure, that the spring-cloud dependencies use the custom client configurations.

Feign

Feign uses a different configuration to enable the clients: feign.okhttp.enabled and feign.httpclient.enabled.

RestTemplate

To make sure, your RestTemplate uses the custom client, you need to configure it accordingly:

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder, ClientHttpRequestFactory clientHttpRequestFactory) {
  return builder.requestFactory(() -> clientHttpRequestFactory).build();
}

Configuration

All configuration values are prefixed with http.client (e.g. http.client.timeouts.connectionTimeout).

It's possible to configure the proxy based on the requested hostnames using the hostPattern config.

Config Description Default Example
sslContext SSL Version (optional) TLSv1.2 TLSv1.3
timeouts.connectionTimeout (optional) Connection Timeout in ms 5000
timeouts.socketTimeout (optional) Socket Timeout in ms, for OkHttp this is used as readTimeout and writeTimeout 10000
proxies[] (optional) Configuration for used proxy servers
proxies[].hostPatterns Pattern for matching the hostname, empty matches all empty google.*
proxies[].proxyHost Hostname or IP of the Proxy 10.0.9.1 or corp-proxy.domain
proxies[].proxyPort Port of the Proxy (optional) 3128
proxies[].proxyUser Proxy user name (optional) testUser
proxies[].proxyPassword Proxy password (optional) testPassword
keystore.path Keystore file path classpath:keystore.jks
keystore.password Keystore password changeit
keystore.type Keystore type (optional) PKCS12
truststore.path Truststore file path classpath:truststore.jks
truststore.password Truststore password changeit
truststore.type Truststore type (optional) JKS

Example:

http:
  client:
    proxies:
      - hostPatterns: ["google.de]
        proxyHost: localhost
        proxyPort: 3333
        proxyUser: testUser
        proxyPassword: testPassword

    timeouts:
      connectionTimeout: 5000
      socketTimeout: 10000

Sample Project

You can find a sample project which configures both Feign and RestTemplate to use either OkHttp or Apache HttpClient in /httpclient-spring-boot-sample.

The project includes the spring-boot-maven-plugin therefor you can simply run mvn clean install spring:boot-run. By default, the project uses Apache HttpClient, if you want to use OkHttp, you can use the following command:

SPRING_PROFILES_ACTIVE=okhttp mvn spring-boot:run

The project also contains a docker-compose.yml file which starts several local servers to demonstrate the usecases.

Versions

Version
1.1.0
1.0.4
1.0.3
1.0.2