- Spring Boot 2.0 Cookbook(Second Edition)
- Alex Antonov
- 118字
- 2021-06-24 19:24:42
How to do it...
There are a number of ways in which we can configure converters. It all depends on which one you prefer or how much control you want to achieve.
- Let's add ByteArrayHttpMessageConverter as @Bean to our WebConfiguration class in the following manner:
@Bean public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() { return new ByteArrayHttpMessageConverter(); }
- Another way to achieve this is to override the configureMessageConverters method in the WebConfiguration class, which extends WebMvcConfigurerAdapter, defining such a method as follows:
@Override public void configureMessageConverters
(List<HttpMessageConverter<?>> converters) { converters.add(new ByteArrayHttpMessageConverter()); }
- If you want to have a bit more control, we can override the extendMessageConverters method in the following way:
@Override public void extendMessageConverters
(List<HttpMessageConverter<?>> converters) { converters.clear(); converters.add(new ByteArrayHttpMessageConverter()); }