Spring Boot 2.0 Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

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.

  1. Let's add ByteArrayHttpMessageConverter as @Bean to our WebConfiguration class in the following manner:
@Bean 
public  
  ByteArrayHttpMessageConverter  
    byteArrayHttpMessageConverter() { 
  return new ByteArrayHttpMessageConverter(); 
} 
  1. 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()); }
  1. 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()); }