diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jackson/EndpointObjectMapper.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jackson/EndpointObjectMapper.java index b1b698c94b1..91c11a9fb1a 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jackson/EndpointObjectMapper.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jackson/EndpointObjectMapper.java @@ -16,13 +16,15 @@ package org.springframework.boot.actuate.endpoint.jackson; +import java.util.Set; + import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.boot.actuate.endpoint.OperationResponseBody; /** * Interface used to supply the {@link ObjectMapper} that should be used when serializing - * {@link OperationResponseBody} endpoint results. + * endpoint results. * * @author Phillip Webb * @since 3.0.0 @@ -30,6 +32,11 @@ import org.springframework.boot.actuate.endpoint.OperationResponseBody; */ public interface EndpointObjectMapper { + /** + * The default supported types. + */ + Set> DEFAULT_SUPPORTED_TYPES = Set.of(OperationResponseBody.class); + /** * Return the {@link ObjectMapper} that should be used to serialize * {@link OperationResponseBody} endpoint results. @@ -37,4 +44,13 @@ public interface EndpointObjectMapper { */ ObjectMapper get(); + /** + * Return the types that this endpoint mapper supports. + * @return the supported types + * @since 4.0.0 + */ + default Set> getSupportedTypes() { + return DEFAULT_SUPPORTED_TYPES; + } + } diff --git a/spring-boot-project/spring-boot-jersey/src/main/java/org/springframework/boot/jersey/autoconfigure/actuate/web/JerseyWebEndpointManagementContextConfiguration.java b/spring-boot-project/spring-boot-jersey/src/main/java/org/springframework/boot/jersey/autoconfigure/actuate/web/JerseyWebEndpointManagementContextConfiguration.java index 2816efab3f5..e8b3f50a7ba 100644 --- a/spring-boot-project/spring-boot-jersey/src/main/java/org/springframework/boot/jersey/autoconfigure/actuate/web/JerseyWebEndpointManagementContextConfiguration.java +++ b/spring-boot-project/spring-boot-jersey/src/main/java/org/springframework/boot/jersey/autoconfigure/actuate/web/JerseyWebEndpointManagementContextConfiguration.java @@ -229,7 +229,12 @@ class JerseyWebEndpointManagementContextConfiguration { @Override public ObjectMapper getContext(Class type) { - return OperationResponseBody.class.isAssignableFrom(type) ? this.endpointObjectMapper.get() : null; + for (Class supportedType : this.endpointObjectMapper.getSupportedTypes()) { + if (supportedType.isAssignableFrom(type)) { + return this.endpointObjectMapper.get(); + } + } + return null; } } diff --git a/spring-boot-project/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/actuate/web/WebFluxEndpointManagementContextConfiguration.java b/spring-boot-project/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/actuate/web/WebFluxEndpointManagementContextConfiguration.java index cdbf8ac5d84..fd78830470a 100644 --- a/spring-boot-project/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/actuate/web/WebFluxEndpointManagementContextConfiguration.java +++ b/spring-boot-project/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/actuate/web/WebFluxEndpointManagementContextConfiguration.java @@ -21,6 +21,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.function.Supplier; import com.fasterxml.jackson.databind.ObjectMapper; @@ -66,6 +67,7 @@ import org.springframework.http.codec.EncoderHttpMessageWriter; import org.springframework.http.codec.HttpMessageWriter; import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.http.server.reactive.HttpHandler; +import org.springframework.util.MimeType; import org.springframework.util.StringUtils; import org.springframework.util.function.SingletonSupplier; import org.springframework.web.reactive.DispatcherHandler; @@ -183,13 +185,18 @@ public class WebFluxEndpointManagementContextConfiguration { @SuppressWarnings({ "removal", "deprecation" }) private void process(Encoder encoder) { if (encoder instanceof org.springframework.http.codec.json.Jackson2JsonEncoder jackson2JsonEncoder) { - jackson2JsonEncoder.registerObjectMappersForType(OperationResponseBody.class, (associations) -> { - ObjectMapper objectMapper = this.endpointObjectMapper.get().get(); - MEDIA_TYPES.forEach((mimeType) -> associations.put(mimeType, objectMapper)); - }); + this.endpointObjectMapper.get() + .getSupportedTypes() + .forEach((type) -> jackson2JsonEncoder.registerObjectMappersForType(type, + this::registerForAllMimeTypes)); } } + private void registerForAllMimeTypes(Map registrar) { + ObjectMapper objectMapper = this.endpointObjectMapper.get().get(); + MEDIA_TYPES.forEach((mimeType) -> registrar.put(mimeType, objectMapper)); + } + } } diff --git a/spring-boot-project/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/actuate/web/WebMvcEndpointManagementContextConfiguration.java b/spring-boot-project/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/actuate/web/WebMvcEndpointManagementContextConfiguration.java index c950e258ff4..1452690924f 100644 --- a/spring-boot-project/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/actuate/web/WebMvcEndpointManagementContextConfiguration.java +++ b/spring-boot-project/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/actuate/web/WebMvcEndpointManagementContextConfiguration.java @@ -21,6 +21,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Map; import com.fasterxml.jackson.databind.ObjectMapper; @@ -185,10 +186,13 @@ public class WebMvcEndpointManagementContextConfiguration { @SuppressWarnings("removal") private void configure(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter converter) { - converter.registerObjectMappersForType(OperationResponseBody.class, (associations) -> { - ObjectMapper objectMapper = this.endpointObjectMapper.get(); - MEDIA_TYPES.forEach((mimeType) -> associations.put(mimeType, objectMapper)); - }); + this.endpointObjectMapper.getSupportedTypes() + .forEach((type) -> converter.registerObjectMappersForType(type, this::registerForAllMimeTypes)); + } + + private void registerForAllMimeTypes(Map registrar) { + ObjectMapper objectMapper = this.endpointObjectMapper.get(); + MEDIA_TYPES.forEach((mimeType) -> registrar.put(mimeType, objectMapper)); } }