Browse Source

Add EndpointObjectMapper supported types method

Closes gh-45876
pull/46230/head
Phillip Webb 8 months ago committed by Andy Wilkinson
parent
commit
b19403c9fc
  1. 18
      spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jackson/EndpointObjectMapper.java
  2. 7
      spring-boot-project/spring-boot-jersey/src/main/java/org/springframework/boot/jersey/autoconfigure/actuate/web/JerseyWebEndpointManagementContextConfiguration.java
  3. 15
      spring-boot-project/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/actuate/web/WebFluxEndpointManagementContextConfiguration.java
  4. 12
      spring-boot-project/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/actuate/web/WebMvcEndpointManagementContextConfiguration.java

18
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jackson/EndpointObjectMapper.java

@ -16,13 +16,15 @@ @@ -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; @@ -30,6 +32,11 @@ import org.springframework.boot.actuate.endpoint.OperationResponseBody;
*/
public interface EndpointObjectMapper {
/**
* The default supported types.
*/
Set<Class<?>> 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 { @@ -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<Class<?>> getSupportedTypes() {
return DEFAULT_SUPPORTED_TYPES;
}
}

7
spring-boot-project/spring-boot-jersey/src/main/java/org/springframework/boot/jersey/autoconfigure/actuate/web/JerseyWebEndpointManagementContextConfiguration.java

@ -229,7 +229,12 @@ class JerseyWebEndpointManagementContextConfiguration { @@ -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;
}
}

15
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; @@ -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; @@ -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 { @@ -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<MimeType, ObjectMapper> registrar) {
ObjectMapper objectMapper = this.endpointObjectMapper.get().get();
MEDIA_TYPES.forEach((mimeType) -> registrar.put(mimeType, objectMapper));
}
}
}

12
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; @@ -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 { @@ -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<MediaType, ObjectMapper> registrar) {
ObjectMapper objectMapper = this.endpointObjectMapper.get();
MEDIA_TYPES.forEach((mimeType) -> registrar.put(mimeType, objectMapper));
}
}

Loading…
Cancel
Save