1

I want to convert spring resttemplate to spring webclient. in spring resttemplate we can add message converters

How can I tell RestTemplate to POST with UTF-8 encoding?

restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));

How can I do it in spring webclient? I want to add message converter to webclient

Bilgehan
  • 1,135
  • 1
  • 14
  • 41

1 Answers1

0

In WebClient you can setup custom codecs(Encoder, Decoder, HttpMessageReader, HttpMessageWriter) using WebClient.builder().codecs()

Here is an example:

WebClient.builder()
   .codecs(
        clientCodecConfigurer ->{
            // .defaultCodecs() set defaultCodecs for you
            // clientCodecConfigurer.defaultCodecs();

            //  You can customize an encoder based on the defualt config.
            //  clientCodecConfigurer.defaultCodecs().jackson2Encoder(...)

            // Or 
            // use customCodecs to register Codecs from scratch.
            clientCodecConfigurer.customCodecs().register(new Jackson2JsonDecoder());
            clientCodecConfigurer.customCodecs().register(new Jackson2JsonEncoder());
        }

).build();
kerbermeister
  • 2,985
  • 3
  • 11
  • 30