Skip to content

Commit

Permalink
Auto-configure headers with static values in REST starter.
Browse files Browse the repository at this point in the history
  • Loading branch information
ch4mpy committed Nov 25, 2024
1 parent 57733e4 commit 294bf6e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
14 changes: 14 additions & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ For Spring Boot 3.4.x.

`spring-addons-starter-rest` provides auto-configuration for `RestClient`, `WebClient` and tooling for `@HttpExchange` proxy generation.

### `8.0.1`
- Auto-configure static headers with `spring-addons-starter-rest`. Sample:
```yaml
com:
c4-soft:
springaddons:
rest:
client:
greet-client:
base-url: ${client-uri}/api
headers:
X-API-KEY: change-me
```
### `8.0.0`
- [`spring-addons-starter-rest`](https://github.com/ch4mpy/spring-addons/tree/master/spring-addons-starter-rest) now expose as `@Bean` some `RestClient` and `WebClient` instances (or builders) with the following configured using application properties:
- Base URI
Expand Down
10 changes: 10 additions & 0 deletions spring-addons-starter-rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This starter aims at auto-configuring `RestClient` and `WebClient` using applica
- `Basic` or OAuth2 `Bearer` authorization; for the latter, using either a client registration or forwarding the access token in the security context of a resource server.
- proxy settings with consideration of `HTTP_PROXY` and `NO_PROXY` environment variables. Finer-grained configuration or overrides can be achieved with custom properties.
- connection and read timeouts
- headers with static values (for instance an API key)

Instantiated REST clients are `WebClient` in WebFlux apps and `RestClient` in servlets, but any client can be switched to `WebClient` in servlets.

Expand Down Expand Up @@ -93,6 +94,15 @@ com:
http:
proxy:
enabled: false
chouette-client:
base-url: https://something.pf/api
# add headers with static values
headers:
X-API-KEY: change-me
X-MULTI-VALUED-HEADER:
- foo
- bar
- bam
```
The `biduleClientBuilder` bean can be used to define a `biduleClient` bean as follows:
```java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public WebClient.Builder getObject() throws Exception {

setAuthorizationHeader(builder, clientProps.getAuthorization(), clientId);

for (var header : clientProps.getHeaders().entrySet()) {
builder.defaultHeader(header.getKey(),
header.getValue().toArray(new String[header.getValue().size()]));
}

return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ public RestClient.Builder getObject() throws Exception {

setAuthorizationHeader(builder, clientProps.getAuthorization(), clientId);

for (var header : clientProps.getHeaders().entrySet()) {
builder.defaultHeader(header.getKey(),
header.getValue().toArray(new String[header.getValue().size()]));
}

return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.net.URL;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.springframework.boot.autoconfigure.AutoConfiguration;
Expand Down Expand Up @@ -119,6 +120,13 @@ public static class RestClientProperties {
*/
private Optional<String> beanName = Optional.empty();

/**
* Some static headers to add to all requests sent by this client (for instance an API key). The
* key is the header name. If the value contains several entries, the header is set several
* times.
*/
private Map<String, List<String>> headers = new HashMap<>();

public Optional<URL> getBaseUrl() {
return baseUrl.map(t -> {
try {
Expand Down

0 comments on commit 294bf6e

Please sign in to comment.