diff --git a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-config/message-converters.adoc b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-config/message-converters.adoc index 0f3d79bc71c..1ead038524e 100644 --- a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-config/message-converters.adoc +++ b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-config/message-converters.adoc @@ -3,11 +3,18 @@ [.small]#xref:web/webflux/config.adoc#webflux-config-message-codecs[See equivalent in the Reactive stack]# -You can customize `HttpMessageConverter` in Java configuration by overriding -{api-spring-framework}/web/servlet/config/annotation/WebMvcConfigurer.html#configureMessageConverters-java.util.List-[`configureMessageConverters()`] -(to replace the default converters created by Spring MVC) or by overriding -{api-spring-framework}/web/servlet/config/annotation/WebMvcConfigurer.html#extendMessageConverters-java.util.List-[`extendMessageConverters()`] -(to customize the default converters or add additional converters to the default ones). +You can set the `HttpMessageConverter` instances to use in Java configuration, +replacing the ones used by default, by overriding +{api-spring-framework}/web/servlet/config/annotation/WebMvcConfigurer.html#configureMessageConverters-java.util.List-[`configureMessageConverters()`]. +You can also customize the list of configured message converters at the end by overriding +{api-spring-framework}/web/servlet/config/annotation/WebMvcConfigurer.html#extendMessageConverters-java.util.List-[`extendMessageConverters()`]. + +TIP: In a Spring Boot application, the `WebMvcAutoConfiguration` adds any +`HttpMessageConverter` beans it detects, in addition to default converters. Hence, in a +Boot application, prefer to use the +https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-config/message-converters.html[HttpMessageConverters] +mechanism. Or alternatively, use `extendMessageConverters` to modify message converters +at the end. The following example adds XML and Jackson JSON converters with a customized `ObjectMapper` instead of the default ones: diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java index 8055d62e258..eb329f47e0d 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 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. @@ -166,10 +166,14 @@ public interface WebMvcConfigurer { *
By default, all built-in converters are configured as long as the * corresponding 3rd party libraries such Jackson JSON, JAXB2, and others * are present on the classpath. - *
Note use of this method turns off default converter - * registration. Alternatively, use - * {@link #extendMessageConverters(java.util.List)} to modify that default - * list of converters. + *
Note that use of this method turns off default converter
+ * registration. However, in a Spring Boot application the
+ * {@code WebMvcAutoConfiguration} adds any {@code HttpMessageConverter}
+ * beans as well as default converters. Hence, in a Boot application use
+ * HttpMessageConverters.
+ * Alternatively, for any scenario, use
+ * {@link #extendMessageConverters(java.util.List)} to modify the configured
+ * list of message converters.
* @param converters initially an empty list of converters
*/
default void configureMessageConverters(List