Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebClientConfigurer overrides exchange strategies #867

Closed
marcingrzejszczak opened this issue Mar 15, 2019 · 5 comments
Closed

WebClientConfigurer overrides exchange strategies #867

marcingrzejszczak opened this issue Mar 15, 2019 · 5 comments

Comments

@marcingrzejszczak
Copy link

marcingrzejszczak commented Mar 15, 2019

Not sure if feature or bug ;) (Hateoas version 1.0.0.M1)

public class WebClientConfigurer {

    private final ObjectMapper mapper;
    private final Collection<HypermediaMappingInformation> hypermediaTypes;

    /**
     * Return a set of {@link ExchangeStrategies} driven by registered {@link HypermediaType}s.
     *
     * @return a collection of {@link Encoder}s and {@link Decoder} assembled into a {@link ExchangeStrategies}.
     */
    public ExchangeStrategies hypermediaExchangeStrategies() {

        List<Encoder<?>> encoders = new ArrayList<>();
        List<Decoder<?>> decoders = new ArrayList<>();

        this.hypermediaTypes.forEach(hypermedia -> {

            ObjectMapper objectMapper = hypermedia.configureObjectMapper(this.mapper.copy());
            MimeType[] mimeTypes = hypermedia.getMediaTypes().toArray(new MimeType[0]);

            encoders.add(new Jackson2JsonEncoder(objectMapper, mimeTypes));
            decoders.add(new Jackson2JsonDecoder(objectMapper, mimeTypes));
        });

        encoders.add(CharSequenceEncoder.allMimeTypes());
        decoders.add(StringDecoder.allMimeTypes());

        return ExchangeStrategies.builder().codecs(clientCodecConfigurer -> {

            encoders.forEach(encoder -> clientCodecConfigurer.customCodecs().encoder(encoder));
            decoders.forEach(decoder -> clientCodecConfigurer.customCodecs().decoder(decoder));

            clientCodecConfigurer.registerDefaults(false);
        }).build();
    }

    /**
     * Register the proper {@link ExchangeStrategies} for a given {@link WebClient}.
     *
     * @param webClient
     * @return mutated webClient with hypermedia support.
     */
    public WebClient registerHypermediaTypes(WebClient webClient) {
        return webClient.mutate().exchangeStrategies(hypermediaExchangeStrategies()).build();
    }
}

this

  public WebClient registerHypermediaTypes(WebClient webClient) {
        return webClient.mutate().exchangeStrategies(hypermediaExchangeStrategies()).build();
    }

overrides the existing exchangeStrategies, which results in

org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'text/plain;charset=UTF-8' not supported for bodyType=org.springframework.core.io.buffer.DataBuffer

when working with Sleuth.

@odrotbohm
Copy link
Member

@gregturn – Is this related to #885?

@gregturn
Copy link
Contributor

Apply spring-projects/spring-framework#22612 when Spring HATEOAS is used with Spring Framwork 5.2.

Retain existing solution when using Spring Framework 5.1.

@gregturn
Copy link
Contributor

Indeed, #885 duplicates this issue, so I dispositioned it and closed it. I also linked the original Spring Framework issue in here, so we could track everything from here.

gregturn added a commit that referenced this issue Mar 21, 2019
Spring Framework 5.1 puts custom encoders and decoders in the wrong place, so Spring HATEOAS can't properly register with WebFlux. To make Spring HATEOS media types work requires overriding features that break other toolkits, like Spring Cloud Sleuth. However, in Spring Framework 5.2, this is no longer needed, so we need only disable the registry when Spring Framework 5.1 is on the classpath.

Related issues: #885, spring-projects/spring-framework#22612
@odrotbohm
Copy link
Member

I've upgraded to Spring Framework 5.2 snapshots for #919, feel free to prepare the necessary changes.

gregturn added a commit that referenced this issue Mar 22, 2019
Thanks to spring-projects/spring-framework#22612, Spring HATEAOAS no longer has to disable the default registry of encoders and decoders to get its custom components to work.

Related issues: #885
gregturn added a commit that referenced this issue Mar 23, 2019
Thanks to spring-projects/spring-framework#22612, Spring HATEAOAS no longer has to disable the default registry of encoders and decoders to get its custom components to work.

Related issues: #885
gregturn added a commit that referenced this issue Apr 3, 2019
Thanks to spring-projects/spring-framework#22612, Spring HATEAOAS no longer has to disable the default registry of encoders and decoders to get its custom components to work.

Related issues: #885, spring-projects/spring-framework#22612
@gregturn
Copy link
Contributor

gregturn commented Apr 3, 2019

Resolved via fc3767b.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants