#1224 - Provide API for users to easily configure WebClient instances.

Introduce `HypermediaWebClientConfigurer` with a simple API that registers hypermedia types via `WebClient.Builder`.

Deprecate `WebClientConfigurer`, leveraging the new solution.

Update reference documentation showing how to use it, with and without Spring Boot.
This commit is contained in:
Greg Turnquist
2020-03-25 16:03:00 -05:00
parent dea0001da2
commit 904a03a241
17 changed files with 327 additions and 186 deletions

View File

@@ -12,6 +12,11 @@ public class HypermediaRestTemplateConfigurer {
private final WebConverters converters;
/**
* Creates a new {@link HypermediaRestTemplateConfigurer} using the {@link WebConverters}.
*
* @param converters
*/
HypermediaRestTemplateConfigurer(WebConverters converters) {
this.converters = converters;
}
@@ -20,7 +25,7 @@ public class HypermediaRestTemplateConfigurer {
* Insert hypermedia-aware message converters in front of any other existing message converters.
*
* @param template
* @return
* @return {@link RestTemplate} capable of speaking hypermedia.
*/
public RestTemplate registerHypermediaTypes(RestTemplate template) {

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2019-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.codec.ClientCodecConfigurer;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
import org.springframework.web.reactive.function.client.WebClient;
import java.util.List;
import java.util.function.Consumer;
/**
* Assembles {@link Jackson2JsonEncoder}s and {@link Jackson2JsonDecoder}s needed to wire a {@link WebClient} with
* hypermedia support.
*
* @author Greg Turnquist
* @author Oliver Drotbohm
* @since 1.1
*/
public class HypermediaWebClientConfigurer {
Consumer<ClientCodecConfigurer> configurer;
/**
* Creates a new {@link HypermediaWebClientConfigurer} for the given {@link ObjectMapper} and
* {@link HypermediaMappingInformation}s.
*
* @param mapper must not be {@literal null}.
* @param hypermediaTypes must not be {@literal null}.
*/
HypermediaWebClientConfigurer(ObjectMapper mapper, List<HypermediaMappingInformation> hypermediaTypes) {
Assert.notNull(mapper, "ObjectMapper must not be null!");
Assert.notNull(hypermediaTypes, "HypermediaMappingInformations must not be null!");
this.configurer = clientCodecConfigurer -> hypermediaTypes.forEach(hypermediaType -> {
ObjectMapper objectMapper = hypermediaType.configureObjectMapper(mapper.copy());
MimeType[] mimeTypes = hypermediaType.getMediaTypes().toArray(new MimeType[0]);
clientCodecConfigurer.customCodecs().registerWithDefaultConfig(new Jackson2JsonEncoder(objectMapper, mimeTypes));
clientCodecConfigurer.customCodecs().registerWithDefaultConfig(new Jackson2JsonDecoder(objectMapper, mimeTypes));
});
}
/**
* Apply the proper {@link Jackson2JsonEncoder}s and {@link Jackson2JsonDecoder}s to this {@link WebClient.Builder}.
*
* @param builder
* @return {@link WebClient.Builder} registered to handle hypermedia types.
*/
public WebClient.Builder registerHypermediaTypes(WebClient.Builder builder) {
return builder.codecs(this.configurer);
}
}

View File

@@ -1,39 +1,10 @@
/*
* Copyright 2019-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas.config;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.Encoder;
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
import org.springframework.http.codec.ClientCodecConfigurer;
import org.springframework.http.codec.CodecConfigurer.CustomCodecs;
import org.springframework.http.codec.json.AbstractJackson2Decoder;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
/**
* Assembles {@link ExchangeStrategies} needed to wire a {@link WebClient} with hypermedia support.
@@ -41,11 +12,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* @author Greg Turnquist
* @author Oliver Drotbohm
* @since 1.0
* @deprecated Migrate to {@link HypermediaWebClientConfigurer} and it's {@link WebClient.Builder}-oriented
* approach.
*/
@Configuration
@Deprecated
public class WebClientConfigurer {
Consumer<ClientCodecConfigurer> configurer;
private final HypermediaWebClientConfigurer hypermediaWebClientConfigurer;
/**
* Creates a new {@link WebClientConfigurer} for the given {@link ObjectMapper} and
@@ -55,29 +28,7 @@ public class WebClientConfigurer {
* @param hypermediaTypes must not be {@literal null}.
*/
public WebClientConfigurer(ObjectMapper mapper, List<HypermediaMappingInformation> hypermediaTypes) {
Assert.notNull(mapper, "ObjectMapper must not be null!");
Assert.notNull(hypermediaTypes, "HypermediaMappingInformations must not be null!");
List<Encoder<?>> encoders = new ArrayList<>();
List<AbstractJackson2Decoder> decoders = new ArrayList<>();
hypermediaTypes.forEach(hypermedia -> {
ObjectMapper objectMapper = hypermedia.configureObjectMapper(mapper.copy());
MimeType[] mimeTypes = hypermedia.getMediaTypes().toArray(new MimeType[0]);
encoders.add(new Jackson2JsonEncoder(objectMapper, mimeTypes));
decoders.add(new Jackson2JsonDecoder(objectMapper, mimeTypes));
});
this.configurer = it -> {
CustomCodecs codecs = it.customCodecs();
encoders.forEach(codecs::registerWithDefaultConfig);
decoders.forEach(codecs::registerWithDefaultConfig);
};
this.hypermediaWebClientConfigurer = new HypermediaWebClientConfigurer(mapper, hypermediaTypes);
}
/**
@@ -88,7 +39,7 @@ public class WebClientConfigurer {
public ExchangeStrategies hypermediaExchangeStrategies() {
return ExchangeStrategies.builder() //
.codecs(configurer) //
.codecs(this.hypermediaWebClientConfigurer.configurer) //
.build();
}
@@ -99,9 +50,6 @@ public class WebClientConfigurer {
* @return mutated webClient with hypermedia support.
*/
public WebClient registerHypermediaTypes(WebClient webClient) {
return webClient.mutate() //
.codecs(configurer) //
.build();
return this.hypermediaWebClientConfigurer.registerHypermediaTypes(webClient.mutate()).build();
}
}

View File

@@ -38,21 +38,20 @@ import java.util.List;
class WebClientHateoasConfiguration {
@Bean
WebClientConfigurer webClientConfigurer(ObjectProvider<ObjectMapper> mapper,
HypermediaWebClientConfigurer webClientConfigurer(ObjectProvider<ObjectMapper> mapper,
List<HypermediaMappingInformation> hypermediaTypes) {
return new WebClientConfigurer(mapper.getIfAvailable(ObjectMapper::new), hypermediaTypes);
return new HypermediaWebClientConfigurer(mapper.getIfAvailable(ObjectMapper::new), hypermediaTypes);
}
@Bean
@Lazy
HypermediaWebTestClientConfigurer webTestClientConfigurer(ObjectProvider<ObjectMapper> mapper,
List<HypermediaMappingInformation> hypermediaTypes) {
HypermediaWebTestClientConfigurer webTestClientConfigurer(ObjectProvider<ObjectMapper> mapper,
List<HypermediaMappingInformation> hypermediaTypes) {
return new HypermediaWebTestClientConfigurer(mapper.getIfAvailable(ObjectMapper::new), hypermediaTypes);
}
@Bean
static HypermediaWebClientBeanPostProcessor webClientBeanPostProcessor(
ObjectProvider<WebClientConfigurer> configurer) {
static HypermediaWebClientBeanPostProcessor webClientBeanPostProcessor(HypermediaWebClientConfigurer configurer) {
return new HypermediaWebClientBeanPostProcessor(configurer);
}
@@ -66,7 +65,7 @@ class WebClientHateoasConfiguration {
@RequiredArgsConstructor
static class HypermediaWebClientBeanPostProcessor implements BeanPostProcessor {
private final ObjectProvider<WebClientConfigurer> configurer;
private final HypermediaWebClientConfigurer configurer;
/*
* (non-Javadoc)
@@ -76,8 +75,8 @@ class WebClientHateoasConfiguration {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof WebClient) {
return this.configurer.getObject().registerHypermediaTypes((WebClient) bean);
if (WebClient.class.isInstance(bean)) {
return this.configurer.registerHypermediaTypes(((WebClient) bean).mutate()).build();
}
return bean;